Created
January 2, 2016 13:07
-
-
Save helloqiu/505e19ab4fbcf930e05f to your computer and use it in GitHub Desktop.
simple homework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author : helloqiu | |
Description: A simple homework. | |
P.S. You should do it yourself. QAQ | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
struct Student{ | |
int id; | |
char name[20]; | |
int score[4]; | |
}; | |
typedef struct Student student; | |
void count(student* man[]); | |
void total(student* man[]); | |
void average(student* man[]); | |
int totalScore(student* man); | |
int num = 0; | |
int main(void){ | |
printf("Please enter the number of the students:\n"); | |
scanf("%d" , &num); | |
student* man[num]; | |
for (int i = 0 ; i < num ; i ++){ | |
man[i] = malloc(sizeof(student)); | |
printf("Input student %d:\n" , i + 1); | |
printf("Number:"); | |
scanf("%d" , &(man[i] -> id)); | |
printf("Name:"); | |
scanf("%s" , &(man[i] -> name[0])); | |
printf("Score:\n"); | |
for (int j = 0 ; j < 4 ; j ++){ | |
printf("Course %d:" , j + 1); | |
scanf("%d" , &(man[i] -> score[j])); | |
} | |
} | |
while (1){ | |
printf("Enter the command:\n"); | |
int tmp; | |
scanf("%d" , &tmp); | |
switch(tmp){ | |
case 0: | |
return 0; | |
case 1: | |
count(man); | |
continue; | |
case 2: | |
total(man); | |
continue; | |
case 3: | |
average(man); | |
continue; | |
} | |
} | |
} | |
void count(student* man[]){ | |
for (int j = 0 ; j < 4 ; j ++){ | |
int ex = 0; | |
int good = 0; | |
int medium = 0; | |
int pass = 0; | |
int not_pass = 0; | |
for (int i = 0 ; i < num ; i ++){ | |
if (man[i] -> score[j] >= 60){ | |
pass += 1; | |
} | |
if (man[i] -> score[j] < 60){ | |
not_pass += 1; | |
} | |
if (man[i] -> score[j] >= 90){ | |
ex += 1; | |
continue; | |
} | |
if (man[i] -> score[j] >= 80){ | |
good += 1; | |
continue; | |
} | |
if (man[i] -> score[j] >= 70){ | |
medium += 1; | |
continue; | |
} | |
} | |
printf("Course %d\n" , j + 1); | |
printf("Excellent:\n%d\n" , ex); | |
printf("Good:\n%d\n" , good); | |
printf("Medium:\n%d\n" , medium); | |
printf("Pass:\n%d\n" , pass); | |
printf("Not Pass:\n%d\n" , not_pass); | |
} | |
} | |
void total(student* man[]){ | |
int order[num]; | |
for (int i = 0 ; i < num ; i ++){ | |
order[i] = i; | |
} | |
for(int i = 0 ; i < num - 1; i ++){ | |
for (int j = i + 1 ; j < num ; j ++){ | |
if (totalScore(man[order[i]]) < totalScore(man[order[j]])){ | |
int tmp = order[i]; | |
order[i] = order[j]; | |
order[j] = tmp; | |
} | |
} | |
} | |
for (int i = 0 ; i < num ; i ++){ | |
printf("Name:%s\n" , man[order[num - i - 1]] -> name); | |
printf("Total:%d\n" ,totalScore(man[order[num - i - 1]])); | |
} | |
} | |
void average(student* man[]){ | |
for (int i = 0 ; i < 4 ; i ++){ | |
int tmp = 0; | |
for (int j = 0 ; j < num ; j ++){ | |
tmp += man[j] -> score[i]; | |
} | |
printf("Course %d:\n" , i + 1); | |
printf("%.1f\n" , tmp * 1.000 / num); | |
} | |
} | |
int totalScore(student* man){ | |
int tmp = 0; | |
for (int i = 0 ; i < 4 ; i ++){ | |
tmp += man -> score[i]; | |
} | |
return tmp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment