You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
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;
}