/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:Annpion.cpp
*作者:王耀鹏
*完成日期:2015年6月20日
*版本号:v1.0
*
*问题描述:编写一个程序,求输入数的平方根。设置异常处理,当输入负数时采用异常处理机制给出提示。 .
*输入描述: 输入浮点数。
*输出描述:输出开方结果。
*/
#include <iostream>
#include <cmath>
using namespace std;
double Sqrt(double a)
{
if(a<0)
throw a;
return sqrt(a);
}
int main()
{
double a;
try
{
cout<<"请输入一个需要开方的数:";
cin>>a;
cout<<a<<"的平方根:"<<Sqrt(a)<<endl;
}
catch (double )
{
cout<<a<<" < 0 ,无法开方。"<<endl;
}
}
运行结果: