From 10eda600bd66ee49127fa02b0e7b0a15a6195dfa Mon Sep 17 00:00:00 2001 From: pc9ha2xvl <2119910569@qq.com> Date: Fri, 22 Nov 2024 17:26:04 +0800 Subject: [PATCH] ADD file via upload --- step2.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 step2.c 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; +}