#include<iostream>#include<cmath>#include"exc_mean.h"doublehmean(double a,double b);doublegmean(double a,double b);intmain(){using std::cin;using std::cout;using std::endl;double x, y, z;
cout <<"Enter two numbers: ";while(cin >> x >> y){try{
z =hmean(x, y);
cout <<"Harmonic mean of "<< x <<" and "<< y;
cout <<" is "<< z << endl;
z =gmean(x, y);
cout <<"Geometric mean of "<< x <<" and "<< y;
cout <<" is "<< z << endl;
cout <<"Enter next set of numbers <q to quit>: ";}catch(bad_hmean &bg){
cout << bg.what();//调用what方法打印hmean中的异常;
cout <<"Try again.\n";continue;}catch(bad_gmean &hg){
cout << hg.what();//调用what方法打印gmean中的异常;
cout <<"Sorry, you don't get to play any more.\n";break;}}
cout <<"Bye!\n";return0;}doublehmean(double a,double b){if(a ==-b){throwbad_hmean();}return2.0* a * b /(a + b);}doublegmean(double a,double b){if(a <0|| b <0){throwbad_gmean();}return std::sqrt(a * b);}
#ifndefEXC_MEAN_H_#defineEXC_MEAN_H_#include<iostream>#include<string>#include<stdexcept>usingnamespace std;classbad_hmean:publiclogic_error{public:explicitbad_hmean(const string &s ="Error in bad_hmean object\nhmean() arguments should be a != -b\n"):logic_error(s){}virtual~bad_hmean()noexcept{}//c++11新声明表明函数不会引发异常;};classbad_gmean:publiclogic_error{public:explicitbad_gmean(const string &s ="Error in bad_hmean object\ngmean() arguments should be >= 0\n"):logic_error(s){}virtual~bad_gmean()noexcept{}//同上};#endif
#include<iostream>#include<cmath>#include"exc_mean.h"doublehmean(double a,double b);doublegmean(double a,double b);intmain(){using std::cin;using std::cout;using std::endl;double x, y, z;
cout <<"Enter two numbers: ";while(cin >> x >> y){try{
z =hmean(x, y);
cout <<"Harmonic mean of "<< x <<" and "<< y;
cout <<" is "<< z << endl;
z =gmean(x, y);
cout <<"Geometric mean of "<< x <<" and "<< y;
cout <<" is "<< z << endl;
cout <<"Enter next set of numbers <q to quit>: ";}catch(mean &m)//捕获基类以及派生类的任何异常;{
cout <<"Sorry, you don't get to play any more.\n";break;}}
cout <<"Bye!\n";return0;}doublehmean(double a,double b){if(a ==-b){
bad_hmean temp("hmean", a, b);
temp.mesg();throw temp;//捕捉到异常}return2.0* a * b /(a + b);}doublegmean(double a,double b){if(a <0|| b <0){
bad_gmean temp("gmean", a, b);
temp.mesg();throw temp;}return std::sqrt(a * b);}
#ifndefEXC_MEAN_H_#defineEXC_MEAN_H_#include<iostream>#include<string>#include<stdexcept>usingnamespace std;classmean:publiclogic_error{private:double v1;double v2;public:mean(const string &s ="none",double a =0.0,double b =0.0):logic_error(s),v1(a),v2(b){}constdouble&val1()const{return v1;}//便于基类访问私有成员;constdouble&val2()const{return v2;}//同上;virtual~mean()noexcept{}//函数不会引发异常;};classbad_hmean:publicmean{public:bad_hmean(const string &s ="none",double a =0.0,double b =0.0):mean(s, a, b){}voidmesg()const;//报告值以及函数名;virtual~bad_hmean()noexcept{}};inlinevoid bad_hmean::mesg()const{
cout <<"hmean("<<val1()<<", "<<val2()<<"):"<< endl;
cout <<"hmean() arguments should be a != b"<< endl;return;}classbad_gmean:publicmean{public:bad_gmean(const string &s ="none",double a =0.0,double b =0.0):mean(s, a, b){}voidmesg()const;//报告值以及函数名;virtual~bad_gmean()noexcept{}};inlinevoid bad_gmean::mesg()const{
cout <<"gmean("<<val1()<<", "<<val2()<<"):"<< endl;
cout <<"gmean() arguments should be >= 0"<< endl;return;}#endif