作品名:学生成绩
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
class Student
{
private:
string name;
float Cscore;
float Mscore;
float Escore;
float Tscore;
float Ascore;
public:
Student( float c, float m, float e, float t, float a, string nam);
Student();
void setname(string na){name = na;}
void setCscore(float c){Cscore = c;}
void setMscore(float m){Mscore = m;}
void setEscore(float e){Escore = e;}
void setTscore(float t){Tscore = t;}
void setAscore(float a){Ascore = a;}
string getname(){return name;}
float getCscore(){return Cscore;}
float getMscore(){return Mscore;}
float getEscore(){return Escore;}
float getTscore(){return Tscore;}
float getAscore(){return Ascore;}
friend void readfile(Student s[]);
friend void Cmaxscore(Student s[]);
friend void Mmaxscore(Student s[]);
friend void Emaxscore(Student s[]);
friend void Tmaxscore(Student s[]);
friend void array_score(Student s[]);
//friend void show_max(Student s[]);
friend void writefile(Student s[]);
};
Student::Student()
{
name = " ";
Cscore = 0;
Mscore = 0;
Escore = 0;
Tscore = 0;
Ascore = 0;
}
Student::Student( float c, float m, float e, float t, float a, string nam)
{
name = nam;
Cscore = c;
Mscore = m;
Escore = e;
Tscore = t;
Ascore = a;
}
void readfile(Student s[]) //读文件
{
string na;
float cscore;
float mscore;
float escore;
float tscore;
float ascore;
ifstream readfile("score.dat", ios::in);
if(! readfile)
{
cerr << "open errror" << endl;
exit(1);
}
for(int i = 0; i < 100; i++)
{
readfile >> na >> cscore >> mscore >> escore;
s[i].setname(na) ;
s[i].setCscore(cscore);
s[i].setMscore(mscore);
s[i].setEscore(escore);
tscore = s[i].Cscore + s[i].Escore + s[i].Escore ;
s[i].setTscore(tscore);
ascore = s[i].Tscore / 3;
s[i].setAscore(ascore);
}
readfile.close();
}
void Cmaxscore(Student s[]) //C++的最高分
{
Student t = s[0];
float max = s[0].Cscore;
for(int i = 1; i < 100; i++)
{
if(max < s[i].Cscore)
{
max = s[i].Cscore;
t = s[i];
}
}
cout << "C++成绩最高的是:" << max << setw(18) << "该同学是:" << t.name <<endl;
}
void Mmaxscore(Student s[]) //高数的最高分
{
Student t = s[0];
float max = s[0].Mscore;
for(int i = 1; i < 100; i++)
{
if(max < s[i].Mscore)
{
max = s[i].Mscore;
t = s[i];
}
}
cout << "高数成绩最高的是:" << max << setw(17) << "该同学是:" << t.name << endl;
}
void Emaxscore(Student s[]) //英语的最高分
{
Student t = s[0];
float max = s[0].Escore;
for(int i = 1; i < 100; i++)
{
if(max < s[i].Escore)
{
max = s[i].Escore;
t = s[i];
}
}
cout << "英语成绩最高的是:" << max << setw(17) << "该同学是:" << t.name <<endl;
}
void Tmaxscore(Student s[]) //总成绩的最高分
{
Student t = s[0];
float max = s[0].Tscore;
for(int i = 1; i < 100; i++)
{
if(max < s[i].Tscore)
{
max = s[i].Tscore;
t = s[i];
}
}
cout << "总成绩最高的是:" << max << setw(19) << "该同学是:" << t.name << endl;
}
void array_score(Student s[]) //排序
{
Student t;
for(int i = 0; i < 100; i++)
{
for(int j = 0; j < 100 - i; j++)
if(s[j].Tscore < s[j + 1].Tscore)
{
t = s[j];
s[j] = s[j + 1];
s[j +1] = t;
}
}
}
//void show_max(Student s[]);
void writefile(Student s[])
{
ofstream writefile("odered_score.dat", ios::out);
if(! writefile)
{
cerr << "open errror" << endl;
exit(1);
}
writefile << "名字" <<setw(8)<< "C++" <<setw(8)<< "高数" << setw(8) << "英语" << setw(8) << "总成绩" << setw(14) << "平均成绩" << endl;
for(int i = 0; i < 100; i++)
{
writefile << s[i].getname() << setw(8) << s[i].getCscore() << setw(8) << s[i].getMscore() << setw(8) << s[i].getEscore() << setw(8) << s[i].getTscore() << setw(14) << s[i].getAscore() << endl;
}
writefile.close();
}
int main()
{
Student stu[100];
readfile(stu);
Cmaxscore(stu);
Mmaxscore(stu);
Emaxscore(stu);
Tmaxscore(stu);
array_score(stu);
writefile(stu);
system("pause");
return 0;
}
再用冒泡法排序时, 不能改变,原来的属性