实验十结构体和共用体.doc

上传人:m**** 文档编号:549408504 上传时间:2023-04-11 格式:DOC 页数:9 大小:2.19MB
返回 下载 相关 举报
实验十结构体和共用体.doc_第1页
第1页 / 共9页
实验十结构体和共用体.doc_第2页
第2页 / 共9页
实验十结构体和共用体.doc_第3页
第3页 / 共9页
实验十结构体和共用体.doc_第4页
第4页 / 共9页
实验十结构体和共用体.doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《实验十结构体和共用体.doc》由会员分享,可在线阅读,更多相关《实验十结构体和共用体.doc(9页珍藏版)》请在金锄头文库上搜索。

1、实验十 结构体和共用体1、 实验目的(1)掌握结构体类型变量的定义和使用;(2)掌握结构体类型数组的概念和使用;(3)掌握链表的概念,初步学会对链表进行操作;(4)掌握共用体的概念与使用。2、 实验内容编程序,然后上机调试运行。(1)有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生数据,要求打印出3门课总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课的成绩、平均分数)。所编写程序为:#include#define N 10struct studentchar num6;char name8;float score3;float avr;stuN;void

2、 main()int i,j,maxi;float sum,max,average;for(i=0;iN;i+)printf(input scores of student %d:n,i+1);printf(No.:);scanf(%s,stui.num);printf(name:);scanf(%s,stui.name);for(j=0;j3;j+)printf(score %d:,j+1);scanf(%f,&stui.scorej);average=0;max=0;maxi=0;for(i=0;iN;i+)sum=0;for(j=0;jmax)max=sum;maxi=i;average

3、/=N;printf( No. name score1 score2 score3 averagen);for(i=0;iN;i+)printf(%5s%10s,stui.num,stui.name);for(j=0;j3;j+)printf(%9.2f,stui.scorej);printf( %8.2fn,stui.avr);printf(average=%5.2fn,average);printf(The highest score is:student %s,%s.n,stumaxi.num,stumaxi.name);printf(His scores are :%6.2f,%6.2

4、f,%6.2f,average:%5.2f.n,stumaxi.score0,stumaxi.score1,stumaxi.score2,stumaxi.avr);运行结果如下:(2)13个人围成一圈,从第1个人开始顺序报号1、2、3。凡报到“3”者退出圈子,找出最后留在圈子中的人原来的序号。所编写程序为:#include#define N 13struct personint number;int nextp;linkN+1;void main()int i,count,h;for(i=1;i=N;i+)if(i=N)linki.nextp=1;elselinki.nextp=i+1;lin

5、ki.number=i;printf(n);count=0;h=N;printf(sequence that persons leave the circle :n);while(countN-1)i=0;while(i!=3)h=linkh.nextp;if(linkh.number)i+;printf(%4d,linkh.number);linkh.number=0;count+;printf(nThe last one is );for(i=1;i=N;i+)if(linki.number)printf(%3d,linki.number);printf(n);运行结果如下:(3) 建立一

6、个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。所编写程序为:#include#include#define LEN sizeof(struct student)struct studentchar num6;char name8;char sex2;int age;struct student *next;str10;void main()struct student *p,*pt,*head;int i,length,iage,flag=1;int find=0;while(flag=1)printf(input leng

7、th of list(10):);scanf(%d,&length);if(length10)flag=0;for(i=0;inext=p;pt=p;printf(No.: );scanf(%s,p-num);printf(name: );scanf(%s,p-name);printf(sex: );scanf(%s,p-sex);printf(age: );scanf(%d,&p-age);p-next=NULL;p=head;printf(n No. name sex agen);while(p!=NULL)printf(%4s%8s%6s%6dn,p-num,p-name,p-sex,p

8、-age);p=p-next;printf(input age:);scanf(%d,&iage);pt=head;p=pt;if(pt-age=iage)p=pt-next;head=pt=p;find=1;elsept=pt-next;while(pt!=NULL)if(pt-age=iage)p-next=pt-next; find=1;elsep=pt;pt=pt-next;if(!find)printf(not found %d.,iage);p=head;printf(n No. name sex agen);while(p!=NULL)printf(%4s%8s,p-num,p-

9、name);printf(%6s%6dn,p-sex,p-age);p=p-next;运行结果如下:(4)输入和运行以下程序:#includeunion dataint i2;float a;long b;char c4; void main()scanf(%d,%d,&u.i0,&u.i1);printf(i0=%d,i1=%da=%fb=%ldc0=%c,c1=%c,c2=%c,c3=%c,u.i0,u.i1,u.a,u.b,u.c0,u.c1,u.c2,u.c3);输入两个整数10000、20000赋给u.i0和u.i1。运行结果如下:将scanf语句改为: scanf(%ld,&u.b);输入60000赋给b。运行结果如下:

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号