#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char id[20]; char name[20]; char gender[10]; char birth[20]; int total_score; } Student; typedef struct { char id[20]; int score1; int score2; int score3; } Score; int main() { Student s1[6]; printf("请输入6个学生的基本信息:\n"); for (int i = 0; i < 6; i++) { printf("学号、姓名、性别、出生日期:"); scanf("%s %s %s %s", s1[i].id, s1[i].name, s1[i].gender, s1[i].birth); s1[i].total_score = 0; } Score s2[6]; printf("请输入6个学生的成绩信息:\n"); for (int i = 0; i < 6; i++) { printf("学号、三门课的成绩:"); scanf("%s %d %d %d", s2[i].id, &s2[i].score1, &s2[i].score2, &s2[i].score3); } for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if (strcmp(s1[i].id, s2[j].id) == 0) { s1[i].total_score += s2[j].score1 + s2[j].score2 + s2[j].score3; } } } int total_score = 0; for (int i = 0; i < 6; i++) { total_score += s1[i].total_score; } float avg_score = (float)total_score / 6; printf("总分大于平均分的学生信息如下:\n"); printf("%-10s%-10s%-10s%-15s%-10s\n", "学号", "姓名", "性别", "出生日期", "总分"); for (int i = 0; i < 6; i++) { if (s1[i].total_score > avg_score) { printf("%-10s%-10s%-10s%-15s%-10d\n", s1[i].id, s1[i].name, s1[i].gender, s1[i].birth, s1[i].total_score); } } return 0; }这段代码用c语言写多加各科成绩
时间: 2024-03-20 08:39:53 浏览: 92
C语言头文件 LIST.H
好的,以下是加入多科成绩的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char id[20];
char name[20];
char gender[10];
char birth[20];
int total_score;
} Student;
typedef struct {
char id[20];
int scores[3]; // 改为数组,存储三门课的成绩
} Score;
int main() {
Student s1[6];
printf("请输入6个学生的基本信息:\n");
for (int i = 0; i < 6; i++) {
printf("学号、姓名、性别、出生日期:");
scanf("%s %s %s %s", s1[i].id, s1[i].name, s1[i].gender, s1[i].birth);
s1[i].total_score = 0;
}
Score s2[6];
printf("请输入6个学生的成绩信息:\n");
for (int i = 0; i < 6; i++) {
printf("学号、三门课的成绩:");
scanf("%s %d %d %d", s2[i].id, &s2[i].scores[0], &s2[i].scores[1], &s2[i].scores[2]);
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (strcmp(s1[i].id, s2[j].id) == 0) {
for (int k = 0; k < 3; k++) { // 计算总分
s1[i].total_score += s2[j].scores[k];
}
}
}
}
int total_score = 0;
for (int i = 0; i < 6; i++) {
total_score += s1[i].total_score;
}
float avg_score = (float)total_score / 6;
printf("总分大于平均分的学生信息如下:\n");
printf("%-10s%-10s%-10s%-15s%-10s\n", "学号", "姓名", "性别", "出生日期", "总分");
for (int i = 0; i < 6; i++) {
if (s1[i].total_score > avg_score) {
printf("%-10s%-10s%-10s%-15s%-10d\n", s1[i].id, s1[i].name, s1[i].gender, s1[i].birth, s1[i].total_score);
}
}
return 0;
}
```
代码中的改动有:
1. 在 Score 结构体中增加了一个整型数组 scores 来存储三门课的成绩。
2. 在输入成绩信息时,从 scanf 中读取三个整数,分别存储到 scores 数组中。
3. 在计算总分时,增加一个循环来遍历 scores 数组,将三门课的成绩相加。
阅读全文