题目描述 你需要使用c语言将一系列的学生期末考试信息进行存储(学生人数不会超过100)。每个学生的信息包括: 姓名(由 first name 和last name 两部分组成,例如Jingyu LI,first_name = "Jingyu" last_name = "LI") ; 学号(12 位数字组成,开头4位为2022、2021、2020); C语言成绩(一个大于等于零的整数); 重修信息(学号 2022.........为否,其余为是); GPA等级(A+, A, B+, B, C+, C, D, F )。 A+: 100-93; A: 92-85; B+: 84-80; B: 79-75; C+:74-70; C: 69-65; D: 64-60; F: <60. 其中,姓名,学号,成绩为输入数据,其余数据需要你计算。 另外,学号不符合规定的数据需要删除掉。 输入 一系列 名、姓、学号、成绩 输出 名、姓、学号、成绩、重修信息、GPA等级
时间: 2024-02-17 07:05:22 浏览: 32
可以定义一个函数来计算GPA等级,根据输入的成绩返回对应的等级。同时,可以在输入时进行学号的规则检查,如果不符合规定则删除该学生信息。
以下是一个示例代码:
```
#include <stdio.h>
#include <string.h>
// 定义结构体
struct student {
char first_name[20];
char last_name[20];
char student_id[13];
int c_score;
char is_retake[3];
char gpa_level[3];
};
// 计算GPA等级
char* calculate_gpa(int score) {
if (score >= 93) {
return "A+";
} else if (score >= 85) {
return "A";
} else if (score >= 80) {
return "B+";
} else if (score >= 75) {
return "B";
} else if (score >= 70) {
return "C+";
} else if (score >= 65) {
return "C";
} else if (score >= 60) {
return "D";
} else {
return "F";
}
}
int main() {
// 定义结构体数组
struct student students[100];
int count = 0;
// 输入学生信息
while (1) {
char first_name[20];
char last_name[20];
char student_id[13];
int c_score;
printf("请输入第%d个学生的信息:\n", count+1);
printf("请输入first name:");
scanf("%s", first_name);
printf("请输入last name:");
scanf("%s", last_name);
printf("请输入学号:");
scanf("%s", student_id);
printf("请输入C语言成绩:");
scanf("%d", &c_score);
// 检查学号是否符合规定
if (strncmp(student_id, "2022", 4) == 0 || strncmp(student_id, "2021", 4) == 0 || strncmp(student_id, "2020", 4) == 0) {
// 添加学生信息到数组中
strcpy(students[count].first_name, first_name);
strcpy(students[count].last_name, last_name);
strcpy(students[count].student_id, student_id);
students[count].c_score = c_score;
if (strncmp(student_id, "2022", 4) == 0) {
strcpy(students[count].is_retake, "否");
} else {
strcpy(students[count].is_retake, "是");
}
strcpy(students[count].gpa_level, calculate_gpa(c_score));
count++;
// 判断是否已经输入100个学生信息
if (count == 100) {
break;
}
} else {
printf("学号不符合规定,请重新输入!\n");
}
}
// 输出学生信息
for (int i = 0; i < count; i++) {
printf("姓名:%s %s\n", students[i].first_name, students[i].last_name);
printf("学号:%s\n", students[i].student_id);
printf("C语言成绩:%d\n", students[i].c_score);
printf("重修信息:%s\n", students[i].is_retake);
printf("GPA等级:%s\n", students[i].gpa_level);
}
return 0;
}
```