一、程序填空题
在此程序中,通过定义学生结构体变量,存储了学生的学号、姓名和三门课的成绩。所有学生数据均以二进制方式输出到文件中。函数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__);
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);
fclose(fp);
}
void 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");
}
}
答案:(1) "rb" (2) > (3) fwrite
二、程序修改题
在此程序中,函数fun的功能是:在字符串的最前端加入n个*号,形成新串,并且覆盖原串。字符串的长度最长允许为79。
#include <stdio.h>
#include <string.h>
void fun ( char s[], int n )
{
char a[80] , *p;
int i;
/**********found***********/
s=p;
for(i=0; i<n; i++) a[i]='*';
do
{ a[i]=*p;
i++;
}
/**********found***********/
while(*p++)
a[i]=0;
strcpy(s,a);
}
void main()
{ int n; char s[80];
printf("\nEnter a string : "); gets(s);
printf("\nThe string \"%s\"\n",s);
printf("\nEnter n ( number of * ) : "); scanf("%d",&n);
fun(s,n);
printf("\nThe string after insert : \"%s\" \n" ,s);
}
答案:(1) p=s; (2) while(*p++);
三、程序设计题
在此程序中,编写函数fun,该函数的功能是:统计各年龄段的人数。N个年龄通过调用随机函数获得,并放入主函数的age数组中。要求函数把0~9岁年龄段的人数放在d[0]中,把10~19岁年龄段的人数放在d[1]中,把20~29岁年龄段的人数放在d[2]中,依此类推,把100岁(含100岁)以上年龄的人数放在d[10]中。结果在主函数中输出。
#include <stdio.h>
# define N 50
# define M 11
void fun(int *a, int *b)
{
}
double rnd()
{
static int t=29,c=217,m=1024,r=0;
r=(r*t+c)%m;
return((double)r/m);
}
void main()
{
FILE *wf;
int age[N], i,d[M];
int b[N]={32,45,15,12,86,49,97,3,44,52,17,95,63};
for(i=0; i<N; i++)
age[i]=(int)(115*rnd()); /*产生一个随机的年龄数组*/
printf("The original data :\n");
for(i=0; i<N; i++)
printf((i+1)%10==0? "%4d\n":"%4d",age[i]); /*每行输出10个数*/
printf("\n\n");
fun(age,d);
for(i=0; i<10; i++)
printf("%4d---%4d :%4d\n", i*10, i*10+9,d[i]);
printf("Over 100 : %4d\n",d[10]);
/******************************/
wf=fopen("out.dat","w");
fun(b,d);
for(i=0; i<10; i++)
fprintf(wf,"%4d---%4d :%4d\n", i*10, i*10+9,d[i]);
fprintf(wf,"Over 100 : %4d",d[10]);
fclose(wf);
/*****************************/
}
答案:
int i,j;
for(j=0;j<M;j++)
b[j]=0; /*数组b初始化为0*/
for(i=0;i<N;i++)
if(a[i]>=100)
b[10]++; /*如果年龄大于等于100,b[10]自增1*/
else
b[a[i]/10]++; /*如果年龄小于100,则将其分别统计到b[a[i]/10]中*/