13课前两题。
This commit is contained in:
70
13/Exercise01.c
Normal file
70
13/Exercise01.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user