付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验十结构体和共用体预备知识:(略)实验程序:1.#includestructbb(intx;char*y;a=1,Pascal,2,Fortran;voidmain()即s=+(p-y);即c=*p-y,p+;即s=p-y,y+structbb*p=a;charc,*s;s=+p-y;/printf(%sn”,s);c=*p+-y;/printf(%cn,c);s=p-y+;/printf(%sn,s);printf(%sn,p-y);2#includeunioneeinta;intb;*p,s4;voidmain()intn=1,i;for(i=0;ia);printf(%dn,+p-a)
2、;3.#includestructstudent(intnum;charname12;floatscore3;floatave;voidmain()(structstudents2;inti,k;for(i=0;i2;i+)(scanf(%d”,&si.num);/输入结束后回车getchar();/消耗缓冲区字符gets();/输入结束后回车scanf(%f%f%f,&si.score0,&si.score1,&si.score2);si.ave=(si.score0+si.score1+si.score2)/3.0;k=0;for(i=1;i2;i+)(if(sk.avesi
3、.ave)k=i;printf(%d%s%f,sk.num,,sk.score0);printf(%f%f%fn”,sk.score1,sk.score2,sk.ave);4.#includestructdata(intbianhao;charname10;voidmain()(structdatab4=(1001,北京,(1002,天津,(1003,上海,(1004,重庆;structdata*p;p=b;5. for(p=b;pbianhao,p-name);按字母顺序排序。#include#include#includestructproductcharname10;flo
4、atprice;voidinput(structproduct*p,intn)charstr10;inti;for(i=0;iname);printf(EnterthePriceofNo.%d:,i+1);gets(str);p-price=atof(str);voidoutput(structproducts)printf(%10st%10.2fn,,s.price);voidsort(structproduct*p,intn)structproducts;inti,j,k;for(i=0;in-1;i+)k=i;for(j=i+1;jname,(p+k)-name)0)k=j;
5、if(k!=i)s=*(p+i);*(p+i)=*(p+k);*(p+k)=s;voidmain()structproducts20;inti;input(s,10);sort(s,10);6. for(i=0;i10;i+)output(si);输出最小值。#include#includevoidmain()(int*a,*b,*c,*min;a=(int*)malloc(sizeof(int);b=(int*)malloc(sizeof(int);c=(int*)malloc(sizeof(int);min=(int*)malloc(sizeof(int);scanf(%d%d%d”,a,
6、b,c);printf(输入的3个数是:%d%d%dn,*a,*b,*c);*min=*a;if(*a*b)*min=*b;if(*min*c)*min=*c;printf(它们的最小值是:%dn,*min);7. free(a);free(b);free(c);free(min);对原题略着更改:有30个学生,每个学生的数据包括学号、姓名和3门课的成绩,从键盘输入每个学生的数据计算:每个学生的平均成绩。计算30个学生每门课程的平均分。按学生平均分从代到高次序打印出每个学生的各课成绩、3门课的平均成绩。输出每门课程的平均分。要求用input函数输入,average1函数求每个学生3门课的平均分
7、,average2函数求30个学生每门课程的平均分,sort函数按学生平均分排序,output函数输出总成绩。#include#include#include#include#defineFsizeof(student)typedefstructscores(intenglish;intmath;intc_language;intall;TP;typedefstructstudents(charsid15;charname15;TPscore;structstudents*next;student;student*input()(student*head,*p1,*p2;intn=0;char
8、ch;/clrscr();head=(student*)malloc(F);head-next=NULL;do(n+;printf(nnPleaseinput%dstudentmessage:nn,n);printf(t%dstudentsid:,n);p1=(student*)malloc(F);p1-next=NULL;scanf(%s”,p1-sid);printf(nt%dstudentname:,n);scanf(%s,p1-name);printf(nt%dstudentscores(englesh,math,c_language):,n);scanf(%d,%d,%d”,&p1-
9、score.english,&p1-score.math,&p1-score.c_language);p1-score.all=p1-score.english+p1-score.math+p1-score.c_language;if(n=1)(head-next=p1;p2=p1;else(p2-next=p1;p2=p1;printf(nntttContinueorback(pressy/n):);ch=getchar();while(ch=y|ch=Y);returnhead;voidaverage1(student*head)(student*p;intj;/clrscr();p=he
10、ad-next;while(p)(j=p-score.all/3;printf(nnname:%staverage:%d,p-name,j);p=p-next;printf(nnnPressenykeyreturn.);getchar();voidaverage2(student*head)(student*p;intn=0,temp1=0,temp2=0,temp3=0;p=head-next;while(p)(temp1+=p-score.english;temp2+=p-score.math;temp3+=p-score.c_language;p=p-next;n+;printf(nna
11、verageenglishis:%dnaveragemathis:%dnaveragec_languageis:%dt”,temp1/n,temp2/n,temp3/n);student*sort(student*head)(student*head1,*p,*q,*r;inttemp1=0,temp2=0,temp3=0,temp4;chars15,n15;head1=head;for(p=head1-next;p-next!=NULL;p=p-next)(r=p;for(q=p-next;q;q=q-next)if(q-score.allr-score.all)r=q;if(r!=p)(s
12、trcpy(s,p-sid);strcpy(n,p-name);temp1=p-score.english;temp2=p-score.math;temp3=p-score.c_language;temp4=p-score.all;strcpy(p-sid,r-sid);strcpy(p-name,r-name);p-score.english=r-score.english;p-score.math=r-score.math;p-score.c_language=r-score.c_language;p-score.all=r-score.all;strcpy(r-sid,s);strcpy
13、(r-name,n);r-score.english=temp1;r-score.math=temp2;r-score.c_language=temp3;r-score.all=temp4;returnheadl;voidoutput(student*head)(student*head2,*p;inti=1;/clrscr();head2=sort(head);for(p=head2-next;p!=NULL;p=p-next)printf(nnname:%stsid:%stenglish:%dtmath:%dtc_language:%dtaverage:%dtmingci:%d”,p-na
14、me,p-sid,p-score.english,p-score.math,p-score.c_language,p-score.all/3,i+);average2(head);printf(nnnttPressenykeyback.);getchar();voidmain()(student*head,*p1,*p2;inti=0,j=1;head=input();do(/clrscr();printf(nn(1):average1.nn(2):average2.nn(3):sort.nn(4):output.nnnPleasechoose:);scanf(%d,&i);switch(i)
15、(case1:average1(head);break;case2:/clrscr();average2(head);printf(nnnPressenykeyretuen.);getchar();break;case3:/clrscr();p1=sort(head);for(p2=p1-next;p2!=NULL;p2=p2-next)printf(nttname:%stmingci:%d,p2-name,j+);printf(nnnPressenykeyback.);getchar();break;case4:output(head);break;default:printf(nYourc
16、hooseisnotright.);break;while(i!=-1);对原题略着更改:建立一个链表,每一个结点包括的成员为学生学号、平均成绩。用malloc函数开辟新结点。要求链表包括8个结点,从键盘输入结点的有效数据。要求用函数create来建立链表。实现下列操作:新增加一个学生的数据。这个新结点要求按学号顺序插入。编写一个函数insert来插入结点。删除第五个结点,并从内存中释放。程序中要求验证删除的结点确已释放。查找特定学生的信息。将链表结点数据输出到屏幕上。#include#include#include#include#defineFsizeof(stu)typedefstruc
17、tstudentintsid;intaverage;structstudent*next;stu;stu*head;stu*create()stu*p1,*p2;intn=0;charch;head=(stu*)malloc(F);head-next=NULL;don+;printf(nnPleaseinput%dstudentmessage:nn,n);printf(t%dstudentsid:,n);p1=(stu*)malloc(F);p1-next=NULL;scanf(%d”,&p1-sid);printf(nt%dstudentaverage:,n);scanf(%d”,&p1-a
18、verage);if(n=1)head-next=p1;p2=p1;elsep2-next=p1;p2=p1;printf(nntttContinueorback(pressy/n):);ch=getch();while(ch=y|ch=Y);returnhead;stu*select(stu*head,intx)stu*s;s=head-next;while(s)(if(s-sid=x)break;s=s-next;returns;stu*insert(stu*head,intx,inty)(stu*p,*r;/,*q;/clrscr();p=head-next;r=(stu*)malloc
19、(sizeof(stu);r-sid=x;r-average=y;if(p=NULL)/*如果插入空表*/(p=r;r-next=NULL;printf(ninsertsuccess!);else(while(xp-sid)/*找到插入的位置,按学号大小。(找到位置或者到了表尾都会跳出循环)*/(if(p-next=NULL)break;p=p-next;if(xsid)/*插到中间位置*/(r-sid=p-sid;r-average=p-average;p-sid=x;p-average=y;r-next=p-next;p-next=r;printf(ninsertsuccess!);els
20、eif(x=p-sid)/*学号不能相同*/printf(nError-yourinputthissamesid.);else/*插到末尾*/(p-next=r;r-next=NULL;printf(ninsertsuccess!);returnhead;stu*get(stu*head,intn)/*得到位置为n的结点的指针*/(stu*p;inti;p=head-next;if(n=0)returnhead;else(for(i=1;inext;returnp;stu*delete(stu*head,intsid)(stu*p,*q;inttemp=0,i=0;p=head-next;if
21、(!p)(printf(nlistisempty.pressenykeyback.”);getch();returnhead;/*表空*/else(while(p)/*查找学号为sid的结点的指针*/(i+;/*标记学号为sid的结点的位置*/if(p-sid=sid)(temp=1;break;/*temp=1标记找到了*/p=p-next;if(temp=1)/*如果有学号为sid的结点*/(q=get(head,i-1);/*得到sid的前一个结点的指针*/q-next=p-next;free(p);printf(nndeletesucess!);returnhead;else/*没有找
22、到*/(printf(nnNOthisdata.n);returnhead;voidprint(stu*head)(stu*p;p=head-next;if(!p)(printf(nlistisempty.pressenykeyback.);getch();while(p)(printf(n%d:t%d”,p-sid,p-average);p=p-next;voidmain()(stu*p1;/*p2;charch1;intn,i=0,j=0;head=create();do(/clrscr();printf(n1.insert.);printf(n2.select.);printf(n3.delect.);printf(n4.printlist.);printf(n5.EXIT);printf(nchoice(1-5);ch1=getch();switch(ch1)(case1:(/clrscr();printf(npleaseinputinsertsid.andaverage(like1,1):);scanf(%d,%d,&i,&j);head=insert(head,i,j);printf(nnnPressenykeyback.);getch();break;case2:(/clrscr();printf(n
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025《齐桓晋文之事》中孟子思想的时代局限性课件
- 铁路线下考试题目及答案
- 2025年临床执业医师《内科学》专项训练测试
- 医保报销材料审核规范考核试题及答案
- 脓肿切开引流试题及答案
- 医疗纠纷应急处置试题及答案
- 医疗投诉季度分析报告制度
- 192红色喜庆卡通小龙背景的新年愿望模板
- 关键部位和工序检查确认制度培训
- 2025《谏太宗十思疏》国家治理的平衡课件
- 2025年高中英语教师资格证考试真题解析及答案
- 2026中考模拟测试试卷及答案(含完整听力音频、完整听力材料)
- 2025年山东省济南市中考化学试题(含答案)
- 机械车间安全隐患排查总结报告
- 薪资核定及管理办法
- 医院运营数据统计分析
- 足球三级裁判试题及答案
- 徐州地铁考试题库及答案
- 房屋地基出租协议书
- 危重新生儿转运规范及流程
- 《高血压诊断与治疗》课件
评论
0/150
提交评论