#include
#define N 5
struct Student
{
int num;
char name[20];
double score[3];
double aver;
int year;
int month;
int day;
};
int main()
{
void input(struct Student stu[]);
void average(struct Student stu[]);
void sort(struct Student stu[]);
int days(struct Student student);
struct Student stu[N], * p = stu;
input(p);
int choice;
int m;
printf("Input each students's information\n");
while (1)
{
printf("===============the Score Processing System============================\n");
printf("1,print each student's average\n");
printf("2,order the students by student's average decreasingly\n");
printf("3,printf brithday of student\n");
printf("======================================================================\n");
printf("Please choose (1~3):\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
average(p); break;
case 2:
sort(p); break;
case 3:
printf("请输入第几名同学的生日\t");
scanf("%d", &m);
printf("该同学的学号:%d ,名字:%s 生日 :%d年%d月%d日,本年第%d天\n", stu[m-1].num, stu[m-1].name, stu[m-1].year, stu[m-1].month, stu[m-1].day, days(stu[m-1]));
default:
break;
}
}
return 0;
}
void input(struct Student stu[])
{
int i;
printf("请输入各学生的信息:\n");
for (i = 0; i < N; i++)
{
printf("学号 姓名 3门课成绩 生日\t");
scanf("%d %s %lf %lf %lf\n %d %d %d", &stu[i].num, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2], &stu[i].year, &stu[i].month, &stu[i].day);
stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
printf("\n");
}
}
void average(struct Student stu[])
{
for (int i = 0; i < N; i++)
{
stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
printf("学号:%d 姓名: %s 平均成绩:%4.2lf", stu[i].num, stu[i].name, stu[i].aver);
}
}
void sort(struct Student stu[])
{
struct Student temp;
int i, j, k;
for (i = 0; i < N - 1; i++)
{
k = i;
for (j = i + 1; j < N; j++)
{
if (stu[i].aver < stu[j].aver)
k = j;
temp = stu[k];
stu[k] = stu[i];
stu[i] = temp;
}
}
for (i = 0; i < N; i++)
{
printf("学号:%d 姓名: %s 三门课的成绩分别为: %4.2lf %4.2lf %4.2lf 平均成绩:%4.2lf\n", stu[i].num, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].aver);
}
printf("\n");
}
int days(struct Student student)
{
if((student.year%4==0&&student.year%100!=0)||(student.year%400==0))
{
if(student.month == 1)
{
return student.day;
}
else if(student.month == 2)
{
return student.day+31;
}
else if(student.month == 3)
{
return student.day+60;
}
else if(student.month == 4)
{
return student.day+91;
}
else if(student.month == 5)
{
return student.day+121;
}
else if(student.month == 6)
{
return student.day+152;
}
else if(student.month == 7)
{
return student.day+182;
}
else if(student.month == 8)
{
return student.day+213;
}
else if(student.month == 9)
{
return student.day+244;
}
else if(student.month == 10)
{
return student.day+274;
}
else if(student.month == 11)
{
return student.day+305;
}
else if(student.month == 12)
{
return student.day+335;
}
}
else
{
if(student.month == 1)
{
return student.day;
}
else if(student.month == 2)
{
return student.day+30;
}
else if(student.month == 3)
{
return student.day+59;
}
else if(student.month == 4)
{
return student.day+90;
}
else if(student.month == 5)
{
return student.day+120;
}
else if(student.month == 6)
{
return student.day+151;
}
else if(student.month == 7)
{
return student.day+181;
}
else if(student.month == 8)
{
return student.day+212;
}
else if(student.month == 9)
{
return student.day+243;
}
else if(student.month == 10)
{
return student.day+273;
}
else if(student.month == 11)
{
return student.day+304;
}
else if(student.month == 12)
{
return student.day+334;
}
}
}
以下是改良版
效果图
#include
#define N 5
struct Student
{
int num;
char name[20];
double score[3];
double aver;
int year;
int month;
int day;
};
int main()
{
void input(struct Student stu[]);
void average(struct Student stu[]);
void sort(struct Student stu[]);
void search(struct Student stu[], int n);
int searchStudentnum(struct Student stu[], int m);
int days(struct Student student);
struct Student stu[N], * p = stu;
input(p);
int choice, m, n, c;
printf("Input each students's information\n");
printf("===============the Score Processing System============================\n");
printf("1,print each student's average\n");
printf("2,order the students by student's average decreasingly\n");
printf("3,printf brithday of student\n");
printf("4,search student according to your searched num\n");
printf("======================================================================\n");
while (1)
{
printf("\n");
printf("Please choose (1~4):\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
average(p); break;
case 2:
sort(p); break;
case 3:
printf("请输入查询编号多少的同学的生日:\t");
scanf("%d", &m);
c = searchStudentnum(p,m);
printf("该同学的学号:%d 名字:%s 生日 :%d年%d月%d日,本年第%d天\n", stu[c].num, stu[c].name, stu[c].year, stu[c].month, stu[c].day, days(stu[c]));break;
case 4:
printf("请输入您要寻找的同学编号:\t");
scanf("%d", &n);
search(p,n);
default:
break;
}
}
return 0;
}
void input(struct Student stu[])
{
int i;
printf("请输入各学生的信息:\n");
for (i = 0; i < N; i++)
{
printf("学号 姓名 3门课成绩 生日\t");
scanf("%d %s %lf %lf %lf\n %d %d %d", &stu[i].num, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2], &stu[i].year, &stu[i].month, &stu[i].day);
stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
printf("\n");
}
}
void average(struct Student stu[])
{
for (int i = 0; i < N; i++)
{
stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
printf("学号:%d 姓名: %s 平均成绩:%4.2lf", stu[i].num, stu[i].name, stu[i].aver);
printf("\n");
}
}
void sort(struct Student stu[])
{
int index[N] = {
0};
int i, j, k, temp;
for(i = 0; i < N; i++)
{
index[i] = i;
}
for (i = 0; i < N - 1; i++)
{
k = i;
for (j = i + 1; j < N; j++)
{
if (stu[index[k]].aver < stu[index[j]].aver)
{
k = j;
}
}
temp = index[k];
index[k] = index[i];
index[i] = temp;
}
for (i = 0; i < N; i++)
{
printf("学号:%d 姓名: %s 三门课的成绩分别为: %4.2lf %4.2lf %4.2lf 平均成绩:%4.2lf\n", stu[index[i]].num, stu[index[i]].name, stu[index[i]].score[0], stu[index[i]].score[1], stu[index[i]].score[2], stu[index[i]].aver);
}
printf("\n");
}
int days(struct Student student)
{
if((student.year%4==0&&student.year%100!=0)||(student.year%400==0))
{
if(student.month == 1)
{
return student.day;
}
else if(student.month == 2)
{
return student.day+31;
}
else if(student.month == 3)
{
return student.day+60;
}
else if(student.month == 4)
{
return student.day+91;
}
else if(student.month == 5)
{
return student.day+121;
}
else if(student.month == 6)
{
return student.day+152;
}
else if(student.month == 7)
{
return student.day+182;
}
else if(student.month == 8)
{
return student.day+213;
}
else if(student.month == 9)
{
return student.day+244;
}
else if(student.month == 10)
{
return student.day+274;
}
else if(student.month == 11)
{
return student.day+305;
}
else if(student.month == 12)
{
return student.day+335;
}
}
else
{
if(student.month == 1)
{
return student.day;
}
else if(student.month == 2)
{
return student.day+30;
}
else if(student.month == 3)
{
return student.day+59;
}
else if(student.month == 4)
{
return student.day+90;
}
else if(student.month == 5)
{
return student.day+120;
}
else if(student.month == 6)
{
return student.day+151;
}
else if(student.month == 7)
{
return student.day+181;
}
else if(student.month == 8)
{
return student.day+212;
}
else if(student.month == 9)
{
return student.day+243;
}
else if(student.month == 10)
{
return student.day+273;
}
else if(student.month == 11)
{
return student.day+304;
}
else if(student.month == 12)
{
return student.day+334;
}
}
}
void search(struct Student stu[], int n)
{
for(int i = 0; i < N; i++)
{
if(stu[i].num == n)
{
printf("学号:%d 姓名: %s 三门课的成绩分别为: %4.2lf %4.2lf %4.2lf 平均成绩:%4.2lf\n", stu[i].num, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].aver);
printf("\n");
}
}
}
int searchStudentnum(struct Student stu[],int m)
{
for(int i = 0; i < N; i++)
{
if(stu[i].num == m)
{
return i;
}
}
}