[uva 12412]A Typical Homework(师兄帮帮忙)

不得不说,这一题有点6,主要有精度控制和计算平均数时不能除以0这些细节要注意,然而我是跪在了排名函数上,代码略凌乱,希望大家不要介意,就让这题作为我博客的开始吧.

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
const double EPS=1e-5;
struct point
{
    int chi,ma,eng,code,flag,sum,cid;
	string s,sid;
	point(int a=1):flag(a){}
};
vector<point> total;
bool compare(point a,point b)
{
	return a.sum<b.sum;
}
void print()
{
	printf("Welcome to Student Performance Management System (SPMS).\n\n");
	printf("1 - Add\n2 - Remove\n3 - Query\n4 - Show ranking\n5 - Show Statistics\n0 - Exit\n\n");
}

void add()
{
	for(;;)
	{  
	  int i,j,k=0;
	  point l;
	  printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
	  cin>>l.sid;
	  if(l.sid=="0")break;
	  cin>>l.cid>>l.s>>l.chi>>l.ma>>l.eng>>l.code;
	  l.sum=l.chi+l.ma+l.eng+l.code;
	  for(i=0;i<total.size();i++)
      if(total[i].sid==l.sid && total[i].flag!=0)
	  {
	    k=1;
	  	break;
	  }
	  if(k==0){total.push_back(l);}
	    else printf("Duplicated SID.\n");
	}
}

int rank(point x)
{
    int sum=1;
    for(int i=0;i<total.size();i++)
    if(total[i].flag!=0 && x.sum<total[i].sum)sum++;
    return sum;
}
void DAQ(int flag)
{
	string s;
	int i,j=1,r;
	for(;;)
	{
	  printf("Please enter SID or name. Enter 0 to finish.\n");
	  cin>>s;
	  if(s=="0")return;
	  r=0;
	  for(i=0;i<total.size();i++)
	  if(total[i].flag)
	  {
  	    point &l=total[i];                          
	  	if(l.flag!=0 && ((l.s==s) || (l.sid==s)))
		{
			if(flag==1)
			{
				cout<<rank(l)<<' '<<l.sid<<' '<<l.cid<<' '<<l.s<<' '<<l.chi<<' '<<l.ma<<' '<<l.eng<<' '<<l.code<<' '<<l.sum<<' ';
				printf("%.2lf\n",l.sum/4.0+EPS);
			}
			else {l.flag=0; r++;}
		}
	  }  
	  if(!flag)printf("%d student(s) removed.\n",r);
	}
}
void writeln()
{
	int i,h,l1,len=0;
	double j;
	int a[10],b[10],ans[10];
	printf("Please enter class ID, 0 for the whole statistics.\n");
	cin>>h;
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(ans,0,sizeof(ans));
	for(i=0;i<total.size();i++)
	if(total[i].flag!=0 &&(total[i].cid==h || h==0))
	{
		point &l=total[i];
		int j=0;
		if(l.chi>=60){j++;a[1]++;}
		if(l.ma>=60){j++;a[2]++;}
		if(l.eng>=60){j++;a[3]++;}
		if(l.code>=60){j++;a[4]++;}
		b[1]+=l.chi; b[2]+=l.ma; b[3]+=l.eng; b[4]+=l.code;
		len++;
		ans[j]++;
	}
	for(i=1;i<=4;i++)
	{
		if(i==1)printf("Chinese\n");
		  else if(i==2)printf("Mathematics\n");
		    else if(i==3)printf("English\n");
		      else printf("Programming\n");
        if(len==0)j=0;
         else j=(b[i]*1.0)/len+EPS;
		printf("Average Score: %.2lf\n",j);
		printf("Number of passed students: %d\nNumber of failed students: %d\n\n",a[i],len-a[i]);
	}
	printf("Overall:\n");
	printf("Number of students who passed all subjects: %d\n",ans[4]);
	printf("Number of students who passed 3 or more subjects: %d\n",ans[3]+ans[4]);
	printf("Number of students who passed 2 or more subjects: %d\n",ans[3]+ans[4]+ans[2]);
	printf("Number of students who passed 1 or more subjects: %d\n",ans[1]+ans[2]+ans[3]+ans[4]);
	printf("Number of students who failed all subjects: %d\n\n",ans[0]);
} 
int main()
{
	int change;
	for(;;)
	{
		print();
		scanf("%d",&change);
		if(change==0)break;
		if(change==1)add();
		if(change==2)DAQ(0);
		if(change==3)DAQ(1);
		if(change==4)printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
		if(change==5)writeln();
	}
	return 0;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/changing___/article/details/48846469
个人分类: uva
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭