异常——排除表达式的异常

题目:

Problem Description

设有表达式 (a+b/c),对其开根号,计算表达式的值,要求能排除除数为0及根号小于0的异常。

Input

输入数据有多组。

Output

对于每组输入数据,输出占两行(具体输出格式参见输出样例)

Sample Input

5 -100 2
23 35 0
43 75 15

Sample Output

a=5,b=-100,c=2
Negative numbers can not square root!
a=23,b=35,c=0
Divisor can not be 0!
a=43,b=75,c=15
The result is:6.9282

 

参考代码:

#include <iostream>
#include <cmath>
using namespace std;

double fun(double a,double b,double c)
{
 if(a+b/c<0)
  throw b;
 if(c==0)
  throw 1;
 return sqrt(a+b/c);
}

int main()
{
 double a,b,c,d;
 
 while(cin>>a>>b>>c)
 {
  cout<<"a="<<a<<",b="<<b<<",c="<<c<<endl;
  try
  {
   d=fun(a,b,c);
   cout<<"The result is:"<<d<<endl;
  }
  catch(int)
  {
   cout<<"Divisor can not be 0!\n";
  }
  catch(double)
  {
   cout<<"Negative numbers can not square root!\n";
  }
 }
 
 return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值