第十六周 项目一-平方根中的异常

/*
 * Copyright (c) 2014, 烟台大学计算机学院
 * All rights reserved.
 * 文件名:test.cpp
 * 作    者:冷基栋  
 * 版 本 号:v1.0
 * 完毕日期:2015年 6 月 27日
 * 问题描写叙述:
 * 输入描写叙述:编写一个程序,求输入数的平方根。设置异常处理。当输入负数时採用异常处理机制给出提示。
 * 程序输出:
 */
#include<iostream>
#include<cmath>
using namespace std;
double squart(int &x);
int main()
{
    int n;
    try
    {
            cout<<"请输入一个数:"<<endl;
            cin>>n;
            cout<<n<<"的平方根为"<<squart(n)<<endl<<endl;
    }
    catch(int)
    {
        cout<<"无法开方!

"<<endl<<endl; } return 0; } double squart(int &x) { double n; if(x>=0) n=sqrt(x); else throw x; return n; }


执行结果:

学习心得:

自上而下

原文地址:https://www.cnblogs.com/lytwajue/p/6786241.html