#include "stdafx.h"
#include <iostream>
double hmean(double a, double b);
int main()
{
double x, y, z;
std::cout << "Enter two numbers: ";
while (std::cin >> x >> y) {
try
{
z = hmean(x, y);
}
catch(const char *s ){
std::cout << s << std::endl;
std::cout << " Enter a new pair of numbers: ";
continue;
}
std::cout << "Harmonic mean of " << x << " and " << y << " is " << z << std::endl;
std::cout << "Enter next set of numbers <q to quit>: ";
}
std::cout << "Bye! \n";
system("pause");
return 0;
}
double hmean(double a, double b) {
if (a == -b) {
throw "bad hmean() arguments a= -b not allowed";
}
return 2.0 *a*b / (a + b);
}
Enter two numbers: 3 6
Harmonic mean of 3 and 6 is 4
Enter next set of numbers <q to quit>: 10 -10
bad hmean() arguments a= -b not allowed
Enter a new pair of numbers: q
Bye!
请按任意键继续. . .
try
{
z = hmean(x, y);
}
if (a == -b) {
throw "bad hmean() arguments a= -b not allowed";
}
catch(const char *s ){
std::cout << s << std::endl;
std::cout << " Enter a new pair of numbers: ";
continue;
}
#include "stdafx.h"
#include <iostream>
class bad_hmean
{
private:
double v1;
double v2;
public :
bad_hmean(double a = 0, double b = 0) :v1(a), v2(b) {}
void mesg();
};
inline void bad_hmean::mesg()
{
std::cout << "hmean ( " << v1 << " ," << v2 << ") ;"
<< "invalid argumnents: a =-b \n";
}
class bad_gmean
{
public :
double v1;
double v2;
bad_gmean(double a = 0, double b = 0) :v1(a), v2(b) {}
const char * mesg();
};
inline const char* bad_gmean::mesg() {
return "gmean() arguments shoud be >=0 \n";
}
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include "exc_mean.h"
double hmean(double a, double b);
double gmean(double a, double b);
int main()
{
using std::cout;
using std::cin;
using std::endl;
double x, y, z;
1 >> 2;
cout << "Enter two numbers ";
while (cin >> x >> y)
{
try
{
z = hmean(x, y);
cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl;
cout << " Geometric mean of " << x << " and " << y << " is " << gmean(x, y) << endl;
cout << " Enter next set of numbers <q to quit >:";
}
catch (bad_hmean & bg)
{
bg.mesg();
cout << "Try again. \n";
continue;
}
catch (bad_gmean & hg)
{
cout << hg.mesg();
cout << "Value used: " << hg.v1 << " ," << hg.v2 << endl;
cout << "Sorry, you don't get to play any more .\n ";
break;
}
}
cout << " Bye! \n";
system("pause");
return 0;
return 0;
}
double hmean(double a, double b) {
if (a == -b)
throw bad_hmean(a, b);
return 2.0 * a*b / (a + b);
}
double gmean(double a, double b) {
if (a < 0 || b < 0) throw bad_gmean(a, b);
return std::sqrt(a * b);
}
输出结果:
Enter two numbers 4 12
Harmonic mean of 4 and 12 is 6
Geometric mean of 4 and 12 is 6.9282
Enter next set of numbers <q to quit >:5 -5
hmean ( 5 ,-5) ;invalid argumnents: a =-b
Try again.
5 -2
Harmonic mean of 5 and -2 is -6.66667
gmean() arguments shoud be >=0
Value used: 5 ,-2
Sorry, you don't get to play any more .
Bye!
请按任意键继续. . .
double harm(double a ) throw(bad_thing);//可能会抛出 bad_thing异常 double marm(double ) throw() ;//不抛出异常
double marm() noexcept;
typedef void (*terminate_handle)() ; terminate_handle set_terminate(terminate_handle f) throw();//c++ 98 terminate_handle set_terinate(terminate_handle f) noexcept; //c++11 void teminate(); //c++98 void teminate() noexcept ; //c++11
void myQuit()
{
std::cout << "Terminating due to uncaught exception \n";
system("pause");
}
在程序开始时执行:
set_terminate(myQuit);
typedef void (* unexpected_handle)(); unexpected_handle set_unexpected(unexpected_handle f) throw();//c++98 unexpected_handle set_unexpected(unexpected_handle f) noexpect;//c++11 void unexpected(); c++ 98 void unexpected() noexcept ;//c+ 0x
void myUnexpected() {
throw std::bad_exception(); // or just throw ;
}
在程序开始时:
set_unexpected(myUnexpected);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有