Roman numerals in C + + Convert to integer number---"Those strange Algorithms" __web

Source: Internet
Author: User
Tags integer numbers

refer to "One-day-one-leetcode"

In the previous blog we discussed the conversion of numbers within 1~3999 to Roman numerals, highlighting some of the expressions of non-7 standard Roman numerals, such as 4,6,3 and so on, which we talked about converting Roman numerals into integer numbers.
In fact, we are in the conversion of the main attention to 4,40,400, such as the number of expression can be.

Code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int tonum (char ch);
int Romantoint (string s) {
    int ret = 0;
    int i = 0;
    while (I < s.length ()) {
        if (i + 1 < s.length () && tonum (s[i)) < Tonum (s[i + 1])) {
            ret = Tonum (s [i + 1]) -Tonum (S[i]);
            i++;
        }
        else{
            ret + tonum (s[i]);
        }
        i++;
    }
    return ret;
}
int tonum (char ch) {
    int num = 0;
    Switch (CH) {case
    ' I ': num = 1;
    Case ' V ': num = 5; break;
    Case ' X ': num = 10; break;
    Case ' L ': num = 50; break;
    Case ' C ': num = 100; break;
    Case ' D ': num = 500; break;
    Case ' M ': num = 1000; break;
    return num;
}
int main () {
    string s = ' mmmdcclxxii ';
    int con = Romantoint (s);
    cout << con << endl;
    return 0;
}

Run Result:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.