#include <stdio.h>
#include <string.h>
#define maxl 20
#define maxn 10005
#define EPS 1e-5
struct stu{
char sid[maxl], name[maxl];
int cid;
int score[4];
} stu[maxn];
int n = 0;
int removed[maxn];
int pasn[5];//下标i分别表示通过i科的人数
int pas[4], fai[4], average[4], sum[4];
void add(){
for(;;){
printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
int sid2 = 1;
char sid1[maxl];
scanf("%s", stu[n].sid);
if( strcmp( stu[n].sid, "0") == 0 ) break;
scanf("%d %s %d %d %d %d", &stu[n].cid, stu[n].name, &stu[n].score[0], &stu[n].score[1], &stu[n].score[2], &stu[n].score[3]);
for( int i=0; i<n; i++){
if( strcmp( stu[n].sid, stu[i].sid) == 0 && !removed[i] ){
printf("Duplicated SID.\n");
sid2 = 0;
break;
}
}
if(sid2) n++;
}
}
int rank( float a){
int r = 1;
for( int i=0; i<n; i++){
if( !removed[i] && stu[i].score[0]+stu[i].score[1]+stu[i].score[2]+stu[i].score[3] > a ) r++;
}
return r;
}
void DQ( int isq){
char s[maxl];
for(;;){
printf("Please enter SID or name. Enter 0 to finish.\n");
scanf("%s", s);
if( strcmp( s, "0") == 0 ) break;
int r = 0;
for( int i=0; i<n; i++){
if( !removed[i] ){
if( strcmp( stu[i].sid, s) == 0 || strcmp( stu[i].name, s) == 0 ){
if(isq){
printf("%d %s %d %s %d %d %d %d %d %.2f\n", rank(stu[i].score[0]+stu[i].score[1]+stu[i].score[2]+stu[i].score[3]), stu[i].sid, stu[i].cid, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3], stu[i].score[0]+stu[i].score[1]+stu[i].score[2]+stu[i].score[3], (stu[i].score[0]+stu[i].score[1]+stu[i].score[2]+stu[i].score[3])/4.0+EPS);
}else{
removed[i] = 1;
r++;
}
}
}
}
if(!isq) printf("%d student(s) removed.\n", r);
}
}
void cal( int i){
int idx = 0;
for( int j=0; j<4; j++){
if( stu[i].score[j] >= 60 ){
pas[j]++;
idx++;
}else{
fai[j]++;
}
sum[j] += stu[i].score[j];
}
for( int j=0; j<5; j++){
if( idx == j ){
pasn[j]++;
}
}
}
void stat(){
printf("Please enter class ID, 0 for the whole statistics.\n");
int c;
scanf("%d", &c);
memset( pasn, 0, sizeof(pasn));
memset( pas, 0, sizeof(pas));
memset( fai, 0, sizeof(fai));
memset( average, 0, sizeof(average));
memset( sum, 0, sizeof(sum));
int m = 0;
if(!c){
for( int i=0; i<n; i++){
if( !removed[i] ) {cal(i); m++;}
}
}else{
for( int i=0; i<n; i++){
if( stu[i].cid == c && !removed[i] ){
cal(i); m++;
}
}
}
for( int i=0; i<4; i++){
switch (i)
{
case 0: printf("Chinese\n"); break;
case 1: printf("Mathematics\n"); break;
case 2: printf("Engilsh\n"); break;
case 3: printf("Programing\n"); break;
}
printf("Average Score:%.2f\nNumber of passed students:%d\nNumber of failed students:%d\n\n", sum[i]*1.0/m+EPS, pas[i], fai[i]);
}
printf("Overall:\nNumber of students who passed all subjects: %d\n", pasn[4]);
printf("Number of students who passed 3 or more subjects: %d\n", pasn[3] + pasn[4]);
printf("Number of students who passed 2 or more subjects: %d\n", pasn[2] + pasn[3] + pasn[4]);
printf("Number of students who passed 1 or more subjects: %d\n", pasn[1] + pasn[2] + pasn[3] + pasn[4]);
printf("Number of students who failed all subjects: %d\n", pasn[0]);
}
int main(){
memset( removed, 0, sizeof(removed));
for(;;){
int choice;
printf("Welcome to Student Performance Management System (SPMS).\n\n\
1 - Add\n\
2 - Remove\n\
3 - Query\n\
4 - Show ranking\n\
5 - Show Statistics\n\
0 - Exit\n\n");
scanf("%d", &choice);
if( choice == 0 ) break;
if( choice == 1 ) add();
if( choice == 2 ) DQ(0);
if( choice == 3 ) DQ(1);
if( choice == 4 ) printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
if( choice == 5 ) stat();
}
return 0;
}
UVa12412
最新推荐文章于 2022-01-23 14:48:00 发布