《高级语言程序设计》实验报告实验序号:8 实验项目名称:结构体
int i;
for(i = 0; i<= number; i++)
{
Stu[i] = (stu[i].score[0]+ stu[i].score[1]+stu[i].score[2])/3.0
}
}
③求平均分最高的学生
int averagescore(student stu[], int number)
{
int k = 0;
//查找最高平均分并记录在数组中的下标值
for( int i = 0; i<= number; i++)
{
………
}
return k; //返回最高平均分数组元素的下标
}
【测试数据】
自己设计一组测试数据。
运行结果截图:
【思考与扩展】
如果这样定义结构体
typedef struct Student
{
int num;//学号
char name[20];//姓名注意字节长度
char classname[20];//班级
float score[3];//三门课程成绩
float aver_score; //平均分
} STU,* pSTU;
说明如下三条语句的含义:
student stu1;
STU stu2;
pSTU pstu;
2.采用结构体数组编写程序,定义一个含职工姓名、工作年限、工资总额的结构体类型,初始化5名职工的信息,最后再对工作年限超过30年的职工加100元工资,然后分别输出工资变化之前和之后的所有职工的信息。
运行结果截图
3.定义一个结构体变量(包括年、月、日)。
计算某年某月某日是本年中第几天?注意闰年问题。
运行结果截图:
四、分析与讨论
对上机实践结果进行分析,上机的心得体会。
成绩
五、教师评语
签名:
日期:
附源程序清单:
1. #include<stdio.h>
struct student
{
int num;
char name[20];
char classname[20];
float score[3];
float aver_score;
}stu[5];
void input()
{
int i;
for(i=1;i<6;i++)
{ printf("第%d个同学",i);
printf("请输入学号名字班级三门课程成绩:\n");
scanf("%d %s %s %f %f %f",&stu[i].num,stu[i].name,stu[i].classname,&stu[i].score[1],&stu [i].score[2],&stu[i].score[3]);
}
};
void averagescore()
{
for(i=1;i<=5;i++)
stu[i].aver_score=((stu[i].score[1]+stu[i].score[2]+stu[i].score[3])/3);
printf("平均成绩:");
for(i=1;i<6;i++)
printf("第%d个同学的平均成绩%f:\n",i,stu[i].aver_score);
printf("\n");
};
void max()
{
int i,k=0;
float temp=stu[1].aver_score;
for(i=2;i<=5;i++)
if(stu[i].aver_score>temp) {temp=stu[i] .aver_score;k=i;};
printf("成绩最好的同学:\n");
printf("%d %s %s %4.2f %4.2f %4.2f %4.2f\n",
stu[k].num,stu[k].name,stu[k].classname,stu[k].score[1],stu[k].score[2],stu[k].score[3],stu[k].aver _score);
};
void main()
{
input();
averagescore();
max();
}
2.#include <stdio.h>
struct worker
{
char name[20];
int workyear;
float salary;
}work[5];
void input()
{
int i;
for(i=1;i<=5;i++)
{
printf("第%d个工人:",i);
printf("请输入名字工作年限工资总额:\n");
scanf("%s %d %f",&work[i].name,&work[i].workyear,&work[i].salary);
};
void main( )
{
int i;
input();
printf("初始化5名职工的信息:\n");
for(i=1;i<=5;i++)
printf("%s %d %f \n",work[i].name,work[i].workyear,work[i].salary);
for(i=1;i<=5;i++)
if(work[i].workyear>30) work[i].salary+=100;
printf("最后5名职工的信息工:\n");
for(i=1;i<=5;i++)
printf("%s %d %f \n",work[i].name,work[i].workyear,work[i].salary);
}
3.#include <stdio.h>
struct years
{
int year;
int month;
int date;
}ye;
int data[12]={31,28,31,30,31,30,31,31,30,31,30,31};
void main( )
{ int i,sum=0;
int leap=0;
printf("请输入改天的年月日:\n");
scanf("%d %d %d",&ye.year,&ye.month,&ye.date);
if(((ye.year%4)==0)&&((ye.year%100)!=0)) leap=1;
if(((ye.year%4)==0)&&((ye.year%400)==0)) leap=1;
if(leap=1) data[1]=29;
for(i=0;i<(ye.month-1);i++)
{
sum+=data[i];
}
sum+=ye.date;
printf("\nit is the %d day.\n",sum); }。