/*【任务2】学生成绩排序
文件score.dat 中保存的是100 名学生的姓名和C++课、高数和英语成绩。
(1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总分、均分数据成员,成员函数根据
需要确定。
(2)读入这名学生的成绩,用对象数组进行存储。
(3)求出各科和总分的最高分。
(4)请按总分的降序(高成绩在前,低成绩在后)排序
(5)在屏幕上显示各科及总分的最高分,排序后的成绩单(包括总分)保存到文件odered_score.dat
中。*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student
{
public:
Student();
void set_name(string name);
void set_c_score(int c_score);
void set_m_score(int m_score);
void set_e_score(int e_score);
void set_s_score(int s_score);
void set_a_score(int a_score);
string get_name(){return name;}
int get_c_score(){return c_score;}
int get_m_score(){return m_score;}
int get_e_score(){return e_score;}
int s_score;
int a_score;
private:
string name;
int c_score;
int m_score;
int e_score;
};
Student::Student()
{
name = '\0';
c_score = 0;
m_score = 0;
e_score = 0;
s_score = 0;
a_score = 0;
}
void Student::set_name(string name)
{
this->name = name;
}
void Student::set_c_score(int c_score)
{
this-> c_score = c_score;
}
void Student::set_m_score(int m_score)
{
this->m_score = m_score;
}
void Student::set_e_score(int e_score)
{
this->e_score = e_score;
}
void Student::set_s_score(int s_score)
{
this->s_score = s_score;
}
void Student::set_a_score(int a_score)
{
this->a_score = a_score;
}
int main()
{
Student stu[100];
int m;
string n;
ifstream infile("score.txt",ios::in);
if (! infile)
{
cerr << "open error" << endl;
exit(1);
}
string name;
int c_score;
int m_score;
int e_score;
int s_score;
int a_score;
for (int i = 0; i < 100; i++ )
{
infile >> name >> c_score >> m_score >> e_score;
stu[i].set_name(name);
stu[i].set_c_score(c_score);
stu[i].set_m_score(m_score);
stu[i].set_e_score(e_score);
s_score = stu[i].get_c_score() + stu[i].get_m_score() + stu[i].get_e_score();
stu[i].set_s_score(s_score);
a_score = stu[i].s_score / 3;
stu[i].set_a_score(a_score );
}
infile.close();
int s_max = stu[0].s_score;
for ( int i = 0; i < 100; i++ )
{
if ( stu[i].s_score > s_max )
{
s_max = stu[i].s_score;
}
}
int c_max = stu[0].get_c_score();
for ( int i = 0; i < 100; i++ )
{
if (stu[i].get_c_score() > c_max)
{
c_max = stu[i].get_c_score();
}
}
int m_max = stu[0].get_m_score();
for ( int i = 0; i < 100; i++ )
{
if (stu[i].get_m_score() > m_max)
{
m_max = stu[i].get_m_score();
}
}
int e_max = stu[0].get_e_score();
for ( int i = 0; i < 100; i++ )
{
if (stu[i].get_e_score() > e_max)
{
e_max = stu[i].get_e_score();
}
}
cout << "总分成绩最大值" << s_max << endl << "c++成绩最大值" << c_max << endl <<"高数成绩最大值" << m_max << endl << "英语成绩最大值" << e_max <<endl;
for( int i = 0; i < 100; i++ )
for ( int j = 0; j < 99 - i; j++ )
{
if ( stu[i].s_score > stu[i+1].s_score )
{
n = stu[i].get_name();
stu[i].get_name() = stu[i+1].get_name();
stu[i+1].get_name() = n;
m = stu[i].s_score;
stu[i].s_score = stu[i+1].s_score;
stu[i+1].s_score = m;
}
}
ofstream outfile("inscore.txt",ios::out);
if (! outfile)
{
cerr << "open error" << endl;
exit(1);
}
for (int i = 0; i < 100; i++ )
{
outfile << stu[i].get_name() << stu[i].get_c_score() << stu[i].get_m_score() << stu[i].get_e_score() << stu[i].s_score ;
}
outfile.close();
system("pause");
return 0;
}