C++ 将罗马数字转换为整数-输出错误-为什么?

C++ 将罗马数字转换为整数-输出错误-为什么?,c++,enums,roman-numerals,C++,Enums,Roman Numerals,这是我的密码。首先,我想说,我一直在试验,所以如果你在这里和那里看到不必要的变量,这就是为什么。但是我的代码的主要部分在我的类romanType中的函数decimal中。当我输入某些罗马数字时,我没有得到我想要的确切数字,它可能在if/else语句的逻辑中的某个地方 顺便说一下,为了演示如何遍历字符串,我通过反向遍历来完成。我从字符串的最末端到最开始,我认为用罗马数字更容易。顺便说一句,我还制作了一个枚举类型,这样我就可以比较罗马数字,看哪一个更小,哪一个更大等等。然后我使用了一个映射来比较枚举

这是我的密码。首先,我想说,我一直在试验,所以如果你在这里和那里看到不必要的变量,这就是为什么。但是我的代码的主要部分在我的类romanType中的函数decimal中。当我输入某些罗马数字时,我没有得到我想要的确切数字,它可能在if/else语句的逻辑中的某个地方

顺便说一下,为了演示如何遍历字符串,我通过反向遍历来完成。我从字符串的最末端到最开始,我认为用罗马数字更容易。顺便说一句,我还制作了一个枚举类型,这样我就可以比较罗马数字,看哪一个更小,哪一个更大等等。然后我使用了一个映射来比较枚举值和char值

所以问题是:例如,当我输入CCC时,我得到的是290而不是300。如果你知道我的逻辑有什么问题,我将非常感激!多谢各位

此外,我对编程还很陌生,如果我在编写这段代码时错过了任何关于类等的风格技巧或任何我能学到的东西,我将不胜感激?请让我知道什么是最好的。多谢各位

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

using namespace std;

class romanType {
string numeral;
int k;
 public:
romanType();
void rnumeral (string b) {numeral = b;}

int decimal(string num, char b, int temp) {
    num = "";


    enum RomanNumerals {I, V, X, L, C, D, M };

    map<char, RomanNumerals> m;

    m['I'] = I;
    m['V'] = V;
    m['X'] = X;
    m['L'] = L;
    m['C'] = C;
    m['D'] = D;
    m['M'] = M;


    RomanNumerals roman1;
    RomanNumerals roman2;


    cout << "Please type in your roman numeral:" ;

    cin >> num;

    for (int i =0; i <num.length()-1; i++){

    }

for(long i = num.length()-1; i>=0; i--)

{
b = num[i];
if (islower(b)) b=toupper(b);

roman1 = m[num[i]];
roman2 = m[num[i-1]];

switch(b){


            case 'I':
                    if(num[i] == num.length()-1){

                            temp += 1;
                            }
                        break;

            case 'V':

                if(roman1 > roman2){
                        temp += 4;
                        continue;
                        }

                else {
                    temp += 5;

                }

                break;


            case 'X':

                if(roman1 > roman2){
                    temp += 9;
                    continue;
                    }

            else {
                temp += 10;
                }

                break;

            case 'L' :

                if(roman1 > roman2){
                        temp += 40;
                        continue;
                    }

                else {
                    temp += 50;
                    }
                break;

            case 'C':

            if(roman1 > roman2){
                temp += 90;
                continue;
            }

            else {
                temp += 100;
            }

                break;

            case 'D' :
            if(roman1 > roman2){
                temp += 400;
                continue;
                }

            else {
                temp += 500;
                }
                break;

            case 'M':
            if(roman1 > roman2){
                temp += 900;
                continue;
                }

            else {
                temp += 1000;
                }
            break;

        }

    }


    return temp;
}
};

romanType::romanType () {
numeral = "";
}


int main() {
string k = "";
char b = ' ';
int temp = 0;

romanType type;
type.rnumeral(k);

int c = type.decimal(k, b, temp);

cout << c;


return 0;
}
#包括
#包括
#包括
使用名称空间std;
类罗马类型{
字符串数字;
int k;
公众:
romanType();
无效数字(字符串b){数字=b;}
整数十进制(字符串数、字符b、整数温度){
num=“”;
枚举罗马数字{I,V,X,L,C,D,M};
地图m;
m['I']=I;
m['V']=V;
m['X']=X;
m['L']=L;
m['C']=C;
m['D']=D;
m['m']=m;
罗马数字罗马1;
罗马数字罗马2;
cout>num;
对于(int i=0;i=0;i--)
{
b=num[i];
if(islower(b))b=toupper(b);
roman1=m[num[i]];
roman2=m[num[i-1]];
开关(b){
案例“I”:
如果(num[i]==num.length()-1){
温度+=1;
}
打破
案例“V”:
如果(罗马1>罗马2){
温度+=4;
继续;
}
否则{
温度+=5;
}
打破
案例“X”:
如果(罗马1>罗马2){
温度+=9;
继续;
}
否则{
温度+=10;
}
打破
案例“L”:
如果(罗马1>罗马2){
温度+=40;
继续;
}
否则{
温度+=50;
}
打破
案例“C”:
如果(罗马1>罗马2){
温度+=90;
继续;
}
否则{
温度+=100;
}
打破
案例“D”:
如果(罗马1>罗马2){
温度+=400;
继续;
}
否则{
温度+=500;
}
打破
案例“M”:
如果(罗马1>罗马2){
温度+=900;
继续;
}
否则{
温度+=1000;
}
打破
}
}
返回温度;
}
};
romanType::romanType(){
数字=”;
}
int main(){
字符串k=“”;
字符b=“”;
内部温度=0;
罗马式;
数字型(k);
int c=十进制类型(k,b,temp);
cout=0;i--)
{
b=num[i];
if(islower(b))b=toupper(b);
roman1=m[num[i]];
roman2=m[num[i-1]];
罗马3=m[num[i+1]];
开关(b){
案例“I”:
如果(roman3>roman1&&i!=num.length()-1){
继续;
}
否则{
温度+=1;
打破
}
案例“V”:
如果(roman1>roman2&&i!=0){
温度+=4;
继续;
}
否则{
温度+=5;
}
打破
案例“X”:
如果(roman3>roman1&&i!=num.length()-1)
继续;
如果(roman1>roman2&&i!=0){
温度+=9;
继续;
}
否则{
温度+=10;
}
打破
案例“L”:
如果(roman1>roman2&&i!=0){
温度+=40;
继续;
}
否则{
温度+=50;
}
打破
案例“C”:
如果(roman3>roman1&&i!=num.length()-1)
继续;
如果(roman2==X&&i!=0){
温度+=90;
继续;
}
否则{
温度+=100;
}
打破
案例“D”:
如果(roman2==C&&i!=0){
温度+=400;
继续;
}
否则{
温度+=500;
}
打破
案例“M”:
如果(roman2==C&&i!=0){
温度+=900;
继续;
}
否则{
温度+=1000;
}
打破
}
}
temp2=temp;
}
void showDecimal(){

cout当我完成了我的评论并稍微调整了你的代码时,我得到了以下信息:

//---------------------------------------------------------------------------
int-roman_ix[256]={-1};
const int roman_val[]={1,5,10,501005001000,0};
const char roman_chr[]={'I','V','X','L','C','D','M',0};
//---------------------------------------------------------------------------
int-roman2int(字符*s)
{
int i,x=0,v=0,v0;
//初始化表(仅一次)
if(roman_ix[0]v)x-=v;else x+=v;//ad
#include <iostream>
#include <string>
#include <map>

using namespace std;

string acceptRN();



class romanType {

string numeral;
int temp2;
int l;
// VARIABLES
public:

romanType();
//DEFAULT CONSTRUCTOR


void getRnumeral (string b)
    {
        numeral = b;
    }
//SETTER

void decimal(string num, int temp, char b) {

    num = numeral;

    enum RomanNumerals {I, V, X, L, C, D, M };

    map<char, RomanNumerals> m;

    m['I'] = I;
    m['V'] = V;
    m['X'] = X;
    m['L'] = L;
    m['C'] = C;
    m['D'] = D;
    m['M'] = M;


    RomanNumerals roman1;
    RomanNumerals roman2;
    RomanNumerals roman3;





for(long i = num.length()-1; i>=0; i--)

{
b = num[i];
if (islower(b)) b=toupper(b);

roman1 = m[num[i]];
roman2 = m[num[i-1]];
roman3 = m[num[i+1]];

switch(b){


        case 'I':

        if( roman3 > roman1 && i != num.length()-1){
            continue;
        }

        else {
                temp += 1;
                        break;
        }



            case 'V':

                if(roman1 > roman2 && i != 0){
                        temp += 4;
                        continue;
                        }

                else {
                    temp += 5;

                }

                break;


            case 'X':

        if( roman3 > roman1 && i != num.length()-1)
            continue;

        if(roman1 > roman2 && i!= 0){
            temp += 9;
            continue;
                    }

            else {
                temp += 10;
                }

                break;

            case 'L' :

                if(roman1 > roman2 && i!= 0){

                        temp += 40;
                        continue;
                    }



                else {
                    temp += 50;
                    }
                break;

            case 'C':

            if( roman3 > roman1 && i != num.length()-1)
                continue;

            if(roman2 == X && i!= 0){
                temp += 90;
                continue;
            }

            else {
                temp += 100;
            }

                break;

            case 'D' :
            if(roman2 == C && i!= 0){
                temp += 400;
                continue;
                }

            else {
                temp += 500;
                }
                break;

            case 'M':
            if(roman2 == C && i!= 0){
                temp += 900;
                continue;
                }

            else {
                temp += 1000;
                }
            break;


        }

    }
 temp2 = temp;
}

void showDecimal() {

cout << "Here is your roman numeral in decimal format:";
cout << temp2 << " \n \n \n";
}

};

romanType::romanType () {
numeral = "";



}


int main() {
string k = acceptRN();
int m = 0;
char l= ' ';

romanType type;
type.getRnumeral(k);
type.decimal(k, m, l);
type.showDecimal();


return 0;
}

string acceptRN(){
string num = "";
cout << "Please type in your roman numeral:" ;
cin >> num;

return num;

}
1776 1776 MDCCLXXVI
1954 1954 MCMLIV
1990 1990 MCMXC
2014 2014 MMXIV
 300  300 CCC
//---------------------------------------------------------------------------
int roman2int(char *s)
    {
    // init
    int i,x=0,v=0,v0;       // A  B    C    D  E  F  G  H  I  J  K   L     M  N  O  P  Q  R  S  T  U  V  W   X  Y  Z
    const int val['Z'-'A'+1]={ 0, 0, 100, 500, 0, 0, 0, 0, 1, 0, 0, 50, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 10, 0, 0 };
    // find end of string
    for (i=0;s[i];i++);
    // process string in reverse
    for (i--;i>=0;i--)
        {
        if ((s[i]<'A')||(s[i]>'Z')) break; // stop on non supported character
        v0=v; v=val[s[i]-'A'];
        if (v0>v) x-=v; else x+=v;
        }
    return x;
    }
//---------------------------------------------------------------------------