#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class student
{
private:
string name;
double cpp;
double math;
double english;
double zong;
public:
student(){};
student(string nam,double c,double m,double e):name(nam),cpp(c),math(m),english(e){zong=c+m+e;}
void set(string nam,double c,double m,double e);
string get_name(){return name;}
double get_cpp(){return cpp;}
double get_math(){return math;}
double get_english(){return english;}
double get_zong(){return zong;}
void set_cpp(double c){cpp=c;}
void set_math(double m){math=m;}
void set_english(double e){english=e;}
void set_zong(double z){zong=z;}
};
void student::set(string nam,double c,double m,double e)
{
name=nam;
cpp=c;
math=m;
english=e;
zong=c+e+m;
}
int main()
{
student a[100],t;
int i,j;
string Name;
double Cpp,Math,English;
ifstream infile("score.dat",ios::in);
if(!infile)
{
cerr<<"open error"<<endl;
exit(1);
}
for(i=0;i<10;i++)
{
infile>>Name>>Cpp>>Math>>English;
a[i].set(Name,Cpp,Math,English);
}
infile.close();
student b("nobody",0,0,0);
for(i=0;i<100;i++)
{
if(a[i].get_cpp()>b.get_cpp())
{
b.set_cpp(a[i].get_cpp());
}
if(a[i].get_math()>b.get_math())
{
b.set_math(a[i].get_math());
}
if(a[i].get_english()>b.get_english())
{
b.set_english(a[i].get_english());
}
if(a[i].get_zong()>b.get_zong())
{
b.set_zong(a[i].get_zong());
}
}
for(j=0;j<100-2;j++)
for(i=0;i<99-j;i++)
if(a[i].get_zong()>a[i+1].get_zong())
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
cout<<"C++最高分为: "<<b.get_cpp()<<endl;
cout<<"高等数学最高分为: "<<b.get_math()<<endl;
cout<<"英语最高分为: "<<b.get_english()<<endl;
cout<<"总分最高分为: "<<b.get_zong()<<endl;
ofstream outfile("ordered_salary.txt",ios::out);
if(!outfile)
{
cout<<"open error"<<endl;
exit(1);
}
for(i=0;i<100;i++)
{
outfile<<a[i].get_name()<<" ";
outfile<<a[i].get_cpp()<<" ";
outfile<<a[i].get_math()<<" ";
outfile<<a[i].get_english()<<" ";
outfile<<a[i].get_zong()<<endl;
}
outfile.close();
system("pause");
return 0;
}
C++最高分为: 98
高等数学最高分为: 98
英语最高分为: 96
总分最高分为: 270
请按任意键继续. . .
这次的任务使我认识到自己的不足,并没有真正的掌握所学的的知识,此次任务中所犯的错误就是,忘记了私有成员是无法在主函数中被调用。