学校学生成绩管理

来源:互联网 发布:windows type命令详解 编辑:程序博客网 时间:2024/04/28 21:18

学校学生成绩管理

假设一个学校成绩管理要管理的学生信息包括:班级、学号、姓名、学期、每门课程的成绩(每学期的课程门数是不一样的) ,对学生的成绩信息进行管理。

要求实现如下的功能:

实现:学生信息的录入;修改;删除和查询,按学期、学号、成绩不及格等查询。

⑴ 输入学生的成绩信息,包含学号、姓名、性别等基本信息和各课成绩

⑵ 显示全部学生各科成绩信息;

⑶ 对各科分班进行成绩统计分析(总分、平均分、最高分、最低分、及格率等);

⑷ 统计各科各分数段人数及班级信息;

⑸ 按学号或姓名查找并显示某个学生的各科成绩;

⑹ 按课程成绩或总分由高到低排序显示;

⑺ 更新某个学生的基本信息或课程成绩;

⑻ 设计一个菜单,具有上述规定的操作要求、退出系统等最基本的功能。


程序如下:

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<iomanip>
using namespace std;
class Tstu //通过定义一个类来定义数据录入的函数
{
  char name[20];
  int number;
  float math,cpp,english,average,sum;
public:
  Tstu()
  {};
  Tstu ( char n[20], int num,float ma,float cj,float eng ) ;
  float getsum();//得到总分的函数
  float getaver(); //得到平均分的函数
 friend void main(); //将主函数定义为友元函数,方便访问类中的成员
};
 
 Tstu::Tstu ( char n[20], int num,float ma,float cj,float eng )
 {
  strcpy(name,n); //得到姓名
  number=num; //得到学号
  math=ma; //得到数学成绩
  cpp=cj; //得到c++成绩
  english=eng; //得到英语成绩
 }
 
 float Tstu::getsum() //得到总分的函数
 {
  sum=cpp+english+math;
  return sum;
 }

 float Tstu::getaver() //得到平均分的函数
 {
  average=getsum()/3;
  return average;
 }

 void main()
{
 cout<<"*****************欢迎来到学生成绩查询系统*****************"<<endl;
 cout<<"请选择您需要的操作!"<<endl; //菜单的输出
 cout<<"操作:"<<endl;
 cout<<"(1)数据录入"<<endl;
 cout<<"(2)增加学生"<<endl;
 cout<<"(3)删除学生"<<endl;
 cout<<"(4)修改数据"<<endl;
 cout<<"查询:"<<endl;
 cout<<"(5)按总成绩查询"<<endl;
 cout<<"(6)按姓名查询"<<endl;
 cout<<"(7)输出所有学生的数据"<<endl;
 cout<<"成绩统计:"<<endl;
 cout<<"(8)每个学生的平均成绩"<<endl;
 cout<<"(9)各科不及格人数"<<endl;
 cout<<"排名:"<<endl;
 cout<<"(10)按总分查询排名"<<endl;
 cout<<"(11)退出"<<endl;
 cout<<"选择相关操作请输入相对的括号里的阿拉伯数字及字母!"<<endl;
 int p=0; //全局变量,用于选择菜单时的输入 
 char w;//全局变量,用于在大项中选择是否继续操作
 Tstu *s[100]; //类定义的指针将所录入的数据按类中的形式分别存储
 ofstream *file[100];
 int i=0; //全局变量,用于做每个录入数据的下标
 int j=0;
 int chris1=0; //用于判断do-while语句
 do //判断输入是否正确
 {
  cin>>p;
  if((p>=1&&p<=11))
  chris1=1;
  else
  cout<<"指令错误!请重新输入:"<<endl;
 }while(chris1==0);
 do{
   switch(p)
   {
     case 1: //数据的录入
   {
           char c; //用于在小项中选择是否继续操作
           char name[20];
           int number;
           float math,cpp,english;
      do{
          cout<<"请输入姓名:"<<endl;
          cin>>name;
          cout<<"请输入学号:"<<endl;
          cin>>number;
          cout<<"请输入数学成绩:"<<endl;
          cin>>math;
          cout<<"请输入C++成绩:"<<endl;
          cin>>cpp;
          cout<<"请输入英语成绩:"<<endl;
          cin>>english;
          fstream outfile,infile; //作一个文本文档在文件夹中用于显示所录入的数据
          outfile.open("f1.txt",ios::out|ios::app);
          if(!file)
{  cout<<"f1.txt can not open.\n";
             return;
 }
 outfile<<name<<"  "<<"学号:"<<number<<"  "<<"数学成绩:"<<math<<"  "<<"C++成绩:"<<cpp<<"  "<<"外语成绩:"<<english<<endl;
            outfile.close();
            infile.open("f1.txt",ios::in);
            if(!file) //判断文件是否打开
 {  cout<<"f1.txt can not open.\n";
              return;
}
            char ch;
            while(infile.get(ch))
            cout<<ch;
            cout<<endl;
            infile.close();
            j++;
            s[i]=new Tstu(name, number,math, cpp, english);
            i++;
            cout<<"数据录入成功,想继续录入吗(y/n)"<<endl;
            cin>>c;
            chris1=0;
            do{
            if(c!='y'&&c!='n')
 { cout<<"指令错误!请重新输入!"<<endl;
             cin>>c;
}
            else
            chris1=1;
   }while(chris1==0);
   }while(c=='y'); //重复输入
   break;
   }
   case 2: //增加学生数据
    {
               char name[20];
               int number;
               float math,cpp,english;
               char c;
            do
   {
              cout<<"请输入您要增加的学生的姓名:"<<endl;
              cin>>name;
              cout<<"请输入你要增加的学生的学号:"<<endl;
              cin>>number;
              cout<<"请输入数学成绩:"<<endl;
              cin>>math;
              cout<<"请输入C++成绩:"<<endl;
              cin>>cpp;
              cout<<"请输入英语成绩:"<<endl;
              cin>>english;
              fstream outfile,infile;
              outfile.open("f1.txt",ios::out|ios::app);
              if(!file)
     {
                cout<<"f1.txt can not open.\n";
                return;
     }
               outfile<<name<<"  "<<"学号:"<<number<<"  "<<"数学成绩:"<<math<<"  "<<"C++成绩:"<<cpp<<"  "<<"外语成绩:"<<english<<endl;
               outfile.close();
               infile.open("f1.txt",ios::in);
              if(!file)
     {
                cout<<"f1.txt can not open.\n";
                return;
     }
                char h;
                while(infile.get(h))
                cout<<h;
                cout<<endl;
                infile.close();
                j++;
                s[i]=new Tstu(name,number, math, cpp, english);
                i++;
                cout<<"数据录入成功,想继续录入吗(y/n)"<<endl;
                cin>>c;
                if(c!='y'&&c!='n')
    {
                     cout<<"指令错误!请重新输入!"<<endl;
                     cin>>c;
    }

   }while(c=='y');
             break;
    }
   case 3://删除学生信息
    {
      char c;
                  string xingming,line,str,name;
      do
      {   ifstream fin("f1.txt");
                      fstream outfile("f2.txt",ios::trunc|ios::out); //作一个文本文档在文件夹中用于显示所录入的数据
                      cout<<"请输入您要删除的学生的姓名:"<<endl;         
                      cin>>xingming;
       while(!fin.eof() )
       {
        getline(fin,line);
                          istringstream stream(line);
                          name = line.substr(0,line.find(" ",0));//找到名字,判断是否相等
                          if(name != xingming)
                          outfile<<line<<endl;
       }
                      outfile.close();
                      fin.close();
                      fstream f("f1.txt",ios::trunc|ios::out);
                      ifstream outf("f2.txt");
                      f<<outf.rdbuf();
                      outf.close();
                      f.close();
                      cout<<"数据删除成功,想继续删除吗(y/n)"<<endl;
                      cin>>c;
                      if(c!='y'&&c!='n')
       {
        cout<<"指令错误!请重新输入!"<<endl;
                          cin>>c;
       }
      }while(c=='y');
                   break;
    }
    
      case 4://修改数据
    {
      string sname,line,str,name;
    int nu;float eng,ma,com;int flag;char c;
    if(i==0)
    {
    cout<<"系统中没有输入数据,请先输入数据!"<<endl;break;
    }
    do
     {
      flag=0;
      cout<<"请输入您要修改的学生的姓名:";
      cin>>name;
      fstream fin("f1.txt",ios::in);
      ofstream outfile("f.txt",ios::trunc|ios::out);
      cout<<endl;
      while(!fin.eof() ) //在文件中查找
     {
       getline(fin,line);
      istringstream stream(line);
      sname = line.substr(0,line.find(" ",0));
      if(name!=sname)
      {
       outfile<<line<<endl;
      }
      else flag=1;
      }
      fin.close();
      if(flag=1)
      {
      cout<<"请输入修改学生姓名及其新学号,数学,c++和英语成绩:";
      cin>>name>>nu>>ma>>com>>eng;
      cout<<endl;
      outfile<<name<<"  "<<"学号:"<<nu<<"  "<<"数学成绩:"<<ma<<"  "<<"C++成绩:"<<com<<"  "<<"外语成绩:"<<eng<<endl;
      outfile.close();
      fstream outf("f1.txt",ios::trunc|ios::out);
      fstream inf("f.txt",ios::in);
      outf<<inf.rdbuf()<<endl;
      outf.close();
      inf.close();
      }
      if(flag=0)cout<<"对不起,您要修改的学生不存在!请查证后输入!"<<endl;
         cout<<"想继续修改吗(y/n):";
      cin>>c;
      cout<<endl;
      if(c!='y'&&c!='n')
      {
      cout<<"  指令错误!请重新输入!"<<endl<<"     ";
      cin>>c;
      }
    }while(c=='y');
    break;
    }
   case 5://按总分查询
    {
     int j=0;float SUM;char c;int flag;
     if(i==0)
    {
    cout<<"系统中没有输入数据,请先输入数据!"<<endl;break;
    }
     do
     {
      flag=0;
      cout<<"请输入您要查询的学生的总成绩:"<<endl;
      cin>>SUM;
      for(int j=0;j<i;j++)
      {
       if(s[j]->getsum()==SUM)
       {
       flag=1;
       cout<<"您要查询的学生是:"<<(*s[j]).name<<endl;
       cout<<"该生的信息为:"<<endl;
       cout<<"  学号:"<<(*s[j]).number<<"  数学分数:"<<(*s[j]).math<<"  c++分数:"<<(*s[j]).cpp<<"  英语分数"<<(*s[j]).english<<endl;
      }
      }
     if(flag==0)cout<<"对不起!您要查询的学生不存在!"<<endl;
     cout<<"是否继续查?(y/n):";
     cin>>c;
     if(c!='y'&&c!='n')
     {
      cout<<"指令错误!请重新输入!"<<endl;
     
      cin>>c;
     };
     }while(c=='y');
     break;
    }
   case 6://按姓名查询
    {
     char n[20];char c;int j=0;int flag;
     if(i==0)
    {
    cout<<"系统中没有输入数据,请先输入数据!"<<endl;break;
    }
     do
     {
      flag=0;
      cout<<"请输入要查询的名字: "<<endl;
      cin>>n;
      for(int j=0;j<i;j++)
      {
      if(strcmp(n,(*s[j]).name)==0)
      {
       flag=1;
       cout<<"您要查询的学生是:"<<(*s[j]).name<<endl;
       cout<<"该生的信息是:学号:"<<(*s[j]).number<<"  数学分数:"<<(*s[j]).math<<"c++分数:"<<(*s[j]).cpp<<"英语分数:"<<(*s[j]).english<<endl;
      }
      }
      if(flag==0)
      cout<<"对不起您要查询的学生不存在,请查证后再输入!"<<endl;
      cout<<"您想继续查询吗?(y/n):";
      cin>>c;
      if(c!='y'&&c!='n')
      {
       cout<<"指令错误!请重新输入:"<<endl;
       cin>>c;
      }
     }while(c=='y');
     break;
    }
   case 7://输出所有学生数据
    {
     cout<<"所有学生数据如下:"<<endl;
     ifstream file;
                 file.open("f1.txt",ios::in);
           cout<<file.rdbuf();
           file.close();
           break;
    }
   case 8://统计每个学生的平均成绩
    {  
                 if(i==0)
    {
      cout<<"系统中没有输入数据,请先输入数据!"<<endl;break;
    }
     for(int m=0;m<i;m++)
     {
      cout<<(*s[m]).name<<"  学号:"<<(*s[m]).number<<"  总分:"<<(*s[m]).getsum()<<"  均分:"<<(*s[m]).getaver()<<endl;
     }
     break;
    }
   case 9://统计各学科不及格人数
    {
     if(i==0)
    {
      cout<<"系统中没有输入数据,请先输入数据!"<<endl;break;
    }
     int m,n,k,j;m=0;n=0;k=0;
     for(j=0;j<i;j++)
     {
      if(s[j]->math<60)m++;
      if(s[j]->cpp<60)n++;
      if(s[j]->english<60)k++;
     }
     cout<<"数学不及格人数为:"<<m<<endl;
     cout<<"c++不及格人数为:"<<n<<endl;
     cout<<"英语不及格人数为:"<<k<<endl;
     break;
    }
      case 10://按总分排名      
    {
     if(i==0)
    {
    cout<<"系统中没有输入数据,请先输入数据!"<<endl;break;
    }
     int h,g,n;int j=0;float a[100],temp;
     for(g=0;g<i;g++)
     {
      a[j]=(*s[g]).getsum();j++;
     }
                 for(h=0;h<i;h++)
     {for(n=0;n<i-h;n++)
      if(a[n]>a[n+1])
      {
         temp=a[n];a[n]=a[n+1];a[n+1]=temp;
      }
     }
     for(n=1;n<=i;n++)
      cout<<a[n]<<endl;
       break;
    }
}
         cout<<"  您想继续进行其他操作吗?(y/n):";
         int flag=0;
   do
   {
       cin>>w;
       cout<<endl;
       if(w!='y'&&w!='n')
       cout<<"  指令错误!请重新输入!"<<endl;
       else
       flag=1;
   }while(flag==0);
   if(w=='y')
   {
    cout<<"*****************欢迎来到学生成绩查询系统*****************"<<endl;
       cout<<"请选择您需要的操作!"<<endl; //菜单的输出
       cout<<"操作:"<<endl;
       cout<<"(1)数据录入"<<endl;
       cout<<"(2)增加学生"<<endl;
       cout<<"(3)删除学生"<<endl;
       cout<<"(4)修改数据"<<endl;
       cout<<"查询:"<<endl;
       cout<<"(5)按总成绩查询"<<endl;
       cout<<"(6)按姓名查询"<<endl;      
 cout<<"(7)输出所有学生的数据"<<endl;
       cout<<"成绩统计:"<<endl;
       cout<<"(8)每个学生的平均成绩"<<endl;
       cout<<"(9)各科不及格人数"<<endl;
       cout<<"排名:"<<endl;
       cout<<"(10)按总分查询排名"<<endl;
       cout<<"(11)退出"<<endl;
       cout<<"选择相关操作请输入相对的括号里的阿拉伯数字及字母!"<<endl;
    cin>>p;
   }
   }while(w=='y');
}







0 0
原创粉丝点击