学生成绩管理C++版

时间:2023-03-08 17:11:42

【标题】学生成绩管理的设计与实现

【开发语言】C++

【主要技术】STL

【概要设计】类名:student

      类成员:No、Name、Math、Eng、Chn、Cpro、Sum

      成员函数:getname、getno、getsum

【基本功能】使用list容器实现对学生成绩类的基本操作:增加、删除、查询、排序

【测试数据】功能测试:按提示输入5组正确的正确的数据和5组有问题的数据,查看程序能否运行正确

      性能测试:随机生成1、5、10、15万条数据,查看程序完成按总分排序所用的时间及打印完成的时间

【测试结果】功能测试:基本功能运行正确,没有进行异常处理

      性能测试:

数据量(万条) 1 5 10 15
排序所用时间(秒) 1.7 25.9 02:35.0 07:49.9
完成打印所用时间(秒) 21.2 01:59.9 05:40.7 11:43.5
排序所需内存(K) 640 3132 6264 9388

  

C语言版随机生成等量的数据情况对比:

数据量(万条) 1 5 10 15
排序所用时间(秒) 0.7 12.2 02:21.6 7:10.7
完成打印所用时间(秒) 8.0 48.7 03:33.5 09:10.7
排序所需内存(K) 1064 4700 9392 14050

 

 

  对比结果:使用C语言实现排序和打印所用时间少,消耗内存更多(C语言版地址:http://www.cnblogs.com/forerve/p/4177245.html)

【详细设计】

 #include<iostream>
 #include<string>
 #include<list>
 #include<algorithm>
 using namespace std;

 class student{
 private:
     string No;
     string Name;
     double Math;
     double Eng;
     double Chn;
     double Cpro;
     double Sum;
 public:
         student(string no, string name, double math, double eng,
                 double chn, double cpro){
                 No = no;
                 Name = name;
                 Math = math;
                 Eng = eng;
                 Chn = chn;
                 Cpro = cpro;
                 Sum = math + eng + chn + cpro;
                 }

         friend ostream& operator <<(ostream& out, student& S)
         {
             out << "\t" << S.No << "\t" << S.Name << "\t" << S.Math << "\t"
              << S.Eng << "\t" << S.Chn << "\t" << S.Cpro << "\t" << S.Sum;
             return out;
         }
         const string getno()
         {
            return No;
         }
         const string getname()
         {
             return Name;
         }
         const double getsum()
         {
             return Sum;
         }
          ~student(){

          }
 };

 void main_remid(); /*输出主要提示信息 */
 void score_remind(); /*输出成绩提示信息*/
 int add(list<student> &lst); /*增加学生*/
 int find(list<student> &lst); /*查询学生信息*/
 int del(list<student> &lst); /*删除学生*/
 void sort_sum(list<student> &lst); /*按总成绩降序打印学生成绩*/
 void sort_no(list<student> &lst); /*按学号升序打印学生成绩*/
 bool cmp_no(student& st1, student& st2); /*用于sort的比较函数*/

 int main()
 {
     list<student> lst; /*使用list容器存储学生信息*/
     char op = ' ';
     main_remid();
     while(op != 'q')
     {
         cin >> op;
         switch(op)
         {
             ':
                 sort_no(lst);
                 break;
             ':
                 add(lst);
                 break;
             ':
                 find(lst);
                 break;
             ':
                 del(lst);
                 break;
             ':
                 sort_sum(lst);
                 break;
             ':
                 main_remid();
                 break;
             default:
                 cout << "输入指令未知,请重新输入" << endl;
                 break;
         }
         if(op != 'q')
             cout << " 请继续选择您想要的操作:" << endl;
     }
     ;
 }

 /*增加学生*/
 int add(list<student> &lst)
 {
     string No;
     string Name;
     double Math;
     double Eng;
     double Chn;
     double Cpro;
     cout << " 请输入要增加学生的学号:" << endl;
      cin >> No;
      for(list<student>::iterator it=lst.begin(); it!=lst.end(); it++)
          if(No == it->getno()){
              cout << "添加失败,此学号已存在,请重新操作" << endl;
              ;
          }
      cout << " 请输入要增加学生的姓名" << endl;
      cin >> Name;
      cout << " 请输入要增加学生的数学成绩:" << endl;
      cin >> Math;
      cout << " 请输入要增加学生的英语成绩:" << endl;
      cin >> Eng;
      cout << " 请输入要增加学生的语文成绩:" << endl;
      cin >> Chn;
      cout << " 请输入要增加学生的C语言成绩:" << endl;
      cin >> Cpro;
      student *st = new student(No, Name, Math, Eng, Chn, Cpro);
      lst.push_back(*st);
 }

 /*查询学生信息*/
 int find(list<student> &lst)
 {
     cout << "请输入要查询学生的学号:" << endl;
     string no;
     cin >> no;
     for(list<student>::iterator it=lst.begin(); it!=lst.end(); it++)
     {
         if(no == it->getno())
         {
             score_remind();
             cout << *it << endl;
             ;
         }
     }
         cout << "不存在此学号,请重新选择操作" << endl;
 }

 /*删除学生*/
 int del(list<student> &lst)
 {
     cout << " 请输入要删除学生的学号:" << endl;
     string no;
     string name;
     cin >> no;
     for(list<student>::iterator it=lst.begin(); it!=lst.end(); it++)
         if(no == it->getno())
         {
             no = it->getno();
             name = it->getname();
             lst.erase(it);
             cout << "学生" << no << " " << name << "删除成功" << endl;
             ;
         }
     cout << " 删除失败,不存在此学号" << endl;
 }

 /*按学号升序打印学生成绩*/
 void sort_no(list<student> &lst)
 {
     list<student> temp;
     temp.push_front(*lst.begin());
     for(list<student>::iterator it=++lst.begin(); it!=lst.end(); it++)
     {
         list<student>::iterator jt = temp.begin();
         while(jt!=temp.end() && strcmp(it->getno().c_str(), jt->getno().c_str()))
             jt++;
         temp.insert(jt, *it);
     }
     score_remind();
     for(list<student>::iterator it=temp.begin(); it!=temp.end(); it++)
     {
         cout << *it << endl;
     }
 }

 /*用于sort的比较函数*/
 /*bool cmp_no(const student& st1, const student& st2)
 {
     return st1.getno() < st2.getno();
 }*/

 /*按成绩升序打印学生成绩*/
 void sort_sum(list<student> &lst)
 {
     list<student> temp;
     temp.push_front(*lst.begin());
     for(list<student>::iterator it=++lst.begin(); it!=lst.end(); it++)
     {
         list<student>::iterator jt = temp.begin();
         while(jt!=temp.end() && it->getsum()<jt->getsum())
             jt++;
         temp.insert(jt, *it);
     }
     score_remind();
     for(list<student>::iterator it=temp.begin(); it!=temp.end(); it++)
     {
         cout << *it << endl;
     }
 }

 /*输出主要提示信息 */
 void main_remid()
 {
     cout << "\t\t\t学生成绩类" << endl << endl;
     cout << "\t\t1.查询所有学生的成绩信息" << endl;
     cout << "\t\t2.增加学生" << endl;
     cout << "\t\t3.查找学生" << endl;
     cout << "\t\t4.删除学生" << endl;
     cout << "\t\t5.查看总分排名" << endl;
     cout << "\t\t6.查看提示" << endl;
     cout << "\t\tq.退出系统" << endl << endl;
 }

 /*输出成绩提示信息*/
 void score_remind()
 {
     cout << "\t\t\t 学生成绩信息" << endl << endl;
     cout << "\t学号\t" << "姓名\t" << "数学\t" << "英语\t"
     << "语文\t" << "C语言\t" << "总成绩" << endl;
 }

学生成绩管理C++版