chapter15test3
2015-07-01 15:53
393 查看
mean.h
#include<iostream>
#include<stdexcept>
class base :public std::logic_error
{
private:
double x; double y;
public:
base(double a = 0, double b = 0) :x(a), y(b),logic_error(0){}
double xr(){ return x; }
double yr(){ return y; }
};
class bad_hmean:public base
{
public:
bad_hmean(double a,double b) :base(a,b){}
void what();
};
inline void bad_hmean::what()
{
std::cout << "You can't process Hmean() with x= " << xr() << ",y= " << yr() << std::endl;
}
class bad_gmean :public base
{
public:
bad_gmean(double a = 0, double b = 0) :base(a,b){}
void what();
};
inline void bad_gmean::what()
{
std::cout << "You can't process Gmean() with x= " << xr() << ",y= " << yr() << std::endl;
}
user.cpp
#include<iostream>
#include<cmath>
#include"mean.h"
using namespace std;
double hmean(double a, double b);
double gmean(double a, double b);
int main()
{
double x, y, z;
cout << "Enter two numbaers :\n";
while (cin >> x >> y)
{
try{z = hmean(x, y);
cout << "Hmean(" << x << "," << y << ")=" << z << endl;
cout << "Gmean(" << x << "," << y << ")=" << gmean(x,y) << endl;
}
catch(bad_hmean &h)
{
h.what();
break;
}
catch (bad_gmean &g)
{
g.what();
break;
}
}
cout << "Finished .\n";
}
double hmean(double a, double b)
{
if (a != -b)
return 2.0*a*b / (a + b);
else
throw bad_hmean(a,b);
}
double gmean(double a, double b)
{
if (a > 0 && b > 0)
return sqrt(a*b);
else
throw bad_gmean(a, b);
}
#include<iostream>
#include<stdexcept>
class base :public std::logic_error
{
private:
double x; double y;
public:
base(double a = 0, double b = 0) :x(a), y(b),logic_error(0){}
double xr(){ return x; }
double yr(){ return y; }
};
class bad_hmean:public base
{
public:
bad_hmean(double a,double b) :base(a,b){}
void what();
};
inline void bad_hmean::what()
{
std::cout << "You can't process Hmean() with x= " << xr() << ",y= " << yr() << std::endl;
}
class bad_gmean :public base
{
public:
bad_gmean(double a = 0, double b = 0) :base(a,b){}
void what();
};
inline void bad_gmean::what()
{
std::cout << "You can't process Gmean() with x= " << xr() << ",y= " << yr() << std::endl;
}
user.cpp
#include<iostream>
#include<cmath>
#include"mean.h"
using namespace std;
double hmean(double a, double b);
double gmean(double a, double b);
int main()
{
double x, y, z;
cout << "Enter two numbaers :\n";
while (cin >> x >> y)
{
try{z = hmean(x, y);
cout << "Hmean(" << x << "," << y << ")=" << z << endl;
cout << "Gmean(" << x << "," << y << ")=" << gmean(x,y) << endl;
}
catch(bad_hmean &h)
{
h.what();
break;
}
catch (bad_gmean &g)
{
g.what();
break;
}
}
cout << "Finished .\n";
}
double hmean(double a, double b)
{
if (a != -b)
return 2.0*a*b / (a + b);
else
throw bad_hmean(a,b);
}
double gmean(double a, double b)
{
if (a > 0 && b > 0)
return sqrt(a*b);
else
throw bad_gmean(a, b);
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++ Custom Control控件向父窗体发送对应的消息
- C++中拷贝构造函数的应用详解
- C++中引用(&)的用法与应用实例分析
- C++使用CriticalSection实现线程同步实例