C语言题目: IPV6(数组,函数)
c:
我们提供的服务有:网站设计、成都做网站、微信公众号开发、网站优化、网站认证、密云ssl等。为千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的密云网站制作公司
#include stdio.h
#include stdlib.h
#include string.h
int main(void){
char (*p)[40];
char ipv6[40],delims[] = ":",*result = NULL;
int n,flag=0,len,j;
scanf("%d",n);
p=malloc(sizeof(char [40])*n);
for(int i=0;in;i++){
scanf("%s",ipv6);
flag=0;
len=0;
result = strtok(ipv6,delims);
while( result != NULL) {
if(strcmp(result,"0000")==0){
if(!flag){
flag=1;
strcat(p[i],":");
len++;
}
if(flag==2){
strcat(p[i],"0:");
len+=2;
}
}else{
int f=0;
for(j=0;jstrlen(result);j++){
if(result[j]=='0' !f)
continue;
p[i][len++]=result[j];
f=1;
}
strcat(p[i],":");
len++;
if(flag==1) flag=2;
}
result= strtok( NULL,delims);
}
if(flag!=1)
p[i][len-1]='\0';
}
for(int i=0;in;i++){
printf("%s\n",p[i]);
}
return 0;
}
C语言编程题,若函数形参为一维数组作函数参数编写函数,统计某一维数组中非0元素的个数?
#include stdio.h
int func(int a[],int n)
{
int i,cnt;
for(cnt=0,i=0;in;++i)
if(a[i])
cnt++;
return cnt;
}
int main()
{
int a[10]={0,1,2,3,4,5,6,7,8,9};
printf("%d\n",func(a,10));
return 0;
}
c语言编程题 二维数组和函数
#include stdio.h
#include stdlib.h
#include string.h
int Input(long number[], int score[][3]) {
int i=0;
printf("Input Score: Number Grad1 Grad2 Grad3\n");
while (i30) {
scanf("%ld %d %d %d", number[i], score[i][0], score[i][1], score[i][2]);
if (number[i]0) break;
i++;
}
return i;
}
void Total1(int score[][3], float sum[], float aver[], int n) {
int i;
for (i=0;in;i++) {
sum[i] = score[i][0] + score[i][1] + score[i][2];
aver[i] = sum[i]/3;
}
}
void Total2(int score[][3], float sum[], float aver[], int n) {
int i;
sum[0] = sum[1] = sum[2] = 0.0f;
for (i=0;in;i++) {
sum[0] += score[i][0];
sum[1] += score[i][1];
sum[2] += score[i][2];
}
for (i=0;i3;i++)
aver[i] = sum[i]/3;
}
void Total3(long num[],int score[][3],float sum[],float aver[],int n) {
int i, j;
long _n;
int _s;
float _sum, _ave;
Total1(score, sum, aver, n);
for (i=0;in-1;i++) {
for (j=n;ji;j--) {
if (sum[i]sum[j]) {
//swap (i, j)
_n = num[i]; num[i] = num[j]; num[j] = _n;
_s = score[i][0];score[i][0] = score[j][0]; score[j][0] = _s;
_s = score[i][1];score[i][1] = score[j][1]; score[j][1] = _s;
_s = score[i][2];score[i][2] = score[j][2]; score[j][2] = _s;
_sum = sum[i]; sum[i] = sum[j]; sum[j] = _sum;
_ave = aver[i]; aver[i] = aver[j]; aver[j] = _ave;
}
}
}
}
void Print(long num[],int score[][3],float sum1[],float aver1[],float sum2[],float aver2[],int n) {
int i;
printf("Pos\tNumber\tGrad1\tGrad2\tGrad3\tSum1\tAver1\n");
for (i=0;in;i++) {
printf("%d\t%ld\t%d\t%d\t%d\t%.1f\t%.1f\n", (i+1), num[i], score[i][0], score[i][1], score[i][2], sum1[i], aver1[i]);
}
printf("No\tSum2\tAver2\n");
for (i=0;i3;i++) {
printf("%d\t%.1f\t%.1f\n", (i+1), sum2[i], aver2[i]);
}
}
int main() {
long number[30];
int score[30][3];
float sum1[30], aver1[30];
float sum2[30], aver2[30];
float sum3[3], aver3[3];
int n;
n = Input(number, score);
Total1(score, sum1, aver1, n);
Total2(score, sum3, aver3, n);
Print(number, score, sum1, aver1, sum3, aver3, n);
Total3(number, score, sum2, aver2, n);
Print(number, score, sum2, aver2, sum3, aver3, n);
return 0;
}
当前文章:c语言数组和函数例题 c语言数组和函数结合
分享链接:http://lswzjz.com/article/ddjhice.html