From 50f9078661deeb8402dde05356a285d1ad60baea Mon Sep 17 00:00:00 2001 From: pfwail38f <1044591285@qq.com> Date: Sun, 12 Nov 2023 22:53:55 +0800 Subject: [PATCH] ADD file via upload --- 3.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 3.c diff --git a/3.c b/3.c new file mode 100644 index 0000000..8913909 --- /dev/null +++ b/3.c @@ -0,0 +1,96 @@ +#include +#include + +#define N 3 // 学生数目 + +struct student { + int id; // 学号 + float math, physics, english, total, average; // 数学成绩,物理成绩,英语成绩,总成绩,平均成绩 +}; + +void input(struct student stu[]) { + int i; + printf("Please input info of the three students:\n"); + for (i = 0; i < N; i++) { + + scanf("%d%f%f%f", &stu[i].id, &stu[i].math, &stu[i].physics, &stu[i].english); + stu[i].total = stu[i].math + stu[i].physics + stu[i].english; + stu[i].average = stu[i].total / 3.0; + } + int j; + struct student temp; + for (i = 0; i < N - 1; i++) { + for (j = 0; j < N - 1 - i; j++) { + if (stu[j].total > stu[j + 1].total) { + temp = stu[j]; + stu[j] = stu[j + 1]; + stu[j + 1] = temp; + } + } + } + + for (i = 0; i < N; i++) { + printf("%d,%.1f,%.1f\n", stu[i].id, stu[i].total, stu[i].average); + } +} + +void output(struct student stu[]) { + int i; + printf("Output student info:\n"); + for (i = 0; i < N; i++) { + printf("Student %d,%.1f,%.1f,%.1f,%.1f\n", stu[i].id, stu[i].math, stu[i].physics, stu[i].english, stu[i].total); + } +} + +void order(struct student stu[]) { + int i, j; + struct student temp; + for (i = 0; i < N - 1; i++) { + for (j = 0; j < N - 1 - i; j++) { + if (stu[j].total > stu[j + 1].total) { + temp = stu[j]; + stu[j] = stu[j + 1]; + stu[j + 1] = temp; + } + } + } + + for (i = 0; i < N; i++) { + printf("Student %d,%.1f,%.1f,%.1f,%.1f,%.1f\n", stu[i].id, stu[i].total, stu[i].average); + } +} + +int main() { + struct student stu[N]; + char choice; + while (1) { + printf(" 1.Input\n"); + printf(" 2.Output\n"); + printf(" 3.Order\n"); + printf(" 4.Quit\n"); + printf("Please input info of the three students:\n"); + scanf("%c", &choice); + switch (choice) { + case 'i': + input(stu); + + break; + case 'o': + output(stu); + break; + case 'r': + order(stu); + break; + case 'q': + printf("You are about to Quit\n"); + exit(0); + case 'm': + printf("You are trying to Make things ordered\n"); + break; + default: + printf("Wrong input\n"); + break; + } + } + return 0; +}