在C ++中将字符串转换为罗马数字

时间:2012-09-07 18:11:15

标签: c++ string-parsing

  

可能重复:
  How to convert integer value to Roman numeral string?

第一次在这里问一个问题。我有一个项目在地平线上,我一直在努力,似乎无法在这里或其他地方找到任何类似的问题。目标是接受没有上限约束的整数,并将其转换为罗马数字。由于整数实际上具有上边界,因此我必须在解析之前将其转换为字符串,使用撇号来表示放置在子字符串中的每个三个字符集。我无法概念化循环a)根据位置将罗马数字分配给他们所看到的内容b)通过显示撇号计算每组三个。

到目前为止,我有:

for(int i=0;i<(int)input.length()/3;i++){
    temp=input.substr(i,3);
    for(int j = 0; j < (int)temp.length(); j++){
        if(j == 0){
            if(temp[j] == '9') cout<<"CM";
            else if(temp[j] >= '8') cout<<"DCCC";
            else if(temp[j] >= '7') cout<<"DCC";
            else if(temp[j] >= '6') cout<<"DC";
            else if(temp[j] >= '5') cout<<"D";
            else if(temp[j] == '4') cout<<"CD";
            else if(temp[j] == '3') cout<<"CCC";
            else if(temp[j] == '2') cout<<"CC";
            else if(temp[j] == '1') cout<<"C";  
        }
        else if(j == 1){
            if(temp[j] == '9') cout<<"XC";
            else if(temp[j] >= '8') cout<<"LXXX";
            else if(temp[j] >= '7') cout<<"LXX";
            else if(temp[j] >= '6') cout<<"LX";
            else if(temp[j] >= '5') cout<<"L";
            else if(temp[j] == '4') cout<<"XL";
            else if(temp[j] == '3') cout<<"XXX";
            else if(temp[j] == '2') cout<<"XX";
            else if(temp[j] == '1') cout<<"X";
        }
        else if(j ==2){
            if(temp[j] == '9') cout<<"IX";
            else if(temp[j] == '8') cout<<"VIII";
            else if(temp[j] == '7') cout<<"VII";
            else if(temp[j] == '6') cout<<"VI";
            else if(temp[j] >= '5') cout<<"V";
            else if(temp[j] == '4') cout<<"IV";
            else if(temp[j] == '3') cout<<"III";
            else if(temp[j] == '2') cout<<"II";
            else if(temp[j] == '1') cout<<"I";
        }
    }
}

这些数字本身显示得足够好,但是我很难弄清楚如何告诉循环从右边开始,然后按三分之一的方式工作,保持数字在输入中的实际位置(例如,1234应该显示1作为I,而不是C.我还需要找出要在撇号中写入的循环。

2 个答案:

答案 0 :(得分:1)

我能想到的最简单的转换为罗马数字的方法是从最大可能的数字/组合开始检查并减少工作量。包括组合,并检查它们从大到小,以便说“XC”总是在“L”之前检查,你不必担心“LXXXX”和“LXL”等。

// This code requires C++11 support.  Namely, initializer lists and type inference.
// If your compiler sucks, there's equivalents for the important stuff.  What really
// really matters is the order of the value=>digits mappings, and the iteration over
// them in the for loop.

#include <vector>
#include <string>
#include <utility>

std::string romanNumeralFor(int n, int markCount = 0) {
    typedef std::pair<int, std::string> valueMapping;
    static std::vector<valueMapping> importantNumbers = {
        {1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"},
        {100,  "C"}, { 90, "XC"}, { 50, "L"}, { 40, "XL"},
        {10,   "X"}, {  9, "IX"}, {  5, "V"}, {  4, "IV"},
        {1,    "I"},
    };

    std::string result;
    bool needMark = false;
    std::string marks(markCount, '\'');
    for (auto mapping : importantNumbers) {
        int value = mapping.first;
        std::string &digits = mapping.second;
        while (n >= value) {
            result += digits;
            n -= value;
            needMark = true;
        }
        if ((value == 1000 || value == 100 || value == 10 || value == 1) && needMark) {
            result += marks;
            needMark = false;
        }
    }
    return result;
}

至于将字符串转换为数字:

// in C++11
int n = std::stoi(str);

// in C++03
std::istringstream iss(str);
int n;
iss >> n;

所以,将你的字符串分成三位数的块(从结尾开始!),并用适当的标记计数传递它们。

答案 1 :(得分:0)

不是最好的解决方案,但它有效。

#include <iostream>
#include <map>
#include <algorithm>
#include <string>

void replaceAll(std::string& str, const std::string& from, const std::string& to) {
    if(from.empty())
        return;
    size_t start_pos = 0;
    while((start_pos = str.find(from, start_pos)) != std::string::npos) {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length();
    }
}

int romanNumeralToInt(std::string s) {
    std::map<char,int> romanNum = {
        {'I',1}, {'V',5}, {'X',10}, {'L',50}, {'C',100},
        {'D',500}, {'M',1000}
    };
    //{"IIIII",5}, {"VV",10}, {"XXXXX",50}, {"LL",100}, {"CCCCC",500}, {"DD",1000}
    int g = 0;
    std::sort(s.begin(),s.end());
    if(s.find("IIIII") != std::string::npos)
        replaceAll(s,"IIIII","V");
    if(s.find("VV") != std::string::npos)
        replaceAll(s,"VV","X");
    if(s.find("XXXXX") != std::string::npos)
        replaceAll(s,"XXXXX","L");
    if(s.find("LL") != std::string::npos)
        replaceAll(s,"LL","C");

    for(auto& i : s) {
        if(romanNum[i] != 0)
            g += romanNum[i];
    }
    return g;
}

int main() {
    std::string st = "XXXXXIIIIIVVVXLLD";
    int a = romanNumeralToInt(st);
    std::cout << a;
}

打印680。