十六周任务2:成绩排序

#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;  
	
	
}

感言:

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

 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值