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

📄 13-4.cpp

📁 Accelerated C++ 课后练习题 本人自己完成、可供参考
💻 CPP
字号:
 #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include <iomanip>
#include<stdexcept>
using std::string;
using std::cout;
using std::cin;
using std::endl;
class Core
{
	friend class Student_info;
public:
	//default constructor for core
	Core():midterm(0),final(0){};
	//build a Core from an istream
	Core(std::istream& is){read(is);};
	std::string name() const;
	virtual std::istream& read(std::istream&);
	virtual double grade() const;
	virtual ~Core(){}
	bool valid() const
	{
		return !homework.empty();
	}

protected:
	//accessible to derived classes
	std::istream& read_common(std::istream&);
	double midterm,final;
	std::vector<double> homework;
	virtual Core* clone() const { return new Core(*this); }

private:
	//accessible only to Core
	std::string n;
};
class Grad:public Core
{
public:
	//both constructor implicitly use cor Core::Core() to initialize the base part
	Grad():thesis(0){};

	//build a Core from an istream
	Grad(std::istream& is){read(is);};
	double grade() const;
	std::istream& read(std::istream&);
protected:
//	Grad* clone() const {return new Grad(*this);}
	Core* clone() const {return new Grad(*this);}
private:
	double thesis;
};


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



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

		//read homework grades
		double x;
		while(in>>x)
			hw.push_back(x);
		
		//clear the stream so that input will work for the next student
		in.clear();
	}
	return in;
}
string Core::name() const
{
	return n;
}
double Core::grade() const
{
	return ::grade(midterm,final,homework);
}
std::istream& Core::read_common(std::istream& in)
{
	//read and store the student's name and exam grades
	in>>n>>midterm>>final;
	return in;
}
std::istream& Core::read(std::istream& in)
{
	read_common(in);
	read_hw(in,homework);
	return in;
}
std::istream& Grad::read(std::istream& in)
{
	read_common(in);
	in>>thesis;
	read_hw(in,homework);
	return in;
}
double Grad::grade() const
{
	return std::_MIN(Core::grade(),thesis);
}
bool compare(const Core& c1,const Core& c2)
{
	return c1.name()<c2.name();
}
bool compare_grades(const Core& c1,const Core& c2)
{
	return c1.grade()<c2.grade();
}
bool compare_Core_ptrs(const Core* cp1,const Core* cp2)
{
	return compare(*cp1,*cp2);
}

class Student_info
{
public:
	//constructor and copy control
	Student_info():cp(0){}
	Student_info(std::istream& is):cp(0){read(is);}
	Student_info(const Student_info&);
	Student_info& operator=(const Student_info&);
	~Student_info(){delete cp;}

	//operations
	std::istream& read(std::istream&);

	std::string name() const
	{
		if(cp)
			return cp->name();
		else
			throw std::runtime_error("uninitialized Student");
	}
	double grade() const
	{
		if(cp)
			return cp->grade();
		else
			throw std::runtime_error("uninitialized Student");
	}
	static bool compare(const Student_info& s1,const Student_info& s2)
	{
		return s1.name()<s2.name();
	}

	bool valid() const
	{
		return cp->valid();
	}
private:
	Core* cp;
};

std::istream& Student_info::read(std::istream& is)
{
	delete cp;		//delete previous object, if any

	char ch;
	is>>ch;			//get record type

	if(ch=='u')
		cp=new Core(is);
	else 
		cp=new Grad(is);
	return is;
}
Student_info::Student_info(const Student_info& s):cp(0)
{
	if(s.cp)
		cp=s.cp->clone();
}
Student_info& Student_info::operator =(const Student_info& s)
{
	if(&s!=this)
	{
		delete cp;
		if(s.cp)
			cp=s.cp->clone();
		else
			cp=0;
	}
	return *this;
}

string letter_grade(double grade)
{
    // range posts for numeric grades
    static const double numbers[] = {
        97, 94, 90, 87, 84, 80, 77, 74, 70, 60, 0
    };

    // names for the letter grades
    static const char* const letters[] = {
        "A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D", "F"
    };

    // compute the number of grades given the size of the array
    // and the size of a single element
    static const size_t ngrades = sizeof(numbers)/sizeof(*numbers);

    // given a numeric grade, find and return the associated letter grade
    for (size_t i = 0; i < ngrades; ++i) {
        if (grade >= numbers[i])
            return letters[i];
    }
    return "?\?\?";
}


int main()
{
	Student_info record(cin);
	if(record.valid())
	{
		double final_grade=record.grade();
		std::streamsize prec=std::cout.precision();
		std::cout<<std::setprecision(3)<<final_grade
				<<std::setprecision(prec)<<std::endl;
	}
    return 0;
}

⌨️ 快捷键说明

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