《第十六周实验报告任务2——学生成绩排序》

来源:互联网 发布:小马搜索软件下载 编辑:程序博客网 时间:2024/05/16 06:36

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:    学生成绩排序                         
* 作    者:       刘江波                      
* 完成日期:    2012     年   6    月   5     日
* 版 本 号:     v.09    

* 对任务及求解方法的描述部分
* 问题描述: 

【任务2】学生成绩排序
文件score.dat 中保存的是100 名学生的姓名和C++课、高数和英语成绩。
(1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总分、均分数据成员,成员函数根据
需要确定。
(2)读入这名学生的成绩,用对象数组进行存储。
(3)求出各科和总分的最高分。
(4)请按总分的降序(高成绩在前,低成绩在后)排序
(5)在屏幕上显示各科及总分的最高分,排序后的成绩单(包括总分)保存到文件odered_score.dat
中。
* 程序头部的注释结束
*/

#include<iostream>   #include<string>   #include<fstream>   using namespace std;    class Student  {  public: Student(){};      Student(string nam, double c, double m, double e):name(nam),score_cpp(c),score_math(m),score_English(e){score_all=c+m+e;}     double all_score();        void write_score(ifstream &in);      void read_score(ofstream &out);  string get_name(){return name;}      double get_cpp(){return score_cpp;}      double get_math(){return score_math;}      double get_english(){return score_English;}      double get_total(){return score_all;}      void set_cpp(double c){score_cpp=c;}      void set_math(double m){score_math=m;}      void set_english(double e){score_English=e;}      void set_total(double t){score_all=t;}  private:      string name;      double score_cpp;      double score_math;      double score_English;      double score_all;    };  void writefile(Student * s, int num);void readfile(Student * s, int num);void bubble_sort(Student * s, int num);double Student::all_score()  {      this->score_all = this->score_cpp + this->score_math + this->score_English;      return this->score_all;  }    void Student::write_score(ifstream &in)  {      in >> this->name >> this->score_cpp >> this->score_math >> this->score_English;  }    void Student::read_score(ofstream &out)  {      out << this->name << '\t' << this->score_cpp << '\t' << this->score_math << '\t' << this->score_English << endl;  }    void writefile(Student * s, int num)  {      ifstream infile("score.dat",ios::in);        if(!infile)      {          cerr << "open error!" << endl;          exit(1);      }        for(int i = 0; i < 100; ++i)      {          s[i].write_score(infile);      }      infile.close();  }    void readfile(Student * s, int num)  {      ofstream outfile("odered_score.txt",ios::out);        if(!outfile)      {          cerr << "open error!" << endl;          exit(1);      }        for(int i = 0; i < 100; ++i)      {          s[i].read_score(outfile);      }      outfile.close();  }      void bubble_sort(Student * s, int num)  {      Student t;        for(int j = 0; j < num-1; j++)      {          for(int i = 0; i < num-1-j; ++i)          {              if(s[i].all_score() <= s[i+1].all_score())              {                  t = s[i];                  s[i] = s[i+1];                  s[i+1] = t;              }          }      }    }    int main()  {      Student stud[100];        writefile(stud, 100); Student max_stud("nobody",0,0,0);  //max_stud是一个不存在的学生,存储最高分 for(int i = 0; i < 100; ++i)      {          if(stud[i].get_cpp()>max_stud.get_cpp()) max_stud.set_cpp(stud[i].get_cpp());          if(stud[i].get_math()>max_stud.get_math()) max_stud.set_math(stud[i].get_math());          if(stud[i].get_english()>max_stud.get_english()) max_stud.set_english(stud[i].get_english()); stud[i].all_score();        if(stud[i].get_total()>max_stud.get_total()) max_stud.set_total(stud[i].get_total());          }       //显示各科及总分的最高分       cout<<"C++最高分为: "<<max_stud.get_cpp()<<endl;      cout<<"高等数学最高分为: "<<max_stud.get_math()<<endl;      cout<<"英语最高分为: "<<max_stud.get_english()<<endl;      cout<<"总分最高分为: "<<max_stud.get_total()<<endl;      cout<<"请到文件ordered_score.txt中查看排序后的结果"<<endl;      bubble_sort(stud, 100);      readfile(stud, 100);        system("pause");      return 0;  }  


 

原创粉丝点击