1、结构体、共用体和用户定义类型、文件及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空(总题数:7,分数:100.00)1.在给定程序中,函数 fun()的功能是将形参指针所指结构体数组中的 3 个元素按 num 成员进行升序排列。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。#includestdio.h#define N 8typedef structint num;int year,month,day;STU;int fun(STU *std,STU *k,int year)inti,n=0;for(i=0;iN;i+)/*found*/If( (1
2、) =year)/*found*/kn+= (2) ;/*found*/return( (3) );main()STU stdN=1,1984,2,15,2,1983,9,21,3,1984,9,1,4,1983,7,15,5,1985,9,28,6,1982,11,15,7,1982,6,22),8,1984,8,19;STU kN;int i,n,year;printf(“Enter a year:“);scanf(“%d“,n=fun(std,k,year);if(n=0)printf(“/nNo person was born in%d/n“,year);elseprintf(“/nT
3、hese persons were born in%d/n“,year);for(i=0;in;i+)printf(“%d %d-%d-%d/a“,ki.Num,ki.year,ki.month,ki.day);(分数:14.00)填空项 1:_2.给定程序通过定义并赋初值的方式,利用结构体变量存储了一名学生的学号、姓名和 3 门课的成绩。函数 fun()的功能是将该学生的各科成绩都乘以一个系数 a。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。#includestdio.htypedef structint num;char name9;float score3;ST
4、U;void show(STU tt)int i;pfintf(“%d %s:“,tt.num,tt.name);for(i=0;i3;i+)printf(“%5.1f“,tt.scorei);printf(“/n“);/*found*/void modify( (1) *ss,float a)int i;for(i=0;i3;i+)/*found*/ss- (2) *=a;main()STU std=1,“Zhanghua“,76.5,78.0,82.0);float a;printf(“/aThe original number and name and scores:/a“);show(
5、std);printf(“/aInput a number:“);scanf(“%f“,/*found*/modify( (3) ,a);printf(“/nA result of modifying:/n“);show(std);(分数:14.00)填空项 1:_3.给定程序中已建立一个带有头结点的单向链表,在 main()函数中将多次调用 fun()函数,依次输出链表尾部结点中的数据,并释放该结点,使链表缩短。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。#includestdio.h#includestdlib.h#define N 8typedef struct
6、 listint data;struct list *next;SLIST;void fun(SLIST *p)SLIST *t,*s;t=p-next;s=p;while(t-next!=NULL)s=t;/*found*/t=t- (1) ;/*found*/printf(“%d“, (2) );s-next=NULL;/*found*/free( (3) );SLIST *creatlist(int *a)SLIST*h,*p,*q;int i;h=p=(SLIST*)malloc(sizeof(SLIST);for(i=0;iN;i+)q=(SLIST*)malloc(sizeof(S
7、LIST);q-data=ai;p-next=q;p=q;p-next=0;return h;void outlist(SLIST *h)SLIST *p;p=h-next;if(p=NULL)printf(“/nThe list is NULL!/a“);elseprintf(“/nHead“);do printf(“-%d“,p-data);p=p-next;while(p!=NULL);printf(“-End/n“);main()SLIST *head;int aN=11,12,15,18,19,22,25,29;head=creatlist(a);printf(“/nOutput f
8、rom head:/n“);outlist(head);printf(“/nOutput from tail:/n“);while(head-next!=NULL)fun(head);printf(“/11/n“);printf(“/nOutput from head again:/n“);outlist(head);(分数:14.00)填空项 1:_4.给定程序中,函数 fun()的功能是在带有头结点的单向链表中,查找数据域中值为 ch 的结点。找到后通过函数值返回该结点在链表中所处的顺序号;若不存在值为 ch 的结点,函数返回 0 值。请在程序的下划线处填入正确的内容并把下划线删除,使程序
9、得出正确的结果。#includestdio.h#includestdlib.h#define N 8typedef struct listint data;struct list *next;SLIST;SLIST *creatlist(char*);void outlist(SLIST*);int fun(SLIST *h,char ch)SLIST *p;int n=0;p=h-next;/*found*/while(p!= (1) )n+;/*found*/if(p-data=ch)return (2) ;else p=p-next;return 0;main()SLIST *head;
10、int k;char ch;char aN=m,p,g,a,w,x,r,d;head=creatlist(a);outlist(head);printf(“Enter a letter:“);scanf(“%c“,/*found*/k=fun( (3) );if(k=0)printf(“/nNot found!/n“);else printf(“The sequence number is:%dn“,k);SLIST *creatlist(char *a)SLIST*h,*p,*q;int i;h=p=(SLIST*)malloc(sizeof(SLIST);for(i=0;iN;i+)q=(
11、SLIST*)malloc(sizeof(SLIST);q-data=ai;p-next=q;p=q;p-next=0;return h;void outlist(SLIST *h)SLIST *p;p=h-next;if(p=NULL)printf(“/nThe list is NULL!/n“);elseprintf(“haHead“);doprintf(“-%c“,p-data);p=p-next;while(p!=NULL);printf(“-End/n“);(分数:14.00)填空项 1:_5.给定程序的功能是:从键盘输入若干行文本(每行不超过 80 个字符),写到文件 myfile
12、4.txt 中(用-1作为字符串输入结束的标志),然后将文件的内容读出显示在屏幕上。文件的读/写分别由自定义函数ReadText()和 WriteText()实现。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。#includestdio.h#includestring.h#includestdlib.hvoid WriteText(FILE*);void ReadText(FILE*);main()FILE *fp;if(fp=fopen(“myfile4.txt“,“w“)=NULL)printf(“open fail!/n“);exit(0);WriteText(f
13、p);fclose(fp);if(fp=fopen(“myfile4.txt“,“r“)=NULL)printf(“open fail!/n“);exit(0);ReadText(fp);fclose(fp);/*found*/void WriteText(FILE (1) )char str81;printf(“/aEnter string with-1 to end:/n“);gets(str);while(strcmp(str,“-1“)!=0)/*found*/fputs( (2) ,fw);fputs(“/n“,fw);gets(str);void ReadText(FILE *fr
14、)char str81;printf(“/nRead file and output to screen:/n“);fgets(str,81,fr);while(!feof(fr)/*found*/printf(“%s“, (3) );fgets(str,81,fr);(分数:14.00)填空项 1:_6.给定程序中,函数 fun()的功能是将形参给定的字符串、整数、浮点数写到文本文件中,再用字符方式从该文本文件中逐个读入并显示在终端屏幕上。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。#include stdio.hvoid fun(char *s,int a,dou
15、ble f)/*found*/(1) fp;char ch;fp=fopen(“file1.txt“,“w“);fprintf(fp,“%s%d%f/n“,s,a,f);fclose(fp);fp=fopen(“file1.txt“,“r“);printf(“/nThe result:/n/n“);ch=fgetc(fp);/*found*/while(!feof( (2) )/*found*/putchar( (3) );ch=fgetc(fp);)putchar(/n);fclose(fp);main()char a10=“Hello!“;int b=12345;double c=98.7
16、6;fun(a,b,c);(分数:14.00)填空项 1:_7.程序通过定义学生结构体变量,存储了学生的学号、姓名和 3 门课的成绩。所有学生数据均以二进制方式输出到文件中。函数 fun()的功能是从形参 filename 所指的文件中读入学生数据,并按照学号从小到大排序,再用二进制方式把排序后的学生数据输出到 filename 所指的文件中,覆盖原来的文件内容。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。#includestdio.h#define N 5typedef struct studentlong sno;char name10;float score3;
17、STU;void fun(char *filename)FILE *fp;int i,j;STU sN,t;/*found*/fp=fopen(filename, (1) );fread(s,sizeof(STU),N,fp);fclose(fp);for(i=0;iN-1;i+)for(j=i+1;jN;j+)/*found*/if(si.sno (2) sj.sno)t=si;si=sj;sj=t;)fp=fopen(filename,“wb“);/*found*/(3) (s,sizeof(STU),N,fp);/*二进制输出*/fclose(fp);main()STUtN=10005,
18、“ZhangSan“,95,80,88,10003,“LiSi“,85,70,78,10002,“CaoKai“,75,60,88,10004,“FangFang“,90,82,87,10001,“MaChao“,91,92,77,SSN;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;jN;j+)printf(“/nNo:%ld Name:%-8s Scores:“,tj.sno,tj
19、name);for(i=0;i3;i+)printf(“%6.2f“,tjscorei);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;jN;j+)printf(“/nNo:%ld Name:%-8s Scores:“,ssj.sno,ssj.name);for(i=0;i3;i+)printf(“%6.2f“,ssj.scorei);print
20、f(“/n“);(分数:16.00)填空项 1:_结构体、共用体和用户定义类型、文件答案解析(总分:100.00,做题时间:90 分钟)一、程序填空(总题数:7,分数:100.00)1.在给定程序中,函数 fun()的功能是将形参指针所指结构体数组中的 3 个元素按 num 成员进行升序排列。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。#includestdio.h#define N 8typedef structint num;int year,month,day;STU;int fun(STU *std,STU *k,int year)inti,n=0;for(i
21、=0;iN;i+)/*found*/If( (1) =year)/*found*/kn+= (2) ;/*found*/return( (3) );main()STU stdN=1,1984,2,15,2,1983,9,21,3,1984,9,1,4,1983,7,15,5,1985,9,28,6,1982,11,15,7,1982,6,22),8,1984,8,19;STU kN;int i,n,year;printf(“Enter a year:“);scanf(“%d“,n=fun(std,k,year);if(n=0)printf(“/nNo person was born in%d/
22、n“,year);elseprintf(“/nThese persons were born in%d/n“,year);for(i=0;in;i+)printf(“%d %d-%d-%d/a“,ki.Num,ki.year,ki.month,ki.day);(分数:14.00)填空项 1:_ (正确答案:(1)stdi.year (2)stdi (3)n)解析:解析 fun()函数的功能是在结构体数组 std 中找出指定出生年份的人员,将其数据放在形参 k 所指的数组中。出生年份是整型变量,可以直接比较。利用循环搜索整个 std 数纽,逐个找到满足条件的STU 结构体。第一空:比较结构体的出
23、生年份是否等于 year,std 是结构体指针,指向结构体数组的首地址,因此访问一个结构体的 year 成员是 stdi.year,第一空处应为“if(stdi.year=year)”。第二空:这里是将找到的数据放在形参 k 所指的数组中,k 是指针变量,指向结构体数组的首地址,stdi是结构体的地址,因此等号左边也应该是地址,故第二空处应为“kn+=stdi;”。第三空:fun()函数的返回值是满足指定条件的人数 n,故第三空处应为“return(n);”。考点 结构体数组,循环结构。2.给定程序通过定义并赋初值的方式,利用结构体变量存储了一名学生的学号、姓名和 3 门课的成绩。函数 fun
24、()的功能是将该学生的各科成绩都乘以一个系数 a。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。#includestdio.htypedef structint num;char name9;float score3;STU;void show(STU tt)int i;pfintf(“%d %s:“,tt.num,tt.name);for(i=0;i3;i+)printf(“%5.1f“,tt.scorei);printf(“/n“);/*found*/void modify( (1) *ss,float a)int i;for(i=0;i3;i+)/*found*/
25、ss- (2) *=a;main()STU std=1,“Zhanghua“,76.5,78.0,82.0);float a;printf(“/aThe original number and name and scores:/a“);show(std);printf(“/aInput a number:“);scanf(“%f“,/*found*/modify( (3) ,a);printf(“/nA result of modifying:/n“);show(std);(分数:14.00)填空项 1:_ (正确答案:(1)STU (2)scorei (3)”。第三空:modifly 函数的
26、第一个参数是指针,故调用该函数时第一个参数应该是变量 std 的地址“struct list *next;SLIST;void fun(SLIST *p)SLIST *t,*s;t=p-next;s=p;while(t-next!=NULL)s=t;/*found*/t=t- (1) ;/*found*/printf(“%d“, (2) );s-next=NULL;/*found*/free( (3) );SLIST *creatlist(int *a)SLIST*h,*p,*q;int i;h=p=(SLIST*)malloc(sizeof(SLIST);for(i=0;iN;i+)q=(S
27、LIST*)malloc(sizeof(SLIST);q-data=ai;p-next=q;p=q;p-next=0;return h;void outlist(SLIST *h)SLIST *p;p=h-next;if(p=NULL)printf(“/nThe list is NULL!/a“);elseprintf(“/nHead“);do printf(“-%d“,p-data);p=p-next;while(p!=NULL);printf(“-End/n“);main()SLIST *head;int aN=11,12,15,18,19,22,25,29;head=creatlist(
28、a);printf(“/nOutput from head:/n“);outlist(head);printf(“/nOutput from tail:/n“);while(head-next!=NULL)fun(head);printf(“/11/n“);printf(“/nOutput from head again:/n“);outlist(head);(分数:14.00)填空项 1:_ (正确答案:(1)next (2)t-data (3)t)解析:解析 fun()函数的功能是输出链表尾部结点中的数据,并释放该结点,使链表缩短。由于链表是单向的,找到链表尾部结点需要利用循环从头结点开始
29、查找,直至找到最后一个。找到之后输出尾部结点的数据,然后删除尾部结点。第一空:fun()函数中循环“while(t-next!=NULL)”的目的是找到尾部结点,利用结点变量 s 和 t,s指向当前结点,t 不断指向下一个结点,因此第一空处应该是“t=t-next;”。第二空:这里是输出尾部结点中的数据,已经利用循环找到了尾部结点 t,t 的数据是 t-data,因此第二空处应该为“printf(“%d“,t-data);”。第三空:输出尾部结点数据之后删除尾部结点,可使用 free();又因为尾部结点是 t,因此第三空处应该为“free(t);”。考点 链表,指针。4.给定程序中,函数 fu
30、n()的功能是在带有头结点的单向链表中,查找数据域中值为 ch 的结点。找到后通过函数值返回该结点在链表中所处的顺序号;若不存在值为 ch 的结点,函数返回 0 值。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。#includestdio.h#includestdlib.h#define N 8typedef struct listint data;struct list *next;SLIST;SLIST *creatlist(char*);void outlist(SLIST*);int fun(SLIST *h,char ch)SLIST *p;int n=0;p
31、=h-next;/*found*/while(p!= (1) )n+;/*found*/if(p-data=ch)return (2) ;else p=p-next;return 0;main()SLIST *head;int k;char ch;char aN=m,p,g,a,w,x,r,d;head=creatlist(a);outlist(head);printf(“Enter a letter:“);scanf(“%c“,/*found*/k=fun( (3) );if(k=0)printf(“/nNot found!/n“);else printf(“The sequence num
32、ber is:%dn“,k);SLIST *creatlist(char *a)SLIST*h,*p,*q;int i;h=p=(SLIST*)malloc(sizeof(SLIST);for(i=0;iN;i+)q=(SLIST*)malloc(sizeof(SLIST);q-data=ai;p-next=q;p=q;p-next=0;return h;void outlist(SLIST *h)SLIST *p;p=h-next;if(p=NULL)printf(“/nThe list is NULL!/n“);elseprintf(“haHead“);doprintf(“-%c“,p-d
33、ata);p=p-next;while(p!=NULL);printf(“-End/n“);(分数:14.00)填空项 1:_ (正确答案:(1)0 或 NULL (2)n (3)head,ch)解析:解析 函数 fun()的功能是查找数据域中值为 ch 的结点。在实现方式上,可采用循环来搜索链表中的每一个结点,比较结点的数据域是否等于 ch。第一空:这里是补充循环结束条件。链表是单向有限的,最后一个结点的指针域指向 NULL,因此可以用这个作为循环结束条件。在 p 指向的结点的指针域不是 NULL 时,表明还有下一个结点,因此第一空处应该是“while(p!=NULL)”。第二空:在循环中
34、n 用来记录顺序号。如果“if(p-data=ch)”成立,则表明已经找到数据域中值为ch 的结点,返回顺序号,即“return n;”。第三空:fun()函数的调用形式是“fun(SLIST *h,char ch)”,第一个参数是头结点地址,第二个参数是需要寻找的 ch 值,因此第三空处应该是“k=fun(head,ch);”。考点 链表,指针。5.给定程序的功能是:从键盘输入若干行文本(每行不超过 80 个字符),写到文件 myfile4.txt 中(用-1作为字符串输入结束的标志),然后将文件的内容读出显示在屏幕上。文件的读/写分别由自定义函数ReadText()和 WriteText(
35、)实现。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。#includestdio.h#includestring.h#includestdlib.hvoid WriteText(FILE*);void ReadText(FILE*);main()FILE *fp;if(fp=fopen(“myfile4.txt“,“w“)=NULL)printf(“open fail!/n“);exit(0);WriteText(fp);fclose(fp);if(fp=fopen(“myfile4.txt“,“r“)=NULL)printf(“open fail!/n“);exit(
36、0);ReadText(fp);fclose(fp);/*found*/void WriteText(FILE (1) )char str81;printf(“/aEnter string with-1 to end:/n“);gets(str);while(strcmp(str,“-1“)!=0)/*found*/fputs( (2) ,fw);fputs(“/n“,fw);gets(str);void ReadText(FILE *fr)char str81;printf(“/nRead file and output to screen:/n“);fgets(str,81,fr);whi
37、le(!feof(fr)/*found*/printf(“%s“, (3) );fgets(str,81,fr);(分数:14.00)填空项 1:_ (正确答案:(1)*fw (2)str (3)str)解析:解析 本题要求把一段文本写到文件 myfile4.txt 中(用-1 作为字符串输入结束的标志),然后将文件的内容读出显示在屏幕上。在此主要考查的是文件操作以及 flouts()和 fgets()函数的应用。第一空:此处是补充函数参数名,参数类型已知是 FILE 文什指针,fputs(“/n“,fw)是将“/n“写入文件,可知 fw 是文件指针名,因此第一空处应该是“fw”。第二空:wh
38、ile(strcmp(str,“-1“)!=0)循环的结束标志是 str 字符串等于“-1“时退出循环,如果 str 不等于“-1“,执行循环体内容:用 fputs()函数将 str 字符串写入文件。fputs()函数的调用格式是“fputs(s,fp);”,其中 s 是指向字符串的指针或字符数组名,fp 是指向将要被写入的文件的文件指针,因此第二空处应该是“str”。第三空:while(!feof(fr)循环的作用是不断读取文件直至到达文件尾,每次最多读 81 个,将读到的数据放在 str 字符串里,并用 printf()输出到屏幕,因此第三空处应该是“str”。考点 文件操作,指针。6.给
39、定程序中,函数 fun()的功能是将形参给定的字符串、整数、浮点数写到文本文件中,再用字符方式从该文本文件中逐个读入并显示在终端屏幕上。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。#include stdio.hvoid fun(char *s,int a,double f)/*found*/(1) fp;char ch;fp=fopen(“file1.txt“,“w“);fprintf(fp,“%s%d%f/n“,s,a,f);fclose(fp);fp=fopen(“file1.txt“,“r“);printf(“/nThe result:/n/n“);ch=fg
40、etc(fp);/*found*/while(!feof( (2) )/*found*/putchar( (3) );ch=fgetc(fp);)putchar(/n);fclose(fp);main()char a10=“Hello!“;int b=12345;double c=98.76;fun(a,b,c);(分数:14.00)填空项 1:_ (正确答案:(1)FILE* (2)fp (3)ch)解析:解析 函数 fun 的功能是将形参给定的字符串、整数、浮点数写到文本文件中,再用字符方式从该文本文件中逐个读入并显示在终端屏幕上。本题主要考查的是文件的读/写。第一空:由“fp=fopen
41、(“file1.txt“,“w“);”可知,fp 是文件型指针,故第一空处的变量 fp 定义类型为“FILE*”。第二空:“while(!feof( (2) )”是在没有到达文件末尾的时候执行循环体内容,文件状态检测函数feof()的一般调用格式是“feof(fp);”,其中 fp 是文件型指针,故第二空处应为“fp”。第三空:题干要求从文件中逐个读入数据并显示在终端屏幕上,第三空处是将从文件中读取的字符 ch 显示在屏幕上,故第三空处应为“ch”。考点 文件操作,函数功能。7.程序通过定义学生结构体变量,存储了学生的学号、姓名和 3 门课的成绩。所有学生数据均以二进制方式输出到文件中。函数
42、fun()的功能是从形参 filename 所指的文件中读入学生数据,并按照学号从小到大排序,再用二进制方式把排序后的学生数据输出到 filename 所指的文件中,覆盖原来的文件内容。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。#includestdio.h#define N 5typedef struct studentlong sno;char name10;float score3;STU;void fun(char *filename)FILE *fp;int i,j;STU sN,t;/*found*/fp=fopen(filename, (1) );fr
43、ead(s,sizeof(STU),N,fp);fclose(fp);for(i=0;iN-1;i+)for(j=i+1;jN;j+)/*found*/if(si.sno (2) sj.sno)t=si;si=sj;sj=t;)fp=fopen(filename,“wb“);/*found*/(3) (s,sizeof(STU),N,fp);/*二进制输出*/fclose(fp);main()STUtN=10005,“ZhangSan“,95,80,88,10003,“LiSi“,85,70,78,10002,“CaoKai“,75,60,88,10004,“FangFang“,90,82,8
44、7,10001,“MaChao“,91,92,77,SSN;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;jN;j+)printf(“/nNo:%ld Name:%-8s Scores:“,tj.sno,tjname);for(i=0;i3;i+)printf(“%6.2f“,tjscorei);printf(“/n“);fun(“student.dat“);printf(“/n/nT
45、he data after sorting:/n/n“);fp=fopen(“student.dat“,“rb“);fread(ss,sizeof(STU),5,fp);fclose(fp);for(j=0;jN;j+)printf(“/nNo:%ld Name:%-8s Scores:“,ssj.sno,ssj.name);for(i=0;i3;i+)printf(“%6.2f“,ssj.scorei);printf(“/n“);(分数:16.00)填空项 1:_ (正确答案:(1)“rb“ (2) (3)fwrite)解析:解析 本题中函数 fun()的功能是从形参 filename 所指的文件中读入学生数据,并按照学号从小到大排序,再用二进制方式把排序后的学生数据输出到 filename 所指的文件中,覆盖原来的文件内容。第一空:根据题干要求,“fp=fopen(filena