题目:
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;
}