⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 9-7.cpp

📁 Accelerated C++ 课后练习题 本人自己完成、可供参考
💻 CPP
字号:
#include<algorithm>
#include<iomanip>
#include<ios>
#include<iostream>
#include<stdexcept>
#include<string>
#include<vector>

class Student_info
{
public:
	Student_info();					//construct an empty Student_info object
	Student_info(std::istream&);	//construct one by read a stream
	std::string name() const 
	{	
		return n;
	}
	bool valid() const
	{
		return !homework.empty();
	}
	std::istream& read_hw(std::istream&);
	std::istream& read(std::istream&);
	double grade() const;

private:
	std::string n;
	double midterm,final;
	std::vector<double> homework;
	double final_grade;
};
bool compare(const Student_info&,const Student_info&);

int main()
{
	std::vector<Student_info> students;
	Student_info record;
	std::string::size_type maxlen=0;

	//read and store the data
	while(record.read(std::cin))
	{
		maxlen=std::_MAX(maxlen,record.name().size());
		students.push_back(record);
	}

	//alphabetize the student records
	std::sort(students.begin(),students.end(),compare);

	//write the names and grades
	for(std::vector<Student_info>::size_type i=0;i!=students.size();++i)
	{
		std::cout<<students[i].name()<<std::string(maxlen+1-students[i].name().size(),' ');


		try
		{
			
			std::streamsize prec=std::cout.precision();
			std::cout<<std::setprecision(3)<<students[i].grade()
				<<std::setprecision(prec)<<std::endl;
		}
		catch(std::domain_error e)
		{
			std::cout<<e.what()<<std::endl;
		}
	}
    return 0;
}




double median(std::vector<double> vec)
{
	typedef std::vector<double>::size_type vec_sz;

	vec_sz size=vec.size();
	if(size==0)
		throw std::domain_error("median of an empty vector");
	std::sort(vec.begin(),vec.end());

	vec_sz mid=size/2;
	return size%2==0?(vec[mid]+vec[mid-1])/2:vec[mid];
}


//compute a student's overall grade from midterm and final exam grades
double grade(double midterm,double final,double homework)
{
	return 0.2*midterm+0.4*final+0.4*homework;
}

//compute a student's overall grade from minterm and final exam grades
//and vector of homework grades
//this funtion does not copy its argument median does so far
double grade(double midterm,double final,const std::vector<double>& hw)
{
	if(hw.size()==0)
		throw std::domain_error("student has done no homework");
	return grade(midterm,final,median(hw));
}



bool compare(const Student_info& x,const Student_info& y)
{
	return x.name()<y.name();
}


//read homework grades from an input stream into a 'vector'
std::istream& Student_info::read_hw(std::istream& in)
{
	if(in)
	{
		//get rid of previous contents
		homework.clear();

		//read homework grades
		double x;
		while(in>>x)
			homework.push_back(x);
		
		//clear the stream so that input will work for the next student
		in.clear();
	}
	return in;
}

std::istream& Student_info::read(std::istream& in)
{
	in>>n>>midterm>>final;
	Student_info::read_hw(in);
	final_grade=::grade(midterm,final,homework);
	return in;
}



//the default constructor
Student_info::Student_info():midterm(0),final(0),final_grade(0)
{
}
Student_info::Student_info(std::istream& is)
{
	read(is);
}
double Student_info::grade() const
{
	return final_grade;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -