实验十 结构体和共用体.doc_第1页
实验十 结构体和共用体.doc_第2页
实验十 结构体和共用体.doc_第3页
实验十 结构体和共用体.doc_第4页
实验十 结构体和共用体.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验十 结构体和共用体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 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,);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/=N;printf( No. name score1 score2 score3 averagen);for(i=0;iN;i+)printf(%5s%10s,stui.num,);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,);printf(His scores are :%6.2f,%6.2f,%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;linki.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) 建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。所编写程序为:#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 length 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-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-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=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论