师兄帮帮忙 UVa12412 一个简单的成绩查询问题
程序员文章站
2024-03-20 09:45:58
...
题目要求
请自行查阅。
代码部分
用线性结构实现。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct
{
int chinese;
int math;
int english;
int programming;
int rank;
int sum;
float average;
}GRADE;
typedef struct SPMS
{
char SID[15];
char CID[25];
char name[15];
GRADE grade;
bool operator<(const SPMS &s2) const
{
if(grade.sum >= s2.grade.sum)
return true;
else
return false;
}
}SPMS;
typedef struct
{
int studentnumber=0;
int classnumber=0;
}informs;
vector<SPMS> stu(105);
informs info;
void Add()
{
printf("Please enter the SID,CID,name and four sorces.Enter 0 to finishi.\n");
int chinese,math,english,programming;
int i,j,k;
char sid[15],cid[25],n[15];
char flag[2]="0";
while(scanf("%s",sid)&&strcmp(sid,flag))
{
scanf("%s%s%d%d%d%d",cid,n,&chinese,&math,&english,&programming);
for(i=0;i<info.studentnumber;i++)
{
if(strcmp(sid,stu[i].SID)==0)
{
printf("Duplicated SID.\n");
break;
}
}
if(i==info.studentnumber)
{
strcpy(stu[info.studentnumber].SID,sid);
strcpy(stu[info.studentnumber].CID,cid);
strcpy(stu[info.studentnumber].name,n);
stu[info.studentnumber].grade.chinese=chinese;
stu[info.studentnumber].grade.english=english;
stu[info.studentnumber].grade.math=math;
stu[info.studentnumber].grade.programming=programming;
stu[info.studentnumber].grade.sum=chinese+english+math+programming;
stu[info.studentnumber].grade.average=stu[info.studentnumber].grade.sum/4.0;
info.studentnumber++;
}
}
sort(stu.begin(),stu.begin()+info.studentnumber);//注意:不要错写成到stu.end(),会死循环!!
}
void Remove()
{
printf("Please enter SID or name.Enter 0 to finish.\n");
int i,j,k;
char ch[15];
char flag[2]="0";
while(scanf("%s",ch)&&strcmp(ch,flag))
{
int count=0;
for(i=0;i<info.studentnumber;i++)
{
if(strcmp(stu[i].SID,ch)==0||strcmp(stu[i].name,ch)==0)
{
stu[i].grade.sum=-1;//表示这个人被删除
count++;
}
}
if(count>1)
printf("%s student removed.\n",ch);
else if(count==1) printf("%s students removed.\n",ch);
else printf("This student does not exist.\n");
sort(stu.begin(),stu.begin()+info.studentnumber);//-1被删除的学生被排在最后,就可以下次继续使用该空间了
info.studentnumber=info.studentnumber-count;
}
}
void Query()
{
printf("Please enter SID or name.Enter 0 to finish.\n");
int i,j,k;
char ch[15];
char flag[2]="0";
while(scanf("%s",ch)&&strcmp(ch,flag))
{
int count=0,flag=0;
for(i=0;i<info.studentnumber;i++)
{
if(strcmp(stu[i].SID,ch)==0||strcmp(stu[i].name,ch)==0)
{
count++;
if(count==1)
printf("SID\tCID\tName\tGrade Ranked\tTotal Points\tAverage Score\n");
if((i-1>=0)&&(stu[i].grade.sum==stu[i-1].grade.sum))
{
flag++;
printf("%s\t%s\t%s\t\t%d\t\t%d\t\t%.2f\n",stu[i].SID,stu[i].CID,stu[i].name,i+1-flag,stu[i].grade.sum,stu[i].grade.average);
}
else
{
flag=0;
printf("%s\t%s\t%s\t\t%d\t\t%d\t\t%.2f\n",stu[i].SID,stu[i].CID,stu[i].name,i+1,stu[i].grade.sum,stu[i].grade.average);
}
}
}
}
}
void ShowStatistics_allgrade()
{
int i,j,k;
int chinesegrade=0,mathgrade=0,englishgrade=0,programminggrade=0;
int count1f=0,count2f=0,count3f=0,count4f=0;
int count_p[4]={0};
for(i=0;i<info.studentnumber;i++)
{
int countp=0;//统计该学生通过考试的数目
//统计成绩
chinesegrade+=stu[i].grade.chinese;
mathgrade+=stu[i].grade.math;
englishgrade+=stu[i].grade.english;
programminggrade+=stu[i].grade.programming;
//统计挂科数和通过数
if(stu[i].grade.chinese<60) count1f++;
else countp++;
if(stu[i].grade.math<60) count2f++;
else countp++;
if(stu[i].grade.english<60) count3f++;
else countp++;
if(stu[i].grade.programming<60) count4f++;
else countp++;
count_p[countp]++;
}
//打印结果
printf("Chinese\n");
printf("Average Score:%.2f\n",chinesegrade/(float)info.studentnumber);
printf("Number of passed students:%d\n",info.studentnumber-count1f);
printf("Number of failed students:%d\n",count1f);
printf("Mathematics\n");
printf("Average Score:%.2f\n",mathgrade/(float)info.studentnumber);
printf("Number of passed students:%d\n",info.studentnumber-count2f);
printf("Number of failed students:%d\n",count2f);
printf("English\n");
printf("Average Score:%.2f\n",englishgrade/(float)info.studentnumber);
printf("Number of passed students:%d\n",info.studentnumber-count3f);
printf("Number of failed students:%d\n",count3f);
printf("Programming\n");
printf("Average Score:%.2f\n",programminggrade/(float)info.studentnumber);
printf("Number of passed students:%d\n",info.studentnumber-count4f);
printf("Number of failed students:%d\n",count4f);
printf("Overall:\n");
printf("Number of students who passed all subjects:%d\n",count_p[4]);
for(j=3;j>=1;j--)
{
printf("Number of students who passed 3 subjects:%d\n",count_p[j]);
}
printf("Number of students who failed all subjects:%d\n",count_p[0]);
}
void ShowStatistics()
{
printf("Please enter class ID,0 for the whole statistics.\n");
char id[15];
char flag[2]="0";
scanf("%s",id);
int i,j,k;
if(strcmp(id,flag))//非全年级
{
int count=0;//人数
int chinesegrade=0,mathgrade=0,englishgrade=0,programminggrade=0;
int count1f=0,count2f=0,count3f=0,count4f=0;
int count_p[4]={0};
for(i=0;i<info.studentnumber;i++)
{
int countp=0;//统计该学生通过考试的数目
if(strcmp(id,stu[i].CID)==0)
{
count++;//该班级人数加 1
//统计成绩
chinesegrade+=stu[i].grade.chinese;
mathgrade+=stu[i].grade.math;
englishgrade+=stu[i].grade.english;
programminggrade+=stu[i].grade.programming;
//统计挂科数和通过数
if(stu[i].grade.chinese<60) count1f++;
else countp++;
if(stu[i].grade.math<60) count2f++;
else countp++;
if(stu[i].grade.english<60) count3f++;
else countp++;
if(stu[i].grade.programming<60) count4f++;
else countp++;
count_p[countp]++;
}
}
//打印结果
printf("Chinese\n");
printf("Average Score:%.2f\n",chinesegrade/(float)count);
printf("Number of passed students:%d\n",count-count1f);
printf("Number of failed students:%d\n",count1f);
printf("Mathematics\n");
printf("Average Score:%.2f\n",mathgrade/(float)count);
printf("Number of passed students:%d\n",count-count2f);
printf("Number of failed students:%d\n",count2f);
printf("English\n");
printf("Average Score:%.2f\n",englishgrade/(float)count);
printf("Number of passed students:%d\n",count-count3f);
printf("Number of failed students:%d\n",count3f);
printf("Programming\n");
printf("Average Score:%.2f\n",programminggrade/(float)count);
printf("Number of passed students:%d\n",count-count4f);
printf("Number of failed students:%d\n",count4f);
printf("Overall:\n");
printf("Number of students who passed all subjects:%d\n",count_p[4]);
for(j=3;j>=1;j--)
{
printf("Number of students who passed 3 subjects:%d\n",count_p[j]);
}
printf("Number of students who failed all subjects:%d\n",count_p[0]);
}
else
ShowStatistics_allgrade();
}
void PrintNemu()
{
printf("Welcome to Student Performance Management System(SPMS).\n\n");
printf("1 - Add\n");
printf("2 - Remove\n");
printf("3 - Query\n");
printf("4 - Show ranking\n");
printf("5 - Show Statistics\n");
printf("0 - Exit\n");
}
int main()
{
int choice;
PrintNemu();
while((scanf("%d",&choice)==1)&&choice)
{
if(choice==1) Add();
else if(choice==2) Remove();
else if(choice==3) Query();
else if(choice==4)
{
printf("Showing the ranklist hurts students' self-esteem.Don't do that\n");
}
else if(choice==5) ShowStatistics();
PrintNemu();
}
return 0;
}
欢迎交流~~
上一篇: java实现MD5和SHA1加密
下一篇: UVA1585 得分 Score