/*
* 程序的版權和版本聲明部分
* 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;
}