2016/5/27 1000. Hex to decimal
来源:互联网 发布:淘宝客服脐橙问题总结 编辑:程序博客网 时间:2024/05/17 02:47
本题考查的是try throw catch 三连发的套路,f()里已经有了try和catch,需要写的就是出错的时候throw一个对象。而runtime_error是在<ctdexcept>的头文件里的类。它的what也是一般的返回字符串。
#include <iostream>#include <string>#include <cmath>#include <stdexcept>using namespace std;int judge(char a){if (a <= 57 && a >= 48){return 1;}else if (a >= 65 && a <= 70){return 2;}else{return 0;}}int parseHex(string s){int result = 0;for (int i = s.length() - 1; i >= 0; i--){if (judge(s[i]) == 0){s = "Hex number format error\n";throw (runtime_error)(s);}else if (judge(s[i]) == 1){result += ((s[i] - 48) * pow(16, s.length() - 1 - i));}else if (judge(s[i]) == 2){result += ((s[i] - 55) * pow(16, s.length() - 1 - i));}}return result;}void f(){try{cout << parseHex("10") << endl;cout << parseHex("ABC") << endl;cout << parseHex("9G3") << endl;}catch (runtime_error ex){cout << ex.what() << endl;}}int main(){f();return 0;}需要注意的是为了方便判断十六进制的所给字符串,写了一个judge函数去判断每一个字符。
0 0
- 2016/5/27 1000. Hex to decimal
- python to print hex of a decimal
- 2016/5/27 1001. Binary to decimal
- convert decimal to binary
- FLTP convert to Decimal
- java decimal to percentage
- Decimal to Binary
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to recurring decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Faction to Recurring Decimal
- Fraction to Recurring Decimal
- 今天对二级指针的深入理解
- Android Studio 查看Shar1码
- Java学习笔记之Java中的包
- fm单体声和立体声的切换
- 【深度学习】聚焦机制DRAM(Deep Recurrent Attention Model)算法详解
- 2016/5/27 1000. Hex to decimal
- WebService笔记(二):简介
- iOS获取当前 年月日和星期
- bootstrap-datetimepicker的bug整理
- 设计模式-策略模式
- Android 探究 LayoutInflater setFactory
- 自定义UI的时候,通常到CGContextRef进行绘图,以下为详细API的叙述
- 从事C和C++开发者快速学python(一)
- background-position的百分比是怎么计算定位的