- 12
- 0
- 约2.5千字
- 约 5页
- 2015-10-01 发布于河南
- 举报
c语言(结构体程序设计).doc
C语言程序设计实验报告
实验七、结构体程序设计
【实验目的】
(1)掌握结构体类型的概念、定义和使用;
(2)掌握结构体数组、结构体指针的定义和使用;
【实验内容及步骤】
1、输入5位同学的一组信息,包括学号、姓名、数学成绩、计算机成绩,求得每位同学的平均分和总分,然后按照总分从高到低排序。
【程序代码】:
struct student
{
int num;
char name[10];
double math_score;
double computer_score;
};
#includestdio.h
main()
{
struct student std[5],std_temp;
int i,j,temp;
double sum[5],aver[5];
for(i=0;i5;i++)
{
printf(输入第%d学生的学号、姓名、数学成绩、计算机成绩:\n,i+1);
scanf(%d%s%lf%lf,std[i].num,std[i].name,std[i].math_score,std[i].computer_score);
}
printf(您输入的学生信息为:\n);
for(i=0;i5;i++)
{
printf(学号:%-5d姓名:%s数学成绩:%3.1lf计算机成绩:%3.1lf\n,std[i].num,std[i].name,std[i].math_score,std[i].computer_score);
}
for(i=0;i5;i++)
{
sum[i]=std[i].computer_score+std[i].math_score;
aver[i]=sum[i]/2;
}
//按最高分降序排列
for(i=0;i4;i++)
{
for(j=0;j4-i;j++)
{
if(sum[j]sum[j+1])
{
//交换最高分
temp=sum[j];
sum[j]=sum[j+1];
sum[j+1]=temp;
//交换对应的学生信息
std_temp=std[j];
std[j]=std[j+1];
std[j+1]=std_temp;
//交换平均分
temp=aver[j];
aver[j]=aver[j+1];
aver[j+1]=temp;
}
}
}
printf(按最高分由高到低为:\n);
for(i=0;i5;i++)
{
printf(学号:%-5d姓名:%s数学成绩:%3.1lf计算机成绩:%3.1lf总分:%3.1lf平均分:%3.1lf\n,std[i].num,std[i].name,std[i].math_score,std[i].computer_score,sum[i],aver[i]);
}
}
【运行结果】
2.定义一个结构体变量(包括年、月、日)。编写一个函数days,计算该日期在本年中是第几天(注意闰年问题)。由主函数将年月日传递给days函数,计算之后,将结果传回到主函数输出。
【程序代码】
struct datetime
{
int year;
int month;
int day;
};
#includestdio.h
int days(struct datetime);
main()
{
int count_day;
struct datetime date;
printf(请输入年月日:\n);
scanf(%d%d%d,date.year,date.month,date.day);
count_day=days(date);
printf(日期%d/%2d/%2d是%d的第%d天\n,date.year,date.month,date.day,date.year,count_day);
}
int days(struct datetime date)
{
int result=0;
int year=date.year,month=date.month,day=date.day;
switch(month-1)
{
case 12:
result+=31;
case 11:
result+=30;
case 10:
result+=31;
case 9:
result+=30;
case 8:
result+=31;
case 7:
r
您可能关注的文档
最近下载
- 芯片的EOS失效分析及焊接工艺优化.pdf VIP
- 老旧小区改造工程施工设计方案与技术措施方案.doc VIP
- 2026年贵州省贵阳市高职单招职业技能考试题库有答案解析.docx VIP
- 2026年南昌健康职业技术学院单招职业倾向性考试题库新版.docx VIP
- 北京现代途胜汽车的维修保养手册.doc VIP
- 小学生必背古诗75首---方便打印版.pdf VIP
- 2026年南昌健康职业技术学院单招职业倾向性测试题库最新.docx VIP
- 2026年江西信息应用职业技术学院单招职业倾向性考试必刷测试卷及答案1套.docx VIP
- 征信报告模板详细版带水印可编辑2025年9月新版.pdf VIP
- 五年级下册第三单元习作满分作文:学写简单的研究报告(精选五篇).pdf VIP
原创力文档

文档评论(0)