对上段代码改为要求输入十位同学的成绩
时间: 2023-09-03 11:10:11 浏览: 47
好的,我已经修改了代码,使其能够输入十位同学的成绩。以下是修改后的代码:
```c
#include <stdio.h>
#include <string.h>
struct student
{
int class_num, id;
int t, j;
double e_score, math, c_score, pinjun;
char name[20];
};
void input(struct student a[])
{
int i;
for (i = 0; i < 10; i++)
{
printf("请输入班号:");
scanf_s("%d", &a[i].class_num);
printf("请输入学号:");
scanf_s("%d", &a[i].id);
printf("请输入名字:");
scanf_s("%s", a[i].name, sizeof(a[i].name));
printf("请输入英语成绩:");
scanf_s("%lf", &a[i].e_score);
printf("请输入C语言成绩:");
scanf_s("%lf", &a[i].c_score);
printf("请输入数学成绩:");
scanf_s("%lf", &a[i].math);
}
}
void jisuan(struct student a[])
{
int i;
for (i = 0; i < 10; i++)
{
a[i].pinjun = (a[i].e_score + a[i].c_score + a[i].math) / 3;
printf("%lf\n", a[i].pinjun);
}
}
void paixv(struct student a[])
{
for (int i = 0; i < 9; i++)
{
for (int j = i + 1; j < 10; j++)
{
if (a[j].pinjun > a[i].pinjun)
{
struct student t = a[j];
a[j] = a[i];
a[i] = t;
}
}
}
for (int i = 0; i < 10; i++)
{
printf("平均分从高到低排序:%s:%f\n", a[i].name, a[i].pinjun);
}
printf("\n");
}
void search(struct student a[], int id)
{
for (int i = 0; i < 10; i++)
{
if (a[i].id == id)
{
printf("学号:%d\n", a[i].id);
printf("班号:%d\n", a[i].class_num);
printf("名字:%s\n", a[i].name);
printf("英语成绩:%lf\n", a[i].e_score);
printf("C语言成绩:%lf\n", a[i].c_score);
printf("数学成绩:%lf\n", a[i].math);
printf("平均分:%lf\n", a[i].pinjun);
return;
}
}
printf("未找到该学号对应的信息。\n");
}
int main(void)
{
struct student a[10];
int i;
printf("***********************************************************\n");
printf("* 欢迎使用学生成绩管理系统 *\n");
printf("***********************************************************\n");
printf("* 1.输入所有学生信息 2.计算个人平均成绩 *\n");
printf("* 3.按课程成绩排序 4.计算课程平均成绩 *\n");
printf("* 5.按学号查询个人信息 6.退出 *\n");
printf("***********************************************************\n");
printf("\n");
printf("请选择要执行的功能1~6:");
scanf_s("%d", &i);
while (i != 0)
{
switch (i)
{
case 1:
input(a);
break;
case 2:
jisuan(a);
break;
case 3:
paixv(a);
break;
case 4:
// TODO: 计算课程平均成绩
break;
case 5:
printf("输入要查找的学号:");
int id;
scanf_s("%d", &id);
search(a, id);
break;
case 6:
return 0;
default:
printf("error!\n");
break;
}
scanf_s("%d", &i);
}
return 0;
}
```
现在,您可以使用这段代码来输入十位同学的成绩,并进行其他操作。请注意,输入的成绩数量和循环次数都已经从原来的 4 改为了 10。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)