十六周任务2:成绩排序

时间:2022-10-20 04:01:37
#include<iostream> 
#include <string>
using namespace std;
#include<fstream>
class Student
{
public:
// Student(string name, double c_score, double m_score, double e_score, double total):this->name(name), this->c_score(c_score), this->m_score(m_score), this->e_score(e_score), this->total(total){}
double get_total();
void get_name(string name);
void get_c_score(double c_score);
void get_m_score(double m_score);
void get_e_score(double e_score);
friend void input(Student *stu) ;
friend void output(Student *stu);
friend void output_max(Student *stu);
friend void ordered_student(Student std[]);
private:
string name;
double c_score;
double m_score;
double e_score;
double total;
};

//总分的计算
double Student::get_total()
{
(this->total) = (this->c_score + this->e_score + this->m_score);
return (this->total);
}

void Student::get_name(string name)
{
this->name = name;
}
void Student::get_c_score(double c_score)
{
this->c_score = c_score;
}
void Student::get_m_score(double m_score)
{
this->m_score = m_score;
}
void Student::get_e_score(double e_score)
{
this->e_score = e_score;
}
//从文件中读取数据
void input(Student stu[])
{
string name;
int i;
double c_score;
double m_score;
double e_score;
ifstream inFile("score.dat",ios::in);
if(!inFile)
{
cerr<<"open error!"<<endl;
exit(1);//提示错误
}
for( i=0;i<100;++i)
{
inFile>>name;
stu[i].get_name( name);

inFile>>c_score;
stu[i].get_c_score( c_score);
inFile>>e_score;
stu[i].get_e_score( e_score);

inFile>>m_score;
stu[i].get_m_score( m_score);
}
inFile.close();//关闭文件
}

void output(Student *stu)//输出数据
{
ofstream writeFile("odered_score.dat",ios::out);
if(!writeFile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<100;++i)
{
writeFile<<stu[i].name;
cout<<stu[i].name<<" ";
writeFile<<stu[i].c_score;
cout<<stu[i].c_score<<" ";
writeFile<<stu[i].e_score;
cout<<stu[i].e_score<<" ";
writeFile<<stu[i].m_score;
cout<<stu[i].m_score<<" ";
writeFile<<stu[i].total;
cout<<stu[i].total<<" ";
cout<<endl;
}
writeFile.close();//关闭文件

}
//求最大数
void output_max(Student *stu)
{
double max1,max2,max3,max4;
max1=stu[0].c_score;
max2=stu[0].m_score;
max3=stu[0].e_score;
max4=stu[0].get_total();
for(int i=0;i<100;++i)
{
if(stu[i].c_score>max1)
{
max1=stu[i].c_score;
}

if(stu[i].m_score>max2)
{
max2=stu[i].m_score;
}

if(stu[i].e_score>max3)
{
max3=stu[i].e_score;
}

if(stu[i].get_total()>max4)
{
max4=stu[i].get_total();
}

}
cout<<"c++的最高成绩是:"<<max1<<endl;
cout<<"高数的最高成绩是:"<<max2<<endl;
cout<<"英语的最高成绩是:"<<max3<<endl;
cout<<"总分的最高成绩是:"<<max4<<endl;

}
//排序
void ordered_student(Student std[])
{
int i, j;
Student t;
for(i = 0; i < 100; ++ i)
{
for(j = 0; j < 100; ++j)
{
if(std[i].total > std[j].total)
{
t = std[i];
std[i] = std[j];
std[j] = t;
}
}
}

}

int main()
{
Student stu[100];
input(stu);
output_max(stu);
ordered_student(stu);
output(stu);
cout<<endl;
system("PAUSE");
return 0;


}

十六周任务2:成绩排序

感言:

这个程序还比较复杂,用到以前的知识点比较多,也顺便复习了一下。。。感觉不错。。。