标题:模板和异常处理
关键词:异常处理
内容:
A今日完成情况
关键词:异常处理
内容:
A今日完成情况
1. 异常处理50%
B具体内容
#include<iostream>
#define MAX 200
#define MiN 100
using namespace std;
int data;
double div;
class except{
char *message;
public:
except(char * ptr){message = ptr;
}
const char *what(){return message;
}
virtual void handling(){
cout<<"再次输入被除数";
cin>>div;
}
void action(){
cout<<"异常为"<<what()<<'\n';
handling();
}
};
class except_derive:public except{
public:
except_derive(char * ptr):except(ptr){
}
virtual void handling(){
if(data>MAX)
cout<<"启动数据转换,将数据减少至"<<(data/=2)<<endl;
else
cout<<"启动数据转换,将数据增加至"<<(data*=2)<<endl;
}
};
double quotient(double m,double n){
if(n==0)
throw except("除数为0的错误抛出");
return m/n;
}
int main()
{
double n,result;
int flag=1;
char * mes_low={"数据太小 超出范围"};
char * mes_high={"数据太大 超出范围"};
cout<<"请输入转换数据";
cin>>data;
cout<<"请输入除数和被除数";
cin>>n>>div;
while(glag){
try{
if((data>MAX)||(data<MIN))
throw except_derive((data>MAX)?(mes_high):(mes_low));
result=quotient(n,div);
cout<<"二数相除的结果为"<<result<<endl;
flag=0;
}
catch(except_derive ex){ex.action();
}
catch(except ex){ex.action();
}
}
}
C明日计划
A 异常处理
C明日计划
A 异常处理