chapter15test2
2015-07-01 15:41
429 查看
这个土木我看了好久才看明白,自己尝试着写出来的程序,和大神的对照了一下,差不多,和树上的也相似只是在头文件里面添加了一个exception, logic_error,程序如下:
mean.h
#include<iostream>
#include<stdexcept>
class bad_hmean:public std::logic_error
{
private:
double x;
double y;
public:
bad_hmean(int a = 0, int b = 0) :x(a), y(b),logic_error(0){}
void what();
};
inline void bad_hmean::what()
{
std::cout << "You can't process Hmean(),for x=-y.\n";
}
class bad_gmean :public std::logic_error
{
private:
double x, y;
public:
bad_gmean(double a = 0, double b = 0) :x(a), y(b),logic_error(0){}
void what();
};
inline void bad_gmean::what()
{
std::cout << "You can't process Gmean() now,for x=0 or y=0.\n";
}
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();
cout << "Try again!\n";
continue;
}
catch (bad_gmean &g)
{
g.what();
cout << "Try again!\n";
continue;
}
}
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);
}
mean.h
#include<iostream>
#include<stdexcept>
class bad_hmean:public std::logic_error
{
private:
double x;
double y;
public:
bad_hmean(int a = 0, int b = 0) :x(a), y(b),logic_error(0){}
void what();
};
inline void bad_hmean::what()
{
std::cout << "You can't process Hmean(),for x=-y.\n";
}
class bad_gmean :public std::logic_error
{
private:
double x, y;
public:
bad_gmean(double a = 0, double b = 0) :x(a), y(b),logic_error(0){}
void what();
};
inline void bad_gmean::what()
{
std::cout << "You can't process Gmean() now,for x=0 or y=0.\n";
}
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();
cout << "Try again!\n";
continue;
}
catch (bad_gmean &g)
{
g.what();
cout << "Try again!\n";
continue;
}
}
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实现线程同步实例