十六周任务二 学生成绩排序文件score.dat 中保存的是100 名学生的姓名和C++课、高数和英语成绩,将成绩排序后保存到ordered_score.dat文件中

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称:    十六周任务二 学生成绩排序文件score.dat 中保存的是100 名学生的姓名和C++课、高数和英语成绩
读入这名学生的成绩,用对象数组进行存储
在屏幕上显示各科及总分的最高分,排序后的成绩单(包括总分)保存到文件odered_score.dat中                        
* 作    者:     石丽君                         
* 完成日期:      2012   年     6  月      4  日
* 版 本 号:          

* 对任务及求解方法的描述部分
* 输入描述: 学生成绩排序
文件score.dat 中保存的是100 名学生的姓名和C++课、高数和英语成绩。
(1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总分、均分数据成员,成员函数根据
需要确定。
(2)读入这名学生的成绩,用对象数组进行存储。
(3)求出各科和总分的最高分。
(4)请按总分的降序(高成绩在前,低成绩在后)排序
(5)在屏幕上显示各科及总分的最高分,排序后的成绩单(包括总分)保存到文件odered_score.dat中。
* 问题描述: 
* 程序输出: 
* 程序头部的注释结束
*/


#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Student
{
private:
	string name;
	int C;
	int Math;
	int English;
	int All_score;
	double Average;
public:
	Student();
	Student(int c,int math,int english,int all_score,double average):C(c),Math(math),English(english),All_score(all_score),Average(average){}
	friend void arrange_all_score(Student stu[]);
	friend void get_frome_file(Student stu[]);
	friend void save_to_file(Student stu[]);
	friend void get_heigh_allscore(Student stu[]);
	friend void get_height_Cscore(Student stu[]);
	friend void get_height_Mathscore(Student stu[]);
	friend void get_height_Englishscore(Student stu[]);

};
Student::Student()
{
	C=0;
	Math=0;
	English=0;
	All_score=0;
	Average=0;
}
void arrange_all_score(Student stu[])
{
	int i,j,t;
	for( j=0;j<99;j++)
	{
		for( i=0;i<99-j;i++)
			if(stu[i].All_score<stu[i+1].All_score)
			{
				t=stu[i].All_score;
				stu[i].All_score=stu[i+1].All_score;
				stu[i+1].All_score=t;
			}
	}
}
void get_heigh_allscore(Student stu[])
{
	Student s;
	int i=0;
	s.All_score=stu[i].All_score;
	for(i=0;i<100;i++)
	{
		if(stu[i].All_score>s.All_score)
		{
			s.All_score =stu[i].All_score ;
			s.name =stu[i].name ;
		}
	}
	cout<<"总分成绩最高为:"<<s.All_score<<'\t'<<"成员:";
	for(i=0;i<100;i++)
		if(s.All_score ==stu[i].All_score )
			cout<<stu[i].name<<" ";
}
void get_height_Cscore(Student stu[])
{
	Student s;
	int i=0;
	s.C=stu[i].C;
	for(i=0;i<100;i++)
	{
		if(stu[i].C>s.C)
		{
			s.C =stu[i].C;
			s.name =stu[i].name ;
		}
	}
	cout<<"C++成绩最高为:"<<s.C<<'\t'<<"成员:";
	for(i=0;i<100;i++)
		if(s.C ==stu[i].C )
			cout<<stu[i].name<<" ";

}
void get_height_Mathscore(Student stu[])
{
	Student s;
	int i=0;
	s.Math=stu[i].Math;
	for(i=0;i<100;i++)
	{
		if(stu[i].Math>s.Math)
		{
			s.Math =stu[i].Math;
			s.name =stu[i].name ;
		}
	}
	cout<<"高数成绩最高为:"<<s.Math<<'\t'<<"成员:";
	for(i=0;i<100;i++)
		if(s.Math==stu[i].Math )
			cout<<stu[i].name<<" ";
}
void get_height_Englishscore(Student stu[])
{
	Student s;
	int i=0;
	s.English=stu[i].English;
	for(i=0;i<100;i++)
	{
		if(stu[i].English>s.English)
		{
			s.English =stu[i].English;
			s.name =stu[i].name ;
		}
	}
	cout<<"英语成绩最高为:"<<s.English<<'\t'<<"成员:";
	for(i=0;i<100;i++)
		if(s.English==stu[i].English )
			cout<<stu[i].name<<" ";

}

void get_frome_file(Student stu[])
{
	ifstream infile("score.dat",ios::in);
	if(!infile)
	{
		cerr<<"open score.dat error!"<<endl;
		exit(1);
	}
	for(int i=0;i<100;i++)
	{
		infile>>stu[i].name>>stu[i].C>>stu[i].Math>>stu[i].English;
		stu[i].Average=(stu[i].C+stu[i].Math+stu[i].English)/3;
		stu[i].All_score=stu[i].C+stu[i].Math+stu[i].English;
	}
	infile.close();

}
void save_to_file(Student stu[])
{
	cout<<"姓名"<<'\t'<<"C++"<<'\t'<<"高数"<<'\t'<<"英语"<<'\t'<<"平均分"<<'\t'<<"总分"<<endl;

	ofstream outfile("ordered_score.dat",ios::out);
	if (!outfile)
	{
		cerr<<"open error!"<<endl;

	}
	for(int i=0;i<100;i++)
	{
		outfile<<stu[i].name<<'\t'<<stu[i].C<<'\t'<<stu[i].Math<<'\t'<<stu[i].English<<'\t'<<stu[i].Average<<'\t'<<stu[i].All_score<<endl;
		cout<<stu[i].name<<'\t'<<stu[i].C<<'\t'<<stu[i].Math<<'\t'<<stu[i].English<<'\t'<<stu[i].Average<<'\t'<<stu[i].All_score<<endl;
	}
	outfile.close();
}
int main()
{
	Student stu[100];
	get_frome_file(stu);
	arrange_all_score( stu);
	get_heigh_allscore(stu);
	cout<<endl;
	get_height_Cscore( stu);
	cout<<endl;
	get_height_Mathscore( stu);
	cout<<endl;
	get_height_Englishscore( stu);
	cout<<endl;
	cout<<endl;
	save_to_file(stu);
	cout<<endl;
	system("pause");
	return 0;

}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值