资源描述:
《实验十结构体和共用体.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验十结构体和共用体预备知识:<略)实验程序:1.#includestructbb{intx。char*y。}a[]={{1,"Pascal"},{2,"Fortran"}}。voidmain(>{structbb*p=a。charc,*s。s=++p->y。//即s=++(p->y>。printf("%s",s>。c=*p++->y。//即c=*p->y,p++。printf("%c",c>。s=p->y++。//即s=p->y,y++printf("%s",s>。printf("%s",p->y>。}2.#i
2、ncludeunionee{inta。intb。}*p,s[4]。voidmain(>{intn=1,i。for(i=0。i<4。i++>{s[i].a=n。s[i].b=s[i].a+1。n+=2。}p=&s[0]。printf("%d",p->a>。printf("%d",++p->a>。}3.#include13/13structstudent{intnum。charname[12]。floatscore[3]。floatave。}。voidmain(>{structstudents[2]。int
3、i,k。for(i=0。i<2。i++>{scanf("%d",&s[i].num>。//输入结束后回车getchar(>。//消耗缓冲区字符gets(s[i].name>。//输入结束后回车scanf("%f%f%f",&s[i].score[0],&s[i].score[1],&s[i].score[2]>。b5E2RGbCAPs[i].ave=(s[i].score[0]+s[i].score[1]+s[i].score[2]>/3.0。p1EanqFDPw}k=0。for(i=1。i<2。i++>{if(s[k].ave
4、>k=i。}printf("%d%s%f",s[k].num,s[k].name,s[k].score[0]>。DXDiTa9E3dprintf("%f%f%f",s[k].score[1],s[k].score[2],s[k].ave>。RTCrpUDGiT}4.#includestructdata{intbianhao。charname[10]。}。voidmain(>{structdatab[4]={{1001,"北京"},{1002,"天津"},{1003,"上海"},{1004,"重庆"}}。5PCzVD7HxAs
5、tructdata*p。p=b。for(p=b。p13/13printf("%d%s",p->bianhao,p->name>。}5.按字母顺序排序。#include#include#includestructproduct{charname[10]。floatprice。}。voidinput(structproduct*p,intn>{charstr[10]。inti。for(i=0。i{printf("EntertheNameofNo.%d
6、:",i+1>。gets(p->name>。printf("EnterthePriceofNo.%d:",i+1>。gets(str>。p->price=atof(str>。}}voidoutput(structproducts>{printf("%10st%10.2f",s.name,s.price>。}voidsort(structproduct*p,intn>{structproducts。inti,j,k。for(i=0。i{k=i。for(j=i+1。jif(strcmp((p+j>->name,(
7、p+k>->name>>0>k=j。if(k!=i>{s=*(p+i>。*(p+i>=*(p+k>。*(p+k>=s。}}}voidmain(>{13/13structproducts[20]。inti。input(s,10>。sort(s,10>。for(i=0。i<10。i++>output(s[i]>。}6.输出最小值。#include#includevoidmain(>{int*a,*b,*c,*min。a=(int*>malloc(sizeof(int>>。b=(int*>malloc(sizeof
8、(int>>。c=(int*>malloc(sizeof(int>>。min=(int*>malloc(sizeof(int>>。sca