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

Converting a string to roman numerals in C++

本文关键字:罗马 数字 转换 字符串 C++      更新时间:2023-10-16

可能重复:
如何将整数值转换为罗马数字字符串?

第一次在这里提问。我有一个项目就在眼前,我一直在努力,似乎在这里或其他地方都找不到它要求的东西。目标是接受一个没有上限限制的整数,并将其转换为罗马数字。由于整数实际上有一个上界,所以在解析它之前,我必须将其转换为字符串,使用撇号表示放置在子字符串中的每组三个字符。我在概念化循环时遇到了困难,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。我还需要弄清楚用撇号写循环。

我能想到的转换为罗马数字的最简单方法是从尽可能大的数字/组合开始检查并向下计算。包括combo,并从最大到最小进行检查,这样说"XC"总是在"L"之前进行检查,你就不必担心"LXXXX"answers"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;

所以,把你的字符串分成三位数的块(从末尾开始!),并用适当的标记数把它们传进去。

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

#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。