第十六周实验报告2

#include<iostream>    
#include<fstream> 
#include<string>
#include<iomanip>
using namespace std; 
class Student  
{  
public:  
	Student();
	Student(string name,int C_plus_plus,int Math,int English,int All_score,double Average);
	void set_name(string name);
	void set_C_plus_plus(int C_plus_plus);
	void set_Math(int Math);
	void set_English(int English);
	void set_All_score(int All_score);
	void set_Average(double Average);
	string get_name();
	int get_C_plus_plus();
	int get_Math();
	int get_English();
	int get_All_score();
	double get_Average();
	friend Student high_C_plus_plus_score(Student  s[]);
	friend Student high_Math_score(Student  s[]) ;
	friend Student high_English_score(Student  s[]);
	friend Student high_All_score(Student  s[]);
	friend void Descending_order(Student  s[]);//排序,降序;
	friend void All_score(Student  s[]);
	friend void Average_score(Student  s[]);
    friend ostream& operator << (ostream&,Student&); //重载流插入运算符“<<”  ;
private:         
    string name;
	int C_plus_plus;
	int Math;
	int English;
	int All_score;
	double Average;
};
void input_student(Student  s[]);
void ordered_student_dat(Student s[]);
int main()  
{  
	Student s1[100],s2; 
    input_student(s1);//读入100人的原始分数   
	All_score(s1);
	Average_score(s1);
    s2=high_C_plus_plus_score(s1);
	cout<<"C++的最高分为:"<<s2<<endl;
	s2=high_Math_score(s1);
	cout<<"高数的最高分为:"<<s2<<'\n' ;
	s2=high_English_score(s1);
	cout<<"英语的最高分为:"<<s2<<'\n' ;
	s2=high_All_score(s1);
	cout<<"总分的最高分为:"<<s2<<'\n' ;
	Descending_order(s1);
	ordered_student_dat(s1);
	cout<<endl;
	system("PAUSE");  
    return 0;  
}
Student::Student()
{
	name="unknow";
	C_plus_plus=0;
	Math=0;
	English=0;
	All_score=0;
	Average=0.0;
}
Student::Student(string name,int C_plus_plus,int Math,int English,int All_score,double Average)
{
	this->name=name;
	this->C_plus_plus=C_plus_plus;
	this->Math=Math;
	this->English=English;
	this->All_score=All_score;
	this->Average=Average;
}

void input_student(Student s[])  
{  
    int i=0; 
	string name;
	int C_plus_plus;
	int Math;
	int English;
	ifstream infile("score.dat",ios::in);
	if (!infile)
	{
		cerr<<"open error!"<<endl;
		exit(1);
	}
	for (i=0;i<100;i++)
	{
		infile>>name;
		s[i].set_name(name);
		infile>>C_plus_plus;
		s[i].set_C_plus_plus(C_plus_plus);
			infile>>Math;
		s[i].set_Math(Math);
			infile>>English;
		s[i].set_English(English);
	}
	infile.close();
	//cout<<endl;
     
}
void ordered_student_dat(Student s[])
{
	ofstream outfile("ordered_student.dat",ios::out);
	if(!outfile)
	{
		cerr<<"open error!"<<endl;
		exit(1);
	}
	for(int i=0;i<100;i++)
		outfile<<s[i].get_name()<<"\t"<<s[i].get_C_plus_plus()<<"\t"<<s[i].get_Math()<<"\t"<<s[i].get_English()<<"\t"<<s[i].get_All_score()<<"\t"<<setiosflags(ios::fixed)<<setprecision(2)<<s[i].get_Average()<<'\n';
	outfile.close();
	return ;
}

 void Student::set_name(string name)
{
	this->name=name;
}
void Student::set_C_plus_plus(int C_plus_plus)
{
	this->C_plus_plus=C_plus_plus;
}	
void Student::set_Math(int Math)
{
	this->Math=Math;
}
void Student::set_English(int English)
{
	this->English=English;
}
void Student::set_All_score(int All_score)
{
	this->All_score=All_score;
}
void Student::set_Average(double Average)
{
	this->Average=Average;
}
string Student::get_name()
{
	return name;
}
int Student::get_C_plus_plus()
{
	return C_plus_plus;
}
int Student::get_Math()
{
	return Math;
}
int Student::get_English()
{
	return English;
}
int Student::get_All_score()
{
	return All_score;
}
double Student::get_Average()
{
	return Average;
}

Student high_C_plus_plus_score(Student  s[]) 
{
	Student student;
	int score;
	int i=0,j=0;
	string name;
	score=s[i].get_C_plus_plus();
	student.set_C_plus_plus(score);
	for(i=0;i<99;++i)
	{
		if(student.get_C_plus_plus()<s[i+1].get_C_plus_plus())
		{
			score=s[i+1].get_C_plus_plus();
			student.set_C_plus_plus(score);
			name=s[i+1].get_name();
			student.set_name(name);
		}
	}
	return student;
}
Student high_Math_score(Student  s[]) 
{
	Student student;
	int score,i=0;
	string name;
	score=s[i].get_Math();
	student.set_Math(score);
	for(i=0;i<99;++i)
	{
		if(student.get_Math()<s[i+1].get_Math())
		{
			score=s[i+1].get_Math();
			student.set_Math(score);
			name=s[i+1].get_name();
			student.set_name(name);
		}
	}
	return student;
}

Student high_English_score(Student  s[]) 
{
	Student student;
	int score,i=0;
	string name;
	score=s[i+1].get_English();
	student.set_English(score);
	for(i=0;i<99;++i)
	{
		if(student.get_English()<s[i+1].get_English())
		{
			score=s[i+1].get_English();
			student.set_English(score);
			name=s[i+1].get_name();
			student.set_name(name);
		}
	}
		return student;
}
Student high_All_score(Student  s[]) 
{
	Student student;
	int score,i=0;
	string name;
	score=s[i].get_All_score();
	student.set_All_score(score);
	for(i=0;i<99;++i)
	{
		if(student.get_All_score()<s[i+1].get_All_score())
		{
			score=s[i+1].get_All_score();
			student.set_All_score(score);
			name=s[i+1].get_name();
			student.set_name(name);
		}
	}
	return student;
}
void Descending_order(Student  s[]) 
{
	Student student;
	string name;
    int i,j;   
    for(i=0;i<100-1;i++) 
	{
        for(j=0;j<100-i-1;j++) 
		{
			if(s[j].get_All_score()<s[j+1].get_All_score())  
            {  
                student=s[j+1];  
                s[j+1]=s[j];  
                s[j]=student;  
            }  
		}
	}
	for (i=0;i<100;i++)  
	{  
		cout<<s[i].get_All_score()<<"  ";  
	}  
}
void All_score(Student  s[]) 
{
	int score;
	for(int i=0;i<100;++i)
	{
		score=s[i].get_C_plus_plus()+s[i].get_Math()+s[i].get_English();
		s[i].set_All_score(score);
	}
		
}
void Average_score(Student  s[]) 
{
	double average;
	for(int i=0;i<100;++i)
	{
		average=double(s[i].get_C_plus_plus()+s[i].get_Math()+s[i].get_English())/3;
		s[i].set_Average(average);
	}
}

ostream& operator << (ostream&output,Student&s)  
{   
	if(s.get_C_plus_plus()!=0)
	{
		output<<s.get_C_plus_plus()<<'\t'<<"该同学名叫:"<<s.get_name()<<endl;  
	}
	else if(s.get_Math()!=0)
	{
		output<<s.get_Math()<<'\t'<<"该同学名叫:"<<s.get_name()<<endl;  
	}
	else if(s.get_English()!=0)
	{
		output<<s.get_English()<<'\t'<<"该同学名叫:"<<s.get_name()<<endl;  
	}
	else
	{
		output<<s.get_All_score()<<'\t'<<"该同学名叫:"<<s.get_name()<<endl;  
	}
    return output;  
} 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值