/*
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:C++第十六周【任务二】
* 作 者: 李洪悬
* 完成日期: 2012 年 6 月 4 日
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:程序解析
* 程序输出:
*/
【任务2】学生成绩排序
文件score.dat 中保存的是100 名学生的姓名和C++课、高数和英语成绩。
(1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总分、均分数据成员,成员函数根据需要确定。
(2)读入这名学生的成绩,用对象数组进行存储。
(3)求出各科和总分的最高分。
(4)请按总分的降序(高成绩在前,低成绩在后)排序
(5)在屏幕上显示各科及总分的最高分,排序后的成绩单(包括总分)保存到文件odered_score.dat
中。
#include<fstream> #include<string> #include<iomanip> #include<iostream> using Namespace std; class Student { public: Student(); Student(string Name, int Cpp,int Math, int English, int Allscore, double Average); void set_Name(string Name); void set_Cpp(int Cpp); void set_Match(int Math); void set_English(int English); void set_Allscore(int Allscore); void set_Average(double Average); string get_Name(); int get_Cpp(); int get_Math(); int get_English(0; int get_Allscore(); double get_Average(); void Max_Cpp(Student s[]); void Max_Math(Student s[]) ; void Max_English(Student s[]); void Max_Allscore(Student s[]); void Order_score(Student s[]); void Allscore(Student s[]); void Average_score(Student s[]); private: string Name; int Cpp; int Math; int English; int Allscore; double Average; }; Student::Student() { Name = "unknown"; Cpp = 0; Math = 0; English = 0; Allscore = 0; Average = 0.0; } Student::Student(string Name, int Cpp, int Math,int English,int Allscore,double Average) { this->Name=Name; this->Cpp=Cpp; this->Math=Math; this->English=English; this->Allscore=Allscore; this->Average=Average; } void Student::set_Name(string Name) { this->Name = Name; } void Student::set_Cpp(int Cpp) { this->Cpp=Cpp; } void Student::set_Match(int Math) { this->Math=Math; } void Student::set_English(int English) { this->English=English; } void Student::set_Allscore(int Allscore) { this->Allscore=Allscore; } void Student::set_Average(int Average) { this->Average=Average; } string Student::get_Name(); { return Name; } int Student::get_Cpp() { return Cpp; } int Student::get_Math() { return Math; } int Student::get_English() { return English; } int Student::get_Allscore(); { return Allscore; } double Student::get_Average() { return Average; } void input_student(Student s[]) { int Cpp,Math,English; string Name; ifstream infile("score.dat",ios::in); if (!infile) { cerr<<"open error!"<<endl; exit(1); } for (int i=0;i<100;i++) { infile>>Name; s[i].set_Name(Name); infile>>Cpp; s[i].set_Cpp(Cpp); infile>>Math; s[i].set_Math(Math); infile>>English; s[i].set_English(English); } infile.close(); } void Order_score(Student s[]) { ofstream outfile("ordered_student.dat",ios::out); if(!outfile) { cerr<<"open error!"<<endl; exit(1); } for(int i=0;i<100;i++) outfile<<s[i].get_Name()<<"\t"<<s[i].get_Cpp()<<"\t"<<s[i].get_Math()<<"\t"<<s[i].get_English()<<"\t"<<s[i].get_Allscore()<<"\t"<<setiosflags(ios::fixed)<<setprecision(2)<<s[i].get_Average()<<'\n'; outfile.close(); return ; } void Max_Cpp(Student s[]) { Student student; int score; int i=0,j=0; string Name; score=s[i].get_Cpp(); student.set_Cpp(score); for(i=0;i<99;++i) { if(student.get_Cpp()<s[i+1].get_Cpp()) { score=s[i+1].get_Cpp(); student.set_Cpp(score); Name=s[i+1].get_Name(); student.set_Name(Name); } } cout<<student.get_Cpp()<<'\n'; cout<<endl; cout<<"获得C++最高分的这几名同学叫:"; for(i=0;i<99;++i) { if(student.get_Cpp()==s[i].get_Cpp()) cout<<s[i].get_Name()<<'\t'; } cout<<'\n'; return ; } void Max_Math(Student s[]) { Student student; int score,i=0; string Name; score=s[i].get_Math(); student.set_Math(score); for(i=0;i<99;++i) { if(student.get_Math()<s[i+1].get_Math()) { score=s[i+1].get_Math(); student.set_Math(score); Name=s[i+1].get_Name(); student.set_Name(Name); } } cout<<student.get_Math()<<'\n'; cout<<endl; cout<<"获得高数最高分的这几名同学叫:"; for(i=0;i<99;++i) { if(student.get_Math()==s[i].get_Math()) cout<<s[i].get_Name()<<'\t'; } cout<<'\n'; return ; } void Max_English(Student s[]) { Student student; int score,i=0; string Name; score=s[i+1].get_English(); student.set_English(score); for(i=0;i<99;++i) { if(student.get_English()<s[i+1].get_English()) { score=s[i+1].get_English(); student.set_English(score); Name=s[i+1].get_Name(); student.set_Name(Name); } } cout<<student.get_English()<<'\n'; cout<<endl; cout<<"获得英语最高分的这几名同学叫:"; for(i=0;i<99;++i) { if(student.get_English()==s[i].get_English()) cout<<s[i].get_Name()<<'\t'; } cout<<'\n'; return ; } void Max_Allscore(Student s[]) { Student student; int score,i=0; string Name; score=s[i].get_Allscore(); student.set_Allscore(score); for(i=0;i<99;++i) { if(student.get_Allscore()<s[i+1].get_Allscore()) { score=s[i+1].get_Allscore(); student.set_Allscore(score); Name=s[i+1].get_Name(); student.set_Name(Name); } } cout<<student.get_Allscore()<<'\n'; cout<<endl; cout<<"获得总分最高分的这几名同学叫:"; for(i=0;i<99;++i) { if(student.get_Allscore()==s[i].get_Allscore()) cout<<s[i].get_Name()<<'\t'; } cout<<'\n'; return ; } int main() { Student s1[100],s2; input_student(s1);//读入人的原始分数 Allscore(s1); Average_score(s1); cout<<"C++的最高分为:"; Max_Cpp_(s1); cout<<endl; cout<<"高数的最高分为:"; Max_Math(s1); cout<<endl; cout<<"英语的最高分为:"; Max_Englishe(s1); cout<<endl; cout<<"总分的最高分为:"; Max_Allscore(s1); cout<<endl; Order_score(s1); ordered_student_dat(s1); cout<<endl; system("PAUSE"); return 0; }