用c语言写代码,设有学生信息如下:学号(长整型)、姓名(字符串型)、年龄(整型)、课程1成绩(实型)、课程2成绩(实型)、课程3成绩(实型)、课程4成绩(实型)、课程5成绩(实型)、课程6成绩(实型)、总分(实型)、平均分(实型)。试编写程序,输入3个学生的上述信息,计算每个学生的总分、平均分,然后输出每个学生学号、姓名、总分、平均分.
时间: 2023-07-04 09:06:43 浏览: 140
以下是用C语言编写的代码:
```c
#include <stdio.h>
#include <string.h>
struct Student {
long id;
char name[20];
int age;
float score[6];
float total;
float average;
};
int main() {
struct Student stu[3];
int i, j;
// 输入每个学生的信息
for (i = 0; i < 3; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
printf("学号:");
scanf("%ld", &stu[i].id);
printf("姓名:");
scanf("%s", stu[i].name);
printf("年龄:");
scanf("%d", &stu[i].age);
printf("课程1成绩:");
scanf("%f", &stu[i].score[0]);
printf("课程2成绩:");
scanf("%f", &stu[i].score[1]);
printf("课程3成绩:");
scanf("%f", &stu[i].score[2]);
printf("课程4成绩:");
scanf("%f", &stu[i].score[3]);
printf("课程5成绩:");
scanf("%f", &stu[i].score[4]);
printf("课程6成绩:");
scanf("%f", &stu[i].score[5]);
}
// 计算每个学生的总分和平均分
for (i = 0; i < 3; i++) {
stu[i].total = 0;
for (j = 0; j < 6; j++) {
stu[i].total += stu[i].score[j];
}
stu[i].average = stu[i].total / 6;
}
// 输出每个学生的学号、姓名、总分和平均分
printf("学号\t姓名\t总分\t平均分\n");
for (i = 0; i < 3; i++) {
printf("%ld\t%s\t%.2f\t%.2f\n", stu[i].id, stu[i].name, stu[i].total, stu[i].average);
}
return 0;
}
```
这段代码定义了一个结构体 `Student`,其中包含了学生的各种信息,包括学号、姓名、年龄、各科成绩、总分和平均分。程序首先通过循环输入每个学生的信息,然后再通过循环计算每个学生的总分和平均分,并将结果输出。输出时,使用了 `\t` 来对齐各个字段,让输出的结果更加美观。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)