oj:进制转换

来源:互联网 发布:通讯软件排名 编辑:程序博客网 时间:2024/05/16 10:36

题目描述

写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )

输入描述:
输入一个十六进制的数值字符串。

输出描述:
输出该数值的十进制字符串。

输入例子:
0xA

输出例子:
10

思路: 利用字符串搜索函数 s.find_first_of(arg),找到“x或X” 出现的位置(C++ Primer P325),然后用substr提取子串,最后通过 字符串数值转换 函数 (C++ Primer P327)

string的搜索操作很多;详解见C++ Primer P325
比如:找一个字符串数字出现的位置
一个字符串中不是数字的位置,等等;

#include <iostream>#include <string>using namespace std;int main(){    string str;    while (cin >> str)    {        auto pos = str.find_first_of("xX");         string s = str.substr(pos + 1);        auto n = stoul(s,0,16);        cout << n << endl;    }    system("pause");    return 0;}
0 0