结构体与共用体综合测试

上传人:工**** 文档编号:431770380 上传时间:2022-12-02 格式:DOC 页数:12 大小:62KB
返回 下载 相关 举报
结构体与共用体综合测试_第1页
第1页 / 共12页
结构体与共用体综合测试_第2页
第2页 / 共12页
结构体与共用体综合测试_第3页
第3页 / 共12页
结构体与共用体综合测试_第4页
第4页 / 共12页
结构体与共用体综合测试_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《结构体与共用体综合测试》由会员分享,可在线阅读,更多相关《结构体与共用体综合测试(12页珍藏版)》请在金锄头文库上搜索。

1、 结构体与共用体综合测试1 定义一个结构体变量(包括年,月,日)。计算该日在本年中是第几天,注意闰年问题。解: struct int year; int month; int day; date; main() int days; printf(input year,month,day:); scanf(%d,%d,%d,&year,month,day); switch(date.month) case 1: days=date.day; break; case 2: days=date.day+31; break; case 3: days=date.day+59; break; case

2、4: days=date.day+90; break; case 5: days=date.day+120; break; case 6: days=date.day+151; break; case 7: days=date.day+181; break; case 8: days=date.day+212; break; case 9: days=date.day+243; break; case 10: days=date.day+273; break; case 11: days=date.day+304; break; case 12: days=date.day+334; brea

3、k; if(date.year%4=0&date.year%100!=0|date.year%400=0)&date.month=3) days+=1; printf(n%d/%d is the %dth day in %d.date.month,date.day,days,date.year); 2 写一个函数days,实现上面的计算。由主函数将年,月,日传递给days函数,计算后将日数传回主函数输出。 解: struct y_m_d int year; int month; int day; date; int days(struct y_m_d datel) int sum; switc

4、h(datel.month) case 1: sum=date1.day; break; case 2: sum=date1.day+31; break; case 3: sum=date1.day+59; break; case 4: sum=date1.day+90; break; case 5: sum=date1.day+120; break; case 6: sum=date1.day+151; break; case 7: sum=date1.day+181; break; case 8: sum=date1.day+212; break; case 9: sum=date1.da

5、y+243; break; case 10: sum=date1.day+273; break; case 11: sum=date1.day+304; break; case 12: sum=date1.day+334; break; if(date1.year%4=0&date1.year%100!=0|date1.year%400=0)&date1.month=3) sum+=1; return(sum); main() printf(input year,month,day:); scanf(%d,%d,%d,&year,month,day); printf(n); printf(%d

6、/%d is the %dth day in %d.date.month,date.day,days(date),date.year); 3 编写一个函数printf,打印一个学生的成绩数组,该数组中有5个学生的数据记录每个记录包括(num,name,score3,用主函数输入这些记录,用printf函数输出这些记录 解: #define N 5 struct student char num6; char name8; int score4; stuN; main() int i,j; for(i=0;iN;i+) printf(ninput score of student %d:n,i+

7、1); printf(No.:); scanf(%s,stui.num); printf(name:); scanf(%s,stui.name); for(j=0;j3;j+) printf(score %d:,j+1); scanf(%d,&stui.scorej); printf(n); print(stu); print(struct student stu6) int i,j; printf(n NO. name score1 score2 score3n); for(i=0;iN;i+) printf(%5s%10s,stui.num,stui.name); for(j=0;j3;j

8、+) printf(%9d,stui.scorej); printf(n); 4 在上题的基础上,编写一个函数input,用来输入5个学生的数据记录. 解: #define N 5 struct student char num6; char name8; int score4; stuN; input(struct student stu) int i,j; for(i=0;iN;i+) printf(ninput scores of student %d:n,i+1); printf(No.:); scanf(%s,stui.num); printf(name:); scanf(%s,st

9、ui.name); for(j=0;j3;j+) printf(score %d:,j+1); scanf(%d,&stui.scorej); printf(n); 写一个main函数,调用input函数以及题3中提供的print函数,就可以完成对学生 数据的输入和输出.5 有10个学生,每个学生的数据包括学号,姓名,3门课的成绩,从键盘输入10个学生的数据,要求打印出3门课的总平均成绩,以及最高分的学生的数据(包括学号,姓名,3门课成绩,平均分数). #define N 10 struct student char num6; char name8; int score4; float av

10、r; stuN; main() int i,j,maxi,sum; float average; for(i=0;iN;i+) printf(ninput score 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(%d,&stui.scorej); average=0; max=0; maxi=0; for(i=0;iN;i+) sum=0; for(j=0;jmax)

11、max=sum; maxi=i; average/=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(%9d,stui.scorej); printf(%8.2fn,stui.avr); printf(average=%6.2fn,average); printf(The highest score is: %s,score total: %d,stumaxi.name,max); 6 编写一个函数

12、new,对n个字符开辟连续的存储空间,此函数应返回一个指针(地址), 指向字符串开始的空间.new(n)表示分配n个字节的内存空间. #define NULL 0 #define NEWSIZE 1000 char newbufNEWSIZE; char *newp=newbuf; char *new(int n) if(newp+n=newbuf+NEWSIZE) newp=newp+n; return(newp-n); else rerurn(NULL); 7 写一个函数free,将上题用new函数占用的空间释放.free(p)表示将p(地址)指向 的单元以后的内存段释放.解: #define NULL 0 #define NEWSIZE 1000 char newbufNEWSIZE; char *newp=

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 工作计划

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