【计算机类职业资格】结构体、共用体和用户定义类型、文件及答案解析.doc
《【计算机类职业资格】结构体、共用体和用户定义类型、文件及答案解析.doc》由会员分享,可在线阅读,更多相关《【计算机类职业资格】结构体、共用体和用户定义类型、文件及答案解析.doc(16页珍藏版)》请在麦多课文档分享上搜索。
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 结构体。第一空:比较结构体的出
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
5000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 职业资格 结构 共用 用户 定义 类型 文件 答案 解析 DOC
