Update step2.c

main
pc9ha2xvl 5 months ago
parent 10eda600bd
commit 6dfbbe0eb7

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