📄 14-1.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
{
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();
}
virtual double grade_thesis()const{return 1;}
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;
};
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;
}
bool compare(const Core& c1,const Core& c2)
{
return c1.name()<c2.name();
}
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;}
operator double() const {return p->grade();}
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;
}
}
int main()
{
Ptr<Core> stu1,stu2;
stu1=new Core(cin);
stu2=new Core(cin);
cout<<stu1<<endl<<stu2<<endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -