第八章 异常处理以及输入输出流

#include
using namespace std;
class ExceptionBase
{
public:
    ExceptionBase(){}
    ~ExceptionBase(){}
    virtual void showReason()
    {
        cout<<"Base Exception!\n";
    }
};
class PushOnFull:public ExceptionBase
{
public:
    virtual void showReason()
    {
        cout<<"Error! Full!\n";
    }
};
class PopOnEmpty:public ExceptionBase
{
public:
    virtual void showReason()
    {
        cout<<"Error! Empty\n";
    }
};
template
class Stack
{
private:
    T *pData;
    int stackSize;
    int top=-1;
public:
    Stack(int s=100);
    ~Stack();
    void push(const T &data);
    T pop();
    bool isEmpty();
    bool isFull();
};
template
Stack::Stack(int s)
{
    stackSize=s;
    pData=new T[s];
}
template
Stack::~Stack()
{
    delete [] pData;
}
template
void Stack::push(const T &data)
{
    if(isFull())
    {
        cout<<"Stack is full\n";
        throw PushOnFull();
    }
    ++top;
    pData[top]=data;
}
template
T Stack::pop()
{
    if(isEmpty())
    {
        cout<<"Stack is empty!\n";
        throw PopOnEmpty();
    }
    T t=pData[top];
    --top;
    return t; 
}
template
bool Stack::isEmpty()
{
    return top==-1;
}
template
bool Stack::isFull()
{
    return top==stackSize-1;
}
int main()
{
    int array[10]={1,3,2,4};
    Stacks(4);
    cout<

2 3 1 Stack is full
Error! Full!
[Finished in 0.4s]
#include
#include
#include
#include
#include
#include
using namespace std;
class Student
{
private:
    string name;
    string specialty;
    long long id;
    double creditPoint;
public:
    Student(string n,string s,long long i,double c):name(n),specialty(s),id(i),creditPoint(c){}
    void setCreditPoint(double c)
    {
        creditPoint=c;
    }
    double getCreditPoint()const
    {
        return creditPoint ;
    }
    string getName()const
    {
        return name;
    }
    long long getId()const
    {
        return id;
    }
    string getSpecialty()const
    {
        return specialty;
    }
    friend ostream& operator<<(ostream& out,const Student& s)
    {
        cout<<"Name:"< students;
    SortType sortType=BY_ID;
};
void StudentManage::addStudent()
{
    string n,s;
    long long i;
    double c;
    cout<<"please enter\nname:";
    cin>>n;
    cout<<"specialty:";
    cin>>s;
    cout<<"id:";
    cin>>i;
    cout<<"credit:";
    cin>>c;
    students.push_back(Student(n,s,i,c));
    SortType st=BY_ID;
    setSortType(st);
}
void StudentManage::removeStudent()
{
    long long i;
    cout<<"please enter id:";
    cin>>i;
    for(auto it=students.begin();it!=students.end();++it)
    {
        if((*it).getId()==i)
        {
            students.erase(it);
            return;
        }
    }
}
void StudentManage::setSortType(SortType st)
{
    function f;
    switch(st)
    {
    case BY_ID:
        f=[](const Student &s1,const Student &s2){return s1.getId()>i;
    for(auto stu:students)
    {
        if(stu.getId()==i)
        {
            cout<>n>>s>>i>>c)
    {
        students.push_back(Student(n,s,i,c));
    }
    fin.close();
}
void StudentManage::saveData(string filename)
{
    ofstream fout;
    fout.open(filename,ios_base::out);
    for(auto stu:students)
    {
        fout<>choice;
    sm.setSortType (static_cast(choice));
    cout<

你可能感兴趣的:(第八章 异常处理以及输入输出流)