Files
BasicsOfComputerSoftwareEng…/POP/13/Exercise01.c
2023-02-21 10:56:54 +08:00

71 lines
1.7 KiB
C

#include <stdio.h>
#include <stdbool.h>
#define MALE false
#define FEMALE true
typedef struct {
int year;
int month;
int date;
} DATE;
typedef struct {
int ID;
char name[21];
bool sex;
DATE birthday;
double score;
} STUDENT;
double getAvg(STUDENT students[], int size) {
double sum = 0;
int i = 0;
for (i = 0; i < size; i++) {
sum += students[i].score;
}
sum /= size;
return sum;
}
void sortStu(STUDENT students[], int size) {
bool moved = false;
int i = 0;
STUDENT tmp;
do {
moved = false;
for (i = 0; i < size - 1; i++) {
if (students[i].score < students[i + 1].score) {
tmp = students[i];
students[i] = students[i+1];
students[i+1] = tmp;
moved = true;
}
}
} while (moved);
for (i = 0; i < size; i++) {
printf("%s: %lf\n", students[i].name, students[i].score);
}
}
int main() {
STUDENT students[10];
int i = 0;
// freopen("fakedata","r",stdin);
for (i = 0; i < 10; i++) {
printf("ID: ");
scanf("%d", &students[i].ID);
printf("Name(max 20 char): ");
scanf("%20s", students[i].name);
printf("Sex: (MALE-0/FEMLE-1)");
scanf("%d", &students[i].sex);
printf("Birthday (Year/Month/Date):");
scanf("%d/%d/%d", &students[i].birthday.year, &students[i].birthday.month, &students[i].birthday.date);
printf("Scores:\n");
scanf("%lf", &students[i].score);
}
printf("The Average is: %lf\n", getAvg(students, 10));
printf("Sort result:\n");
sortStu(students, 10);
return 0;
}