第五章 5.6.3节练习
来源:互联网 发布:sql insert update 编辑:程序博客网 时间:2024/06/05 16:39
练习5.23
编写一段程序,从标准输入读取两个整数,输入第一个数除以第二个数的结果。
解答:
#include <iostream>using namespace std;int main(){int a, b;cin >> a >> b;cout << a / b << endl;}
练习5.24
修改你的程序,使得当第二个数是0时抛出异常。先不要设定catch子句,运行程序并真的为除数输入0,看看会发生什么?
解答:
#include <iostream>#include <stdexcept>using namespace std;int main(){int a, b;cin >> a >> b;if (b == 0){throw range_error("The denominator is 0");}cout << a / b << endl;}
练习5.26
修改上一题的程序,使用try语句块去捕捉异常。catch子句应该为用户输出一条提示信息,询问其是否输入新数并重新执行try语句块的内容。
解答:
<pre name="code" class="cpp">#include <iostream>#include <stdexcept>#include <string>using namespace std;int main(){int a, b;string choose;try{cin >> a >> b;if (b == 0){throw range_error("The denominator is 0");}}catch (range_error &e){cout << e.what()<< endl;while (1){cout << "Do you want to type other num for denominator?[y/N]" << endl;cin >> choose;if (choose.at(0) == 'y' || choose.at(0) == 'Y'){cin >> b;if (b == 0){cout << "The denominator is 0" << endl;continue;}else{break;}}else{break;}}}cout << a / b << endl;}
0 0
- 第五章 5.6.3节练习
- 第五章 5.1节练习
- 第五章 5.4.4节练习 & 5.5.1节练习 & 5.5.2节练习 & 5.5.3节练习
- C++ Primer 第五版 5.51节&5.6.3节练习
- C++Primer第五版 5.6.3节练习
- 第五章 5.3.2节练习
- HTML第五章上机练习3
- 第五章 编程练习
- 第五章练习
- 第五章练习
- Python 第五章练习
- 第五章 5.2节练习 & 5.3.1节练习
- 第五章 5.4.1节练习 & 5.4.2节练习
- 人力资源第五章练习解答
- simply scheme 第五章 练习
- 第五六章练习总结
- 【慕课笔记】第五章 阶段练习 第3节 使用Eclipse调试程序
- 《算法导论》第五章-第3节_练习(参考答案)
- UISegmentControl控件的简单使用
- 纪念acm 2014 牡丹江邀请赛
- hdu 4983 欧拉函数
- next_permutation函数
- hdu 4472 dp
- 第五章 5.6.3节练习
- C#实现全局快捷键(系统热键)响应(转)
- Spark技术内幕: Task向Executor提交的源码解析
- Nginx源码学习(第零章):nginx安装
- arm汇编—ldr加载指令,ldr伪指令
- Cocos2dx 中anchorPoint锚点和scale缩放之间的配合使用
- Android 如何通过相册和相机来获取图片
- CSS初学笔记
- 关于adr指令的理解