结构体实验123351陈正宁.docx
- 文档编号:20131433
- 上传时间:2023-04-25
- 格式:DOCX
- 页数:13
- 大小:219.05KB
结构体实验123351陈正宁.docx
《结构体实验123351陈正宁.docx》由会员分享,可在线阅读,更多相关《结构体实验123351陈正宁.docx(13页珍藏版)》请在冰豆网上搜索。
结构体实验123351陈正宁
淮海工学院计算机科学系
实验报告书
课程名:
《C语言程序设计》
题目:
实验10结构体
班级:
软嵌151
学号:
2015123349
姓名:
陈正宁
1、实验内容或题目
(1)定义一个结构体变量(包括年、月、日),计算该日在本年中是第几天,注意闰年问题。
(2)编写一个程序,从键盘输入10本书的名称和定价并存在一个结构体数组中,从中查找定价最高和最低的书的名称和定价,并输出到屏幕上。
(3)教材330页第5题。
2、实验目的与要求
掌握结构体的定义
掌握结构体中成员变量的使用方法
可以使用指针实现定价最高和最低的书的查找
3、实验步骤与源程序
实验步骤
源代码
(1)、
#include
structdate
{
intyear;
intmonth;
intday;
};
intleap_year(inta);
intcal_day(structdatea);
/*判断闰年*/
intleap_year(inta)
{
if(a%400==0||(a%4==0&&a%100!
=0))
return1;
else
return0;
}
/*计算一年中的第几天*/
intcal_day(structdatea)
{
intsum=0,b[]={31,28,31,30,31,30,31,31,30,31,30,31};
for(inti=0;i sum+=b[i]; if(a.month>2) sum=sum+a.day+leap_year(a.year); else sum=sum+a.day; returnsum; } voidmain() { structdatea; intn; printf("\n请输入日期(年月日)\n"); scanf("%d%d%d",&a.year,&a.month,&a.day); n=cal_day(a); printf("该日在本年中是第%d天\n",n); } (2)、 #include #defineMAX100 typedefstructBook { charname[MAX];/*书名*/ doubleprice;/*定价*/ }Book; voidmain() { Bookbooks[10]; doublep; inti,max,min; printf("input10booksdetails\n"); for(i=0;i<10;i++) { printf("No.%2d\n",i+1); printf("请输入书名: "); scanf("%s",books[i].name); printf("请输入书的价格: "); scanf("%lf",&p); books[i].price=p; } max=min=0; for(i=1;i<10;i++) { if(books[i].price>books[max].price) max=i; if(books[i].price min=i; } printf("\n"); printf("搜索结果如下: \n"); printf("maximumprice: %.2lf,name: %s\n",books[max].price,books[max].name); printf("minimumprice: %.2lf,name: %s\n",books[min].price,books[min].name); } (3)、 #include"stdio.h" #include #defineN10 structstudent { charid[10]; charname[10]; intscore[3]; floataverage; }stud[N]; voidinput() { inti; for(i=0;i { printf("第%d个学生的信息(学号姓名课程1课程2课程3): \n",i+1); scanf("%s%s%d%d%d",stud[i].id,stud[i].name,&stud[i].score[0],&stud[i].score[1],&stud[i].score[2]); stud[i].average=(stud[i].score[0]+stud[i].score[1]+stud[i].score[2])/3.0; } } voidoutput() { inti; printf("\n"); printf("学号\t姓名\t课程\t1课程2\t课程3\t平均分\n"); for(i=0;i printf("%s\t%s\t%d\t%d\t%d\t%3.1f\n",stud[i].id,stud[i].name,stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].average); } voidsortput() { inti,j; structstudenttemp; for(i=0;i { for(j=0;j { if(stud[j].average { temp=stud[j]; stud[j]=stud[j+1]; stud[j+1]=temp; } } } printf("最高分的学生: \n%s\t%s\t%d\t%d\t%d\t%3.1f\n",stud[0].id,stud[0].name,stud[0].score[0],stud[0].score[1],stud[0].score[2],stud[0].average); } voidmain() { input(); output(); sortput(); } 4、测试数据与实验结果(可以抓图粘贴) (1)、 (2)、 (3)、 5、结果分析与实验体会 (1)struct是声明结构体类型时所必须使用的关键字,不能省略。 (2)关于结构体还有结构体数组,结构体指针。 (3)将一个结构体变量的值传递给另外一个函数,有3个方法: 用结构体变量的成员作参数。 用结构体变量作实参 用指向结构体变量(或数组元素)的指针作实参,将结构体变量(或数组元素)的地址传给形参。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 结构 实验 123351 正宁