diff --git a/step2.c b/step2.c new file mode 100644 index 0000000..8707371 --- /dev/null +++ b/step2.c @@ -0,0 +1,48 @@ +#include +int main() { + int student_id[3]; + float math_score[3], physics_score[3], english_score[3], total_score[3]; + + // 遍历三个学生的数据录入 + int i; + for (i = 0; i < 3; i++) { + // 录入学号,并验证其有效性(5位自然数且首位不为0) + while (1) { + scanf("%d", &student_id[i]); + if (student_id[i] >= 10000 && student_id[i] <= 99999) { + break; + } else { + printf("学号应为5位自然数且首位不能是0,请重新输入学号%d的信息:\n", i + 1); + // 清除输入缓冲区中的无效输入 + while (getchar() != '\n'); + } + } + + // 录入并验证三个科目的成绩(两位整数加一位小数) + int j; + for (j = 0; j < 3; j++) { + float *score = (j == 0) ? &math_score[i] : (j == 1) ? &physics_score[i] : &english_score[i]; + while (1) { + scanf("%f", score); + if (*score >= 0.0 && *score < 100.0) { // 假设成绩在0到99.9之间 + break; + } else { + printf("成绩应在0.0到99.9之间,请重新输入学生%d的科目%d成绩:\n", i + 1, j + 1); + // 清除输入缓冲区中的无效输入 + while (getchar() != '\n'); + } + } + } + + // 计算总成绩 + total_score[i] = math_score[i] + physics_score[i] + english_score[i]; + } + + // 按照指定格式打印输出结果 + + for (i = 0; i < 3; i++) { + printf("%05d %.1f %.1f %.1f %.1f\n", student_id[i], math_score[i], physics_score[i], english_score[i], total_score[i]); + } + + return 0; +}