From 63d2eebf1a1bc3f242aadac17f6d22d34b4c3acb Mon Sep 17 00:00:00 2001 From: pc9ha2xvl <2119910569@qq.com> Date: Sat, 23 Nov 2024 01:57:31 +0800 Subject: [PATCH] ADD file via upload --- step3.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 step3.c diff --git a/step3.c b/step3.c new file mode 100644 index 0000000..25970c9 --- /dev/null +++ b/step3.c @@ -0,0 +1,100 @@ +#include +typedef struct { + int id; + double scores[3]; + double total; + double average; +} Student; +void displayMenu(); +void inputStudentInfo(Student students[], int count); +void calculateTotalAndAverage(Student students[], int count); +void sortStudentsByTotalScore(Student students[], int count); +void displayStudentInfo(Student students[], int count); + +int main() { + Student students[3]; + char choice; + do { + displayMenu(); + scanf("%c", &choice); + rewind(stdin); + switch (choice) { + case 'i': + inputStudentInfo(students, 3); + calculateTotalAndAverage(students, 3); + sortStudentsByTotalScore(students, 3); + displayStudentInfo(students, 3); + break; + case 'm': + printf("You are trying to Make things ordered\n"); + break; + case 'q': + printf("You are about to Quit\n"); + break; + default: + printf("Wrong input\n"); + break; + } + } while (choice != 'q'); + + return 0; +} + +void displayMenu() { + printf("\n"); + int i; + for (i = 0; i < 30; i++) printf(" "); + printf("1. Input\n"); + for (i = 0; i < 30; i++) printf(" "); + printf("2. Output\n"); + for (i = 0; i < 30; i++) printf(" "); + printf("3. Order\n"); + for (i = 0; i < 30; i++) printf(" "); + printf("4. Quit\n"); + printf("\nPlease input your option: "); +} + +void inputStudentInfo(Student students[], int count) { + printf("Please input info of the three students:\n"); + int i,j; + for (i = 0; i < count; i++) { + printf(""); + scanf("%d", &students[i].id); + for (j = 0; j < 3; j++) { + printf(""); + scanf("%lf", &students[i].scores[j]); + } + } +} + +void calculateTotalAndAverage(Student students[], int count) { + int i,j; + for ( i = 0; i < count; i++) { + students[i].total = 0; + for ( j = 0; j < 3; j++) { + students[i].total += students[i].scores[j]; + } + students[i].average = students[i].total / 3; + } +} + +void sortStudentsByTotalScore(Student students[], int count) { + Student temp; + int i,j; + for (i = 0; i < count - 1; i++) { + for (j = 0; j < count - i - 1; j++) { + if (students[j].total > students[j + 1].total) { + temp = students[j]; + students[j] = students[j + 1]; + students[j + 1] = temp; + } + } + } +} + +void displayStudentInfo(Student students[], int count) { + int i; + for ( i = 0; i < count; i++) { + printf("%d,%.1f,%.1f\n", students[i].id, students[i].total, students[i].average); + } +}