82. 程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是对形参b所指结构体变量中的数据进行修改,最后在主函数中输出修改后的数据。
例如:b所指变量t中的学号、姓名、和三门课的成绩依次是:10002、“ZhangQi”、93、85、87,修改后输出t中的数据应为:10004、“LiJie”、93、85、87。
#include <stdio.h>
#include <string.h>
struct student
{ long sno;
char name[10];
float score[3];
};
void fun( struct student *b)
{ int i;
/*******************************found******************************/
b__1__ = 10004; ->sno
/*******************************found******************************/
strcpy(b__2__, "LiJie"); ->name
}
main( )
{ struct student t={10002,"ZhangQi", 93, 85, 87};
int i;
printf("\n\nThe original data :\n");
printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);
for (i=0; i<3; i ) printf("%6.2f ", t.score[i]);
printf("\n");
/*******************************found******************************/
fun(__3__); &t
printf("\nThe data after modified :\n");
printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);
for (i=0; i<3; i ) printf("%6.2f ", t.score[i]);
printf("\n"); }
83. 给定程序中,函数fun的功能是:判定形参a所指的N×N(规定N为奇数)的矩阵是否是“幻方”,若是,函数返回值为1;不是,函数返回值为0。“幻方”的判定条件是;矩阵每行、每列、主对角线及反对角线上元素之和都相等。
例如,以下3×3的矩阵就是一个“幻方”:
4 9 2
3 5 7
8 1 6
#include <stdio.h>
#define N 3
int fun(int (*a)[N])
{ int i,j,m1,m2,row,colum;
m1=m2=0;
for(i=0; i<N; i )
{ j=N-i-1; m1 =a[i][i]; m2 =a[i][j]; }
if(m1!=m2) return 0;
for(i=0; i<N; i )
/*******************************found******************************/
{ row=colum= __1__; 0
for(j=0; j<N; j )
{ row =a[i][j]; colum =a[j][i]; }
/*******************************found******************************/
if( (row!=colum) __2__ (row!=m1) ) return 0; ||
}
/*******************************found******************************/
return __3__; 1
}
main( )
{ int x[N][N],i,j;
printf("Enter number for array:\n");
for(i=0; i<N; i )
for(j=0; j<N; j ) scanf("%d",&x[i][j]);
printf("Array:\n");
for(i=0; i<N; i )
{ for(j=0; j<N; j ) printf("%3d",x[i][j]);
printf("\n"); }
if(fun(x)) printf("The Array is a magic square.\n");
else printf("The Array isn't a magic square.\n"); }
84. 程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是将形参a所指结构体变量s中的数据进行修改,并把a中地址作为函数值返回主函数,在主函数中输出修改后的数据。
例如:a所指变量s中的学号、姓名、和三门课的成绩依次是:10001、“ZhangSan”、95、80、88,修改后输出t中的数据应为:10002、“LiSi”、96、81、89。
#include <stdio.h>
#include <string.h>
struct student
{ long sno;
char name[10];
float score[3];
};
/*******************************found******************************/
__1__ fun(struct student *a) struct student*
{ int i;
a->sno = 10002;
strcpy(a->name, "LiSi");
/*******************************found******************************/
for (i=0; i<3; i ) __2__ = 1; a->score[i]
/*******************************found******************************/
return __3__ ; 1
}
main( )
{ struct student s={10001,"ZhangSan", 95, 80, 88}, *t;
int i;
printf("\n\nThe original data :\n");
printf("\nNo: %ld Name: %s\nScores: ",s.sno, s.name);
for (i=0; i<3; i ) printf("%6.2f ", s.score[i]);
printf("\n");
t = fun(&s);
printf("\nThe data after modified :\n");
printf("\nNo: %ld Name: %s\nScores: ",t->sno, t->name);
for (i=0; i<3; i ) printf("%6.2f ", t->score[i]);
printf("\n"); }
85. 给定程序中,函数fun的功能是:计算下式前n项的和作为函数值返回。
S= …
例如,当形参n的值为10时,函数返回:9.612558。
#include <stdio.h>
double fun(int n)
{ int i; double s, t;
/*******************************found******************************/
s=__1__; 0
/*******************************found******************************/
for(i=1; i<=__2__; i ) n
{ t=2.0*i;
/*******************************found******************************/
s=s (2.0*i-1)*(2.0*i 1)/__3__; (t*t)
}
return s;
}
main( )
{ int n=-1;
while(n<0)
{ printf("Please input(n>0): "); scanf("%d",&n); }
printf("\nThe result is: %f\n",fun(n)); }
86. 给定程序中,函数fun的功能是:计算出形参s所指字符串中包含的单词个数,作为函数值返回。为便于统计,规定各单词之间用空格隔开。
例如,形参s所指的字符串为:This is a C language program ,函数的返回值为6。
#include <stdio.h>
int fun(char *s)
{ int n=0, flag=0;
while(*s!='\0')
{ if(*s!=' ' && flag==0) {
/*******************************found******************************/
__1__ ; flag=1;} n
/*******************************found******************************/
if (*s==' ') flag= __2__ ; 0
/*******************************found******************************/
__3__ ; s
}
return n;
}
main( )
{ char str[81]; int n;
printf("\nEnter a line text:\n"); gets(str);
n=fun(str);
printf("\nThere are %d words in this text.\n\n",n); }
87. 程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数据均以二进制方式输出到文件中。函数fun的功能是从形参filename所指的文件中读入学生数据,并按照学号从小到大排序后,再用二进制方式把排序后的学生数据输出到filename所指的文件中,覆盖原来的文件内容。
#include <stdio.h>
#define N 5
typedef struct student
{ long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename)
{ FILE *fp; int i, j;
STU s[N], t;
/*******************************found******************************/
fp = fopen(filename, __1__); “rb”
fread(s, sizeof(STU), N, fp);
fclose(fp);
for (i=0; i<N-1; i )
for (j=i 1; j<N; j )
/*******************************found******************************/
if (s[i].sno __2__) >s[j].sno
{ t = s[i]; s[i] = s[j]; s[j] = t; }
fp = fopen(filename, "wb");
/*******************************found******************************/
__3__(s, sizeof(STU), N, fp); fwrite
fclose(fp); }
main( )
{ STU t[N]={ {10005,"ZhangSan", 95, 80, 88}, {10003,"LiSi", 85, 70, 78},
{10002,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87},
{10001,"MaChao", 91, 92, 77}}, ss[N];
int i,j; FILE *fp;
fp = fopen("student.dat", "wb");
fwrite(t, sizeof(STU), 5, fp);
fclose(fp);
printf("\n\nThe original data :\n\n");
for (j=0; j<N; j )
{ printf("\nNo: %ld Name: %-8s Scores: ",t[j].sno, t[j].name);
for (i=0; i<3; i ) printf("%6.2f ", t[j].score[i]);
printf("\n"); }
fun("student.dat");
printf("\n\nThe data after sorting :\n\n");
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), 5, fp);
fclose(fp);
for (j=0; j<N; j )
{ printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i ) printf("%6.2f ", ss[j].score[i]);
printf("\n"); }
}
88. 给定程序中,函数fun的功能是:在形参ss所指字符串数组中,查找含有形参substr所指子串的所有字符串并输出,若没找到则输出相应信息。ss所指字符串数组中共有N个字符串,且串长小于M。程序中库函数strstr(s1,s2)的功能是在s1串中查找s2子串,若没有,函数值为0,若有,为非0。
#include <stdio.h>
#include <string.h>
#define N 5
#define M 15
void fun(char (*ss)[M], char *substr)
{ int i,find=0;
/*******************************found******************************/
for(i=0; i< __1__ ; i ) N
/*******************************found******************************/
if( strstr(ss[i], __2__) != NULL ) substr
{ find=1; puts(ss[i]); printf("\n"); }
/*******************************found******************************/
if (find==__3__) printf("\nDon't found!\n") 0
}
main( )
{ char x[N][M]={"BASIC","C langwage","Java","QBASIC","Access"},str[M];
int i;
printf("\nThe original string\n\n");
for(i=0;i<N;i )puts(x[i]); printf("\n");
printf("\nEnter a string for search : "); gets(str);
fun(x,str); }
89. 给定程序中,函数fun的功能是:将形参n中,各位上为偶数的取出,并按原来从高位到低位的顺序组成一个新的数,并作为函数返回。
例如,从主函数输入一个整数:27638496,函数返回值为:26846。
#include <stdio.h>
unsigned long fun(unsigned long n)
{ unsigned long x=0, s, i; int t;
s=n;
/*******************************found******************************/
i=__1__; 1
/*******************************found******************************/
while(__2__) s!=0
{ t=s%10;
if(t%2==0) {
/*******************************found******************************/
x=x t*i; i=__3__; i*10
}
s=s/10; }
return x;
}
main( )
{ unsigned long n=-1;
while(n>99999999||n<0)
{ printf("Please input(0<n<100000000): "); scanf("%ld",&n); }
printf("\nThe result is: %ld\n",fun(n)); }
90. 给定程序中,函数fun的功能是将带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为:2、4、6、8、10,逆置后,从头至尾结点数据域依次为:10、8、6、4、2。
#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef struct node
{ int data;
struct node *next;
} NODE;
void fun(NODE *h)
{ NODE *p, *q, *r;
/*******************************found******************************/
p = __1__; h->next
/*******************************found******************************/
if (__2__) return; p->next==NULL
q = p->next;
p->next = NULL;
while (q)
{ r = q->next; q->next = p;
/*******************************found******************************/
p = q; q = __3__; r
}
h->next = p;
}
NODE *creatlist(int a[])
{ NODE *h,*p,*q; int i;
h = (NODE *)malloc(sizeof(NODE));
h->next = NULL;
for(i=0; i<N; i )
{ q=(NODE *)malloc(sizeof(NODE));
q->data=a[i];
q->next = NULL;
if (h->next == NULL) h->next = p = q;
else { p->next = q; p = q; }
}
return h;
}
void outlist(NODE *h)
{ NODE *p;
p = h->next;
if (p==NULL) printf("The list is NULL!\n");
else { printf("\nHead ");
do{ printf("->%d", p->data); p=p->next; } while(p!=NULL);
printf("->End\n"); }
}
main( )
{ NODE *head;
int a[N]={2,4,6,8,10};
head=creatlist(a); printf("\nThe original list:\n");
outlist(head); fun(head);
printf("\nThe list after inverting :\n");
outlist(head);
}
91. 给定程序中,函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),将所指数组中小于平均值的数据移至数组的前部,大于等于平均值的数据移至x所指数组的后部,平均值作为函数值返回,在主函数中输出平均值和移动后的数据。
例如,有10个正数:46 30 32 40 6 17 45 15 48 26,平均值为:30.500000
移动后的输出为:30 6 17 15 26 46 32 40 45 48
#include <stdlib.h>
#include <stdio.h>
#define N 10
double fun(double *x)
{ int i, j; double av, y[N];
av=0;
/*******************************found******************************/
for(i=0; i<N; i ) av =__1__; x[i]/N
for(i=j=0; i<N; i )
if( x[i]<av ){
/*******************************found******************************/
y[j]=x[i]; x[i]=-1; __2__;} j
i=0;
while(i<N)
{ if( x[i]!= -1 ) y[j ]=x[i];
/*******************************found******************************/
__3__; i
}
for(i=0; i<N; i )x[i] = y[i];
return av;
}
main( )
{ int i; double x[N];
for(i=0; i<N; i ){ x[i]=rand()%50; printf("%4.0f ",x[i]);}
printf("\n");
printf("\nThe average is: %f\n",fun(x));
printf("\nThe result :\n",fun(x));
for(i=0; i<N; i ) printf("%5.0f ",x[i]);
printf("\n"); }
92. 给定程序中,函数fun的功能是:判断形参s所指字符串是否是“回文”(Palindrome),若是,函数返回值为1;不是,函数返回值为0。“回文”是正读和反读都一样的字符串(不区分大小写字母)。
例如,LEVEL和Level是“回文”,而LEVLEV不是“回文”。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int fun(char *s)
{ char *lp,*rp;
/*******************************found******************************/
lp= __1__ ; s
rp=s strlen(s)-1;
while((toupper(*lp)==toupper(*rp)) && (lp<rp) ) {
/*******************************found******************************/
lp ; rp __2__ ; } --
/*******************************found******************************/
if(lp<rp) __3__ ; return 0
else return 1; }
main( )
{ char s[81];
printf("Enter a string: "); scanf("%s",s);
if(fun(s)) printf("\n\"%s\" is a Palindrome.\n\n",s);
else printf("\n\"%s\" isn't a Palindrome.\n\n",s); }
93. 给定程序中,函数fun的功能是:在3×4的矩阵中找出在行上最大、在列上最小的那个元素,若没有符合条件的元素则输出相应信息。
例如,有下列矩阵:
1 2 13 4
7 8 10 6
3 5 9 7
程序执行结果为:find: a[2][2]=9
#include <stdio.h>
#define M 3
#define N 4
void fun(int (*a)[N])
{ int i=0,j,find=0,rmax,c,k;
while( (i<M) && (!find))
{ rmax=a[i][0]; c=0;
for(j=1; j<N; j )
if(rmax<a[i][j]) {
/*******************************found******************************/
rmax=a[i][j]; c= __1__ ; } i
find=1; k=0;
while(k<M && find)
/*******************************found******************************/
{ if (k!=i && a[k][c]<=rmax) find= __2__ ; 0
k ; }
if(find) printf("find: a[%d][%d]=%d\n",i,c,a[i][c]);
/*******************************found******************************/
__3__ ; i
}
if(!find) printf("not found!\n");
}
main( )
{ int x[M][N],i,j;
printf("Enter number for array:\n");
for(i=0; i<M; i )
for(j=0; j<N; j ) scanf("%d",&x[i][j]);
printf("The array:\n");
for(i=0; i<M; i )
{ for(j=0; j<N; j ) printf("%3d",x[i][j]);
printf("\n\n"); }
fun(x);
}
94. 给定程序中,函数fun的功能是:找出形参s所指字符串中出现频率最高的字母(不区分大小写),并统计出其出现的次数。
例如,形参s所指的字符串为:abcAbsmaxless,程序执行后的输出结果为:
letter ’a’:3 times
letter ’s’:3 times
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void fun(char *s)
{ int k[26]={0},n,i,max=0; char ch;
while(*s)
{ if( isalpha(*s) )
/*******************************found******************************/
{ ch=tolower(__1__); *s
n=ch-'a';
/*******************************found******************************/
k[n] = __2__ ; } 1
s ;
/*******************************found******************************/
if(max<k[n]) max= __3__ ; k[n]
}
printf("\nAfter count :\n");
for(i=0; i<26;i )
if (k[i]==max) printf("\nletter \'%c\' : %d times\n",i 'a',k[i]);
}
main( )
{ char s[81];
printf("\nEnter a string:\n\n"); gets(s);
fun(s); }


