C语言课本上第8章结构体例题源码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

-1-/*为减轻输入工作量,下面给出课本上第8章结构体例题代码,复制到VC6.0中运行即可*//*p187【例8.1】对结构变量进行初始化,并输出各成员值。*/#includestdio.hstructscore{floatchinese;floatmath;floatenglish;};structstudent{intnumber;charname[9];charsex;intage;charaddress[30];structscoreachie;/*achie是structscore类型*/};main(){structstudentNa={10001,wanli,'M',19,zhongshanroad,78.0,67.0,90.0};structstudentNb={10002,lifang,'F',18,huajinroad,80.0,77.0,86.0};printf(%d,%s,%c,%d,%s,%.1f,%.1f,%.1f\n,Na.number,Na.name,Na.sex,Na.age,Na.address,Na.achie.chinese,Na.achie.math,Na.achie.english);printf(%d,%s,%c,%d,%s,%.1f,%.1f,%.1f\n,Nb.number,Nb.name,Nb.sex,Nb.age,Nb.address,Nb.achie.chinese,Nb.achie.math,Nb.achie.english);}/*p188【例8.2】设某组有4个人,填写如下的登记表,除姓名、学号外,还有三科成绩,编程实现对表格的计算,求解出每个人的三科平均成绩,求出四个学生的单科平均,并按平均成绩由高分到低分输出。*/#includestdio.hstructstu{charname[20];longnumber;floatscore[4];-2-};main(){voidaver();voidorder();voidoutput();voidout_row();structstustud[4]={{liping,1,67.0,72.0,65.0},{yaoming,2,77.0,73.0,80.0},{liudong,3,81.0,79.0,90.0},{zhangwei,4,68.0,78.0,89.0}};/*定义结构体数组并初始化*/aver(stud,4);order(stud,4);output(stud,4);out_row(stud,4);}voidaver(structstuarr[],intn){inti,j;for(i=0;in;i++){arr[i].score[3]=0;for(j=0;j3;j++)arr[i].score[3]+=arr[i].score[j];arr[i].score[3]/=3;}}voidorder(structstuarr[],intn){structstutemp;inti,j;for(i=0;in-1;i++)for(j=0;jn-1-i;j++)if(arr[j].score[3]arr[j+1].score[3]){temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}voidoutput(structstuarr[],intn){inti,j;printf(******************************TABLE*****************************\n);printf(----------------------------------------------------------------\n);printf(|%10s|%8s|%7s|%7s|%7s|%7s|\n,NAME,NUMBER,ENGLISH,MATH,PHYSICS,AVERAGE);-3-printf(----------------------------------------------------------------\n);for(i=0;in;i++){printf(|%10s|%8ld,arr[i].name,arr[i].number);for(j=0;j4;j++)printf(|%7.2f,arr[i].score[j]);printf(\n);printf(------------------------------------------------------------\n);}}voidout_row(structstuarr[],intn){floatrow[4]={0};inti,j;for(i=0;i4;i++){for(j=0;jn;j++)row[i]=row[i]+arr[j].score[i];row[i]=row[i]/n;}printf(|%19c|,'');for(i=0;i4;i++)printf(%7.2f|,row[i]);printf(\n------------------------------------------------------------\n);}//p192【例8.3】指向结构体变量的指针变量的使用。#includestdio.hvoidmain(){structtest{inti,j;charm,n;};structtesta,b;structtest*pa,*pb;pa=&a;pb=&b;-4-pa-i=pa-j=10;pa-m=pa-n='H';printf(%d,%d,%c,%c\n,pa-i,pa-j,pa-m,pa-n);b=a;/*将结构体变量a赋给另一个具有相同结构的结构体变量b*/printf(%d,%d,%c,%c\n,pb-i,pb-j,pb-m,pb-n);printf(%d,%d,%c,%c\n,++pa-i,pa-j--,--pa-m,pa-n++);printf(%d,%d,%c,%c\n,pb-i,pb-j,pb-m,pb-n);pb=pa;/*将指针pb也指向结构体变量a*/printf(%d,%d,%c,%c\n,++pa-i,pa-j--,--pa-m,pa-n++);printf(%d,%d,%c,%c\n,pb-i,pb-j,pb-m,pb-n);}//p193【例8.4】指向结构体数组的指针变量的使用。#includestdio.hstructdata/*定义结构体类型*/{intyear,month,day;};structstu/*定义结构体类型*/{charname[20];longnum;structdatabirthday;};voidmain(){inti;structstu*p,student[4]={{zhouping,1,1983,7,2},{wangling,2,1984,3,24},{hubo,3,1982,5,16},{huaqiang,4,1982,7,21}};/*定义结构体数组并初始化*/p=student;/*将数组的首地址赋值给指针p,p指向了一维数组student*/printf(\n1----Outputname,number,year,month,day\n);for(i=0;i4;i++)/*采用指针法输出数组元素的各成员*/printf(%16s%7ld%8d/%d/%d\n,(p+i)-name,(p+i)-num,(p+i)-birthday.year,(p+i)-birthday.month,(p+i)-birthday.day);printf(\n2----Outputname,number,year,month,day\n);-5-for(i=0;i4;i++,p++)/*采用指针法输出数组元素的各成员*/printf(%16s%7ld%8d/%d/%d\n,p-name,p-num,p-birthday.year,p-birthday.month,p-birthday.day);printf(\n3-----Outputname,number,year,month,day\n);for(i=0;i4;i++)/*采用地址法输出数组元素的各成员*/printf(%16s%7ld%8d/%d/%d\n,(student+i)-name,(student+i)-num,(student+i)-birthday.year,(student+i)-birthday.month,(student+i)-birthday.day);p=student;printf(\n4-----Outputname,number,year,month,day\n);for(i=0;i4;i++)/*采用指针的数组描述法输出数组元素的各成员*/printf(%16s%7ld%8d/%d/%d\n,p[i].name,p[i].num,p[i].birthday.year,p[i].birthday.month,p[i].birthday.day);}//p194【例8.5】将【例8.2】改用结构体指针变量作函数参数实现。#includestdio.hstructstu{charname[20];longnumber;floatscore[4];};main(){voidinput();/*函数声明*/voidaver();voidorder();voidoutput();voidout_row();structstustud[4]={{liping,1,67.0,72.0,65.0},{yaoming,2,77.0,73.0,80.0},{liudong,3,81.0,79.0,90.0},{zhangwei,4,68.0,78.0,89.0}};/*定义结构体数组并初始化*/floatrow[3];aver(stud,4);order(stud,4);output(stud,4);out_row(stud,4);}-6-voidaver(structstu*ptr,intn)/*形式参数中使用了指向结构体变量的指向变量*/{inti,j;for(i=0;in;i++){(ptr+i)-score[3]=0;/*采用结构体指针变量的表达方式-*/for(j=0;j3;j++)(ptr+i)-score[3]+=(ptr+i)-score[j];(ptr+i)-score[3]/=3;}}voidorder(structstu*ptr,intn){structstutemp;inti,j,x,y;for(i=0;in-1;i++)for(j=0;jn-1-i;j++)if((ptr+j)-score[3](ptr+j+1)-score[3]){temp=*(ptr+j);*(ptr+j)=*(ptr+j+1);*(ptr+j+1)=temp;}}voidoutput(structstu*ptr,intn){inti,j;printf(***********************TABLE************************\n);printf(----------------------------------------------------\n);printf(|%10s|%8s|%7s|%7s|%7s|%7s|\n,Name,Number,English,mathema,physics,average);printf(----------------------------------------------------\n);for(i=0;in;i++){printf(|%10s|%8ld|,(ptr+i)-name

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功