唐鹏程吧 关注:9贴子:128
  • 2回复贴,共1

在贴吧上看到的一道c++题试着做了一下,效果不好。

只看楼主收藏回复


#include<iostream>
struct student//学生
{
long
stu_id;//学号
int
stu_score[5];//成绩
int
total_score;//总分
int
total_ranking; //总分排名
};
int n=0;
void enter(student&);//录入成绩
void count_total(student&);//计算总分和平均分并输出
void ranking(student a[]);//按学号和总分排名并输出
void query(const student&);//按输入的学号输出排名和成绩
void count_per(const student a[]);//按等次统计百分比并输出
void output(const student&);//输出学生信息
int main()
{
using
namespace std;
cout<<"my
name is tpc";
cout<<"欢迎使用:\n请输入a录入成绩\n成绩录入后输入b计算总分和平均分\n输入c输出按学号和总分的排名\n输入d进行学号输入查询\n输入e进行优等排名统计\n输入f输出所有学生信息\n输入q退出程序。";
char h;
cin>>h;
student information[50]={{}};
while(h!='q')
{
if(h!='a'&&h!='b'&&h!='c'&&h!='d'&&h!='e'&&h!='f')
{
cout<<"请输入正确的数字:";
cin.clear();
while(cin.get()!='\n')
continue;
cin>>h;
continue;
}
char x='a';
int count=0;
switch(h)
{
case 'a' :
while(x!='q'or count==50)
{
enter(information[count]);
count++;
cout<<"输入a继续输入,输入q结束输入:";
cin>>x;
}
cout<<"录入完毕!"<<endl;break;
case 'b':
for(int i=0;i<n;i++)
{
count_total(information[i]);
cout<<endl;
}
break;
case 'c': ranking(information);break;
case 'd':
cout<<"请输入学号:";long id;
while(cin>>id)
{
for(int
i=0;i<n;i++)
{
if(information[i].stu_id==id)
query(information[i]);
I


IP属地:四川1楼2013-03-06 22:14回复
    }
    cout<<"继续输入要查询的学号并输入q结束输入:";
    }
    cin.clear();
    while(cin.get()!='\n')
    continue;
    break;
    case 'e': count_per(information);break;
    case 'f': for(int
    i=0;i<n;i++)output(information[i]);break;
    }
    cout<<"欢迎使用:\n成绩录入后输入b计算总分和平均分\n输入c输出按学号和总分的排名\n输入d进行学号输入查询\n输入e进行优等排名统计\n输入f输出所有学生信息\n输入q退出程序。";
    cin>>h;
    }
    cout<<"谢谢使用!";
    return 0;
    }
    void enter(student& a)
    {
    char
    b;
    using
    namespace std;
    cout<<"请输入学生的信息按照学号 语文成绩 数学成绩 英语成绩 物理成绩 化学成绩输入并以空格隔开输入q结束输入:";
    if(cin>>a.stu_id)
    {
    for(int
    i=0;i<5;i++)
    cin>>a.stu_score[i];
    cout<<"录入成功!";
    n++;
    }
    else
    {
    cout<<"录入失败!请重新输入:";
    cin.clear();
    while(cin.get()!='\n')
    continue;
    enter(a);
    }
    }
    void count_total(student& a)
    {
    using
    namespace std;
    int
    average=0;
    for(int
    i=0;i<5;i++)
    a.total_score+=a.stu_score[i];
    average=a.total_score/5;
    cout<<"学号为"<<a.stu_id<<"的学生总分为:"<<a.total_score<<"
    平均分为:"<<average;
    }
    void ranking(student a[])
    {
    using
    namespace std;
    cout<<"按学号输出排名:"<<"\n"<<"\t学号 语文成绩 数学成绩 英语成绩 物理成绩 化学成绩\n";
    for(int
    i=0;i<n-1;i++)
    {
    int
    t=i;
    for(int j=i+1;j<n;j++)
    {
    if(a[t].stu_id>a[j].stu_id)
    t=j;
    }
    student temp={};
    temp=a[i];
    a[i]=a[t];
    a[t]=temp;
    }
    for(int
    c=0;c<n;c++)
    cout<<"\t"<<a[c].stu_id<<"\t"<<a[c].stu_score[0]<<"\t"<<a[c].stu_score[1]<<"\t"<<a[c].stu_score[2]<<"\t"<<a[c].stu_score[3]<<"\t"<<a[c].stu_score[4]<<endl;
    cout<<"按总成绩排名输出排名:"<<endl<<"\t总成绩 学号 语文成绩 数学成绩 英语成绩 物理成绩 化学成绩\n";
    I


    IP属地:四川2楼2013-03-06 22:14
    回复

      for(int
      i=0;i<n-1;i++)
      {
      int
      t=i;
      for(int j=i+1;j<n;j++)
      {
      if(a[t].total_score>a[j].total_score)
      t=j;
      }
      student temp={};
      temp=a[i];
      a[i]=a[t];
      a[t]=temp;
      a[i].total_ranking=i+1;
      }
      a[n-1].total_ranking=n;
      for(int
      c=0;c<n;c++)
      cout<<"\t"<<a[c].total_score<<"\t"<<a[c].stu_id<<"\t"<<a[c].stu_score[0]<<"\t"<<a[c].stu_score[1]<<"\t"<<a[c].stu_score[2]<<"\t"<<a[c].stu_score[3]<<"\t"<<a[c].stu_score[4]<<endl;
      }
      void query(const student& b)
      {
      using
      namespace std;
      cout<<"总分排名及各科成绩:";
      cout<<"\t"<<b.total_ranking<<"
      ";
      for(int
      i=0;i<5;i++)
      cout<<b.stu_score[i]<<"
      ";
      cout<<endl;
      }
      void count_per(const student a[])
      {
      using
      namespace std;
      int
      fine[5]={0},good[5]={0},middle[5]={0},pass[5]={0},fail[5]={0};
      for(int
      j=0;j<5;j++)
      {
      for(int i=0;i<n;i++)
      {
      if(a[i].stu_score[j]>=90)
      fine[j]++;
      else if(a[i].stu_score[j]>=80)
      good[j]++;
      else if(a[i].stu_score[j]>=70)
      middle[j]++;
      else if(a[i].stu_score[j]>=60)
      pass[j]++;
      else fail[j]++;
      }
      }
      cout<<"语文 数学 英语 物理 化学优等生人数及百分比分别为:\n";
      for(int x=0;x<5;x++)
      cout<<"\t"<<fine[x]<<" 占"<<fine[x]*100/n<<"%"<<"
      ";
      cout<<endl;
      cout<<"语文 数学 英语 物理 化学优良生人数及百分比分别为:\n";
      for(int x=0;x<5;x++)
      cout<<"\t"<<good[x]<<" 占"<<good[x]*100/n<<"%"<<"
      ";
      cout<<endl;
      cout<<"语文 数学 英语 物理 化学中等人数及百分比分别为:\n";
      for(int x=0;x<5;x++)
      cout<<"\t"<<middle[x]<<" 占"<<middle[x]*100/n<<"%"<<"
      ";
      cout<<endl;
      cout<<"语文 数学 英语 物理 化学及格人数及百分比分别为:\n";
      for(int x=0;x<5;x++)
      cout<<"\t"<<pass[x]<<" 占"<<pass[x]*100/n<<"%"<<"
      ";
      cout<<endl;
      cout<<"语文 数学 英语 物理 化学不及格人数及百分比分别为:\n";
      for(int x=0;x<5;x++)
      cout<<"\t"<<fail[x]<<" 占"<<fail[x]*100/n<<"%"<<"
      ";
      cout<<endl;
      }
      void output(const student& a)
      {
      using
      namespace std;
      cout<<"所有学生的信息:"<<endl<<"\t学号 语文成绩 数学成绩 英语成绩 物理成绩 化学成绩\n";
      for(int
      i=0;i<n;i++)
      {
      cout<<"\t"<<a.stu_id<<"
      ";
      for(int
      j=0;j<5;j++)
      cout<<a.stu_score[j]<<"
      ";
      cout<<endl;
      }
      }


      IP属地:四川3楼2013-03-06 22:14
      回复