struct student { char name[20]; int score[MAX_SUB_NUM]; // 每门科目的成绩 int total; // 总分 float average; // 平均分 };
时间: 2023-10-21 19:06:20 浏览: 35
这是一个定义结构体的代码,结构体名为student,包含了学生的姓名、每门科目的成绩、总分和平均分。
其中,name是一个字符数组,可以存储学生姓名,长度为20。
score是一个整型数组,长度为MAX_SUB_NUM,表示每门科目的成绩,可以根据实际情况修改MAX_SUB_NUM的值。
total是一个整型变量,表示学生的总分。
average是一个浮点型变量,表示学生的平均分。
通过定义这个结构体,我们可以方便地存储和处理学生的信息。
相关问题
#include <stdio.h> #include <string.h> #define MAX_STUDENT_NUM 30 // #define MAX_COURSE_NUM 6 // #define NAME_LEN 20 // struct Student { char id[10];//学生学号 char name[NAME_LEN];//姓名 int scores[MAX_COURSE_NUM];//各科分数 int total_score;//总分 float avg_score;//平均分 }; void input_record(struct Student students[], int num);//录入每个学生的学号、姓名和各科考试成绩 void calc_total_avg_course(struct Student students[], int num, int course_num);//计算每门课程的总分和平均分 请用c语言分别补充函数内容
补充input_record函数内容:
```
void input_record(struct Student students[], int num)
{
for (int i = 0; i < num; i++)
{
printf("请输入第%d个学生的学号:", i+1);
scanf("%s", students[i].id);
printf("请输入第%d个学生的姓名:", i+1);
scanf("%s", students[i].name);
for (int j = 0; j < MAX_COURSE_NUM; j++)
{
printf("请输入第%d个学生的第%d门课程成绩:", i+1, j+1);
scanf("%d", &students[i].scores[j]);
}
}
}
```
补充calc_total_avg_course函数内容:
```
void calc_total_avg_course(struct Student students[], int num, int course_num)
{
for (int i = 0; i < course_num; i++)
{
int total_score = 0;
for (int j = 0; j < num; j++)
{
total_score += students[j].scores[i];
}
printf("第%d门课程的总分为:%d,平均分为:%.2f\n", i+1, total_score, (float)total_score/num);
}
}
```
注:这里假设MAX_COURSE_NUM为6,NAME_LEN为20。
#include <stdio.h> #include <string.h> #define MAX_STUDENT_NUM 30 // #define MAX_COURSE_NUM 6 // #define NAME_LEN 20 // struct Student { char id[10];//学生学号 char name[NAME_LEN];//姓名 int scores[MAX_COURSE_NUM];//各科分数 int total_score;//总分 float avg_score;//平均分 }; void statistic_analysis(struct Student students[], int num, int course_num);//对每门课程分别统计每个类别的人数以及所占的百分比 void list_record(struct Student students[], int num, int course_num);//输出每个学生的学号、姓名、各科考试成绩以及每门课程的总分和平均分 请用c语言分别补充函数内容
statistic_analysis函数内容如下:
```
void statistic_analysis(struct Student students[], int num, int course_num) {
int i, j;
int count[MAX_COURSE_NUM][3] = {0}; // 统计每门课程的A、B、C类人数
float percent[MAX_COURSE_NUM][3] = {0}; // 统计每门课程的A、B、C类人数所占百分比
// 遍历每个学生
for (i = 0; i < num; i++) {
// 遍历每门课程
for (j = 0; j < course_num; j++) {
if (students[i].scores[j] >= 90) {
count[j][0]++; // A类人数+1
} else if (students[i].scores[j] >= 80) {
count[j][1]++; // B类人数+1
} else if (students[i].scores[j] >= 70) {
count[j][2]++; // C类人数+1
}
}
}
// 计算每门课程的A、B、C类人数所占百分比
for (i = 0; i < course_num; i++) {
for (j = 0; j < 3; j++) {
percent[i][j] = (float) count[i][j] / num * 100;
}
}
// 输出统计结果
printf("Course\tA\tB\tC\n");
for (i = 0; i < course_num; i++) {
printf("%d\t%.2f%%\t%.2f%%\t%.2f%%\n", i+1, percent[i][0], percent[i][1], percent[i][2]);
}
}
```
list_record函数内容如下:
```
void list_record(struct Student students[], int num, int course_num) {
int i, j;
printf("ID\tName\t");
for (i = 0; i < course_num; i++) {
printf("Course%d\t", i+1);
}
printf("Total\tAverage\n");
// 输出每个学生的信息
for (i = 0; i < num; i++) {
printf("%s\t%s\t", students[i].id, students[i].name);
int total_score = 0;
for (j = 0; j < course_num; j++) {
printf("%d\t", students[i].scores[j]);
total_score += students[i].scores[j];
}
students[i].total_score = total_score;
students[i].avg_score = (float) total_score / course_num;
printf("%d\t%.2f\n", students[i].total_score, students[i].avg_score);
}
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)