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

📄 14-2(3).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;


template<class T> class Ptr
{
public:
	//to copy the object conditionally when needed
	void make_unique()
	{
		if(*refptr!=1)
		{
			--*refptr;
			refptr=new size_t(1);
			p=p?clone(p):0;
		}
	}

	Ptr():refptr(new size_t(1)),p(0){}
	Ptr(T* t):refptr(new size_t(1)),p(t){}
	Ptr(const Ptr& h):refptr(h.refptr),p(h.p){++*refptr;}

	Ptr& operator=(const Ptr&);
	~Ptr();
	operator bool() const { return p?true:false;}
	T& operator*() const;
	T* operator->() const;
private:
	T* p;
	size_t* refptr;
};
template<class T> 
T& Ptr<T>::operator *() const
{
	if(p)
		return *p;
	throw std::runtime_error("unbound Ptr");
}
template<class T> 
T* Ptr<T>::operator ->() const
{
	if(p)
		return p;
	throw std::runtime_error("unbound Ptr");
}
template<class T> 
Ptr<T>& Ptr<T>::operator =(const Ptr& rhs)
{
	++*rhs.refptr;
	//free the left-hand side, destorying pointers if appropriate
	if(--*refptr==0)
	{
		delete refptr;
		delete p;
	}

	//copy in values from the right-hand side
	refptr=rhs.refptr;
	p=rhs.p;
	return *this;
}
template<class T>
Ptr<T>::~Ptr()
{
	if(--*refptr==0)
	{
		delete refptr;
		delete p;
	}
}
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){}
	std::string name() const;
	virtual std::istream& read(std::istream&);
	virtual double grade() const;
	virtual ~Core(){}


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() {}
	Student_info(std::istream& is) {read(is);}

	Student_info(const Student_info&);
	Student_info& operator=(const Student_info&);


	//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();
	}
private:
	Ptr<Core> cp;
};

std::istream& Student_info::read(std::istream& is)
{
	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)
	{
		if(s.cp)
			cp=s.cp->clone();
		else
			cp=0;
	}
	return *this;
}
int main()
{
	std::vector<Student_info> students;		//store pointers, not objects
	Student_info record;				//temporary must be a pointer as well
	string::size_type maxlen=0;

	//read and store the data
	while(record.read(cin))
	{
		maxlen=std::_MAX(maxlen,record.name().size());
		students.push_back(record);
	}
	//pass the version of compare that works on pointers
	std::sort(students.begin(),students.end(),Student_info::compare);

	//write the names and grades
	for(std::vector<Student_info>::size_type i=0;i!=students.size();++i)
	{
		//students[i] is a pointer that we dereference to call the function
		cout<<students[i].name()
			<<string(maxlen+1-students[i].name().size(),' ');
		try
		{
			double final_grade=students[i].grade();
			std::streamsize prec=cout.precision();
			cout<<std::setprecision(3)<<final_grade
				<<std::setprecision(prec)<<endl;
		}
		catch(std::domain_error e)
		{
			cout<<e.what()<<endl;
		} 
	}
	return 0;
}

⌨️ 快捷键说明

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