?? 13-2.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(){}
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
{
cout<<"Core::neme() const"<<endl;
return n;
}
double Core::grade() const
{
cout<<"Core::grade() const"<<endl;
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
{
cout<<"Grad::grade() const"<<endl;
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();
}
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;
}
int main()
{
Core* p1 = new Core(cin);
Core* p2 = new Grad(cin);
Core s1(cin);
Grad s2(cin);
cout<<p1->grade()<<endl;;
cout<<p1->name()<<endl;
cout<<p2->grade()<<endl;
cout<<p2->name()<<endl;
cout<<s1.grade()<<endl;
cout<<s1.name()<<endl;
cout<<s2.name()<<endl;
cout<<s2.grade()<<endl;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -