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.
100 lines
2.8 KiB
100 lines
2.8 KiB
2 weeks ago
|
#include <stdio.h>
|
||
|
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];
|
||
|
int choice;
|
||
|
do {
|
||
|
displayMenu();
|
||
|
scanf(" %c", &choice);
|
||
|
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);
|
||
|
}
|
||
|
}
|