ADD file via upload

main
pc9ha2xvl 1 week ago
parent 7858c66a79
commit 63d2eebf1a

@ -0,0 +1,100 @@
#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];
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);
}
}
Loading…
Cancel
Save