第十六周实验报告二

这是一个C++程序,用于读取学生分数数据文件,计算总分、平均分,并进行排序和输出最高分。程序首先从'score.dat'文件中读取学生的姓名、C语言、数学和英语分数,然后输出数据到'ordered_score.dat',同时找出各科最高分和总分最高分。最后,程序使用冒泡排序对学生总分进行排序并显示排序后的成绩。
摘要由CSDN通过智能技术生成
  1. 程序的版权和版本声明部分 
  2. * Copyright (c) 2011, 烟台大学计算机学院学生 
  3. * All rights reserved. 
  4. * 文件名称: 
  5. * 作者:李君凯 
  6. * 完成日期: 2012年 6月 6日
  7. * 版本号: 
  8. *对任务及求解方法的描述部分
  9. #include<iostream> 
    #include <string>
    using namespace std;
    #include<fstream>
    class Student
    {
    public:
    	double get_total();
    	double get_average();
    	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 paixu(Student *stu);
    private:
    	string name;
    	double c_score;
    	double m_score;
    	double e_score;
    	double total;
    	double average;
    };
    
    double Student::get_total()
    {
    	(this->total) = (this->c_score + this->e_score + this->m_score);
    	return (this->total);
    }
    
    double Student::get_average()
    {
    	(this->average) = (this->c_score + this->e_score + this->m_score)/3;
    	return (this->average);
    }
    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].average;
    			cout<<stu[i].average<<" ";
    			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<<max1<<endl;
    	cout<<max2<<endl;
    	cout<<max3<<endl;
    	cout<<max4<<endl;
    
    }
    
    	void paixu(Student *stu)
    	{
    		
    		Student t;
    		for( int i=0;i<100;++i)
    		{
    			stu[i].get_total();
    			stu[i].get_average();
    		}
    		for(int i=0;i<99;++i)
    			for(int j=0;j<99-i;++j)
    			{
    				if(stu[i].total<stu[i+1].total)
    				{
    					t=stu[i];
    					stu[i]=stu[i+1];
    					stu[i+1]=t;
    				}
    			}
    	}
    int main()
    {	
    	Student stu[100]; 
        input(stu);
    	output_max(stu);
    	paixu(stu);
    	output(stu);
        cout<<endl;  
    	system("PAUSE");  
        return 0;  
    
    
    }
    

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值