程序设计与问题求解实验
题目
设计一个保存学生成绩信息的结构,包括学号、姓名、课程名、平时成绩、考试成绩、总评成绩。分别用函数实现以下功能:
- 输入n个学生的信息(平时和考试成绩)
- 要求计算并输出学生的总分(平时20%,考试80%)并输出;
- 输出总分最高和最低的学生信息。
代码
我是用动态链表来存储学生信息的!
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//#include <time.h>
#define LEN sizeof(struct student)
struct course
{
float pea,exam;
};
struct student //声明结构体
{
long num;
char name[20];
float score;
struct course Chi,Math,Eng;
struct student *next;
};
int n; //记录链表结构体数量
struct student *max,*min; //保存最高分与最低分同学的信息
struct student *creat(void) //建立关于学生的动态链表,并计算总成绩
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student * )malloc(LEN);
printf("Please input student's ID and Name :\n");
scanf("%ld %s",&p1->num,&p1->name);
head=NULL;
while (p1->num!=0)
{
n=n+1;
if (n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student * )malloc(LEN);
printf("Please input student's ID and Name :\n");
scanf("%ld %s",&p1->num,&p1->name);
}
p2->next=NULL;
return(head);
}
void input(struct student *head)
{
struct student *p;
srand( (unsigned)time( NULL ) );
for(p=head;p!=NULL;p=p->next)
{
printf("Please input %s's common,exam score of Chinese:\n",p->name);
//printf("%.0f\t%.0f\n",p->Chi.pea=rand()%100,p->Chi.exam=rand()%100);
scanf("%f %f",&p->Chi.pea,&p->Chi.exam);
printf("Please input %s's common,exam score of Math:\n",p->name);
//printf("%.0f\t%.0f\n",p->Math.pea=rand()%100,p->Math.exam=rand()%100);
scanf("%f %f",&p->Math.pea,&p->Math.exam);
printf("Please input %s's common,exam score of English:\n",p->name);
//printf("%.0f\t%.0f\n",p->Eng.pea=rand()%100,p->Eng.exam=rand()%100);
scanf("%f %f",&p->Eng.pea,&p->Eng.exam);
p->score=(p->Chi.pea+p->Math.pea+p->Eng.pea)*0.2
+(p->Chi.exam+p->Math.exam+p->Eng.exam)*0.8;
//printf(" %f \n",p->score);
}
}
void seek(struct student *head) //搜索总分最高和最低
{
struct student *p;
float Max,Min;
Max=Min=head->score;
for(p=head;p!=NULL;p=p->next)
{
if(p->score>=Max)
{
Max=p->score;
max=p;
}
if(p->score<=Min)
{
Min=p->score;
min=p;
}
}
}
void print () //输出总分最高和最低
{
printf("\nThere are %d students in all.\n",n);
printf("The information of the students with the highest total score is as follows :\n");
printf("%ld\t%s\t%.2f\n",max->num,max->name,max->score);
printf("The information of the students with the lowest total score is as follows :\n");
printf("%ld\t%s\t%.2f\n",min->num,min->name,min->score);
}
int main ( ) //主函数
{
struct student *head;
head=creat();
input(head);
seek(head);
print();
system("pause");
return 0;
}