0;y--){ if(romanum.find(百分之[y])!=string::npos){ 合计+=y*100; input.erase(0,数百[y].length()); 打破 } } 对于(int y=9;y>0;y--){ if(romanNum.find(tens[y])!=string::npos){ 总数+=y*10; input.erase(0,十[y].length()); 打破 } } 对于(int y=9;y>0;y--){ if(romanum.find(单位[y])!=string::npos){ 总+=y; input.erase(0,单位[y].length()); 打破 } } cout,c++,C++" /> 0;y--){ if(romanum.find(百分之[y])!=string::npos){ 合计+=y*100; input.erase(0,数百[y].length()); 打破 } } 对于(int y=9;y>0;y--){ if(romanNum.find(tens[y])!=string::npos){ 总数+=y*10; input.erase(0,十[y].length()); 打破 } } 对于(int y=9;y>0;y--){ if(romanum.find(单位[y])!=string::npos){ 总+=y; input.erase(0,单位[y].length()); 打破 } } cout,c++,C++" />

使用C+中的find语句将罗马数字转换为数字+; 我在C++中有困难将罗马数字转换成正常数字,代码工作到一定程度,但是如果输入数字(XIV 14或LIV等),它将输出15或55。 我已经尝试实现find语句,但是我不知道如何使用它来解决我的问题,这里是我的代码到目前为止的副本 int convNum; int total = 0; string romanNum; const string units [10]= {"0","I","II","III","IV","V","VI","VII","VIII","IX"}; const string tens [10]= {"0","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}; const string hundreds [10]= {"0","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; const string thousands [4]= {"0","M","MM","MMM"}; string input; while(!cin.eof()){ cin>>romanNum; if(cin.fail()){ break; }else{ for(int i=0; i<romanNum.length(); i++){ romanNum[i]=toupper(romanNum[i]); } for(int y=3; y > 0; y--){ if(romanNum.find(thousands[y])!= string::npos){ total += y*1000; input.erase(0,thousands[y].length()); break; } } for(int y=9; y > 0; y--){ if(romanNum.find(hundreds[y])!= string::npos){ total += y*100; input.erase(0,hundreds[y].length()); break; } } for(int y=9; y > 0; y--){ if(romanNum.find(tens[y])!= string::npos){ total += y*10; input.erase(0,tens[y].length()); break; } } for(int y=9; y > 0; y--){ if(romanNum.find(units[y])!= string::npos){ total += y; input.erase(0,units[y].length()); break; } } cout << total << endl; total = 0; } for(int k=0; k < romanNum.length(); k++){ input[k] = romanNum[k]; } } return 0; int-convNum; int-total=0; 字符串罗马数字; 常量字符串单位[10]={“0”、“I”、“II”、“III”、“IV”、“V”、“VI”、“VII”、“VIII”、“IX”}; 常量字符串十[10]={“0”、“X”、“XX”、“XXX”、“XL”、“L”、“LX”、“LXX”、“XC”}; 常量字符串百[10]={“0”、“C”、“CC”、“CCC”、“CD”、“D”、“DC”、“DCC”、“DCCC”、“CM”}; 常量字符串千[4]={“0”、“M”、“MM”、“MMM”}; 字符串输入; 而(!cin.eof()){ 罗曼努姆; if(cin.fail()){ 打破 }否则{ 对于(int i=0;i 0;y--){ if(romanum.find(千[y])!=string::npos){ 合计+=y*1000; input.erase(0,千[y].length()); 打破 } } 对于(int y=9;y>0;y--){ if(romanum.find(百分之[y])!=string::npos){ 合计+=y*100; input.erase(0,数百[y].length()); 打破 } } 对于(int y=9;y>0;y--){ if(romanNum.find(tens[y])!=string::npos){ 总数+=y*10; input.erase(0,十[y].length()); 打破 } } 对于(int y=9;y>0;y--){ if(romanum.find(单位[y])!=string::npos){ 总+=y; input.erase(0,单位[y].length()); 打破 } } cout

使用C+中的find语句将罗马数字转换为数字+; 我在C++中有困难将罗马数字转换成正常数字,代码工作到一定程度,但是如果输入数字(XIV 14或LIV等),它将输出15或55。 我已经尝试实现find语句,但是我不知道如何使用它来解决我的问题,这里是我的代码到目前为止的副本 int convNum; int total = 0; string romanNum; const string units [10]= {"0","I","II","III","IV","V","VI","VII","VIII","IX"}; const string tens [10]= {"0","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}; const string hundreds [10]= {"0","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; const string thousands [4]= {"0","M","MM","MMM"}; string input; while(!cin.eof()){ cin>>romanNum; if(cin.fail()){ break; }else{ for(int i=0; i<romanNum.length(); i++){ romanNum[i]=toupper(romanNum[i]); } for(int y=3; y > 0; y--){ if(romanNum.find(thousands[y])!= string::npos){ total += y*1000; input.erase(0,thousands[y].length()); break; } } for(int y=9; y > 0; y--){ if(romanNum.find(hundreds[y])!= string::npos){ total += y*100; input.erase(0,hundreds[y].length()); break; } } for(int y=9; y > 0; y--){ if(romanNum.find(tens[y])!= string::npos){ total += y*10; input.erase(0,tens[y].length()); break; } } for(int y=9; y > 0; y--){ if(romanNum.find(units[y])!= string::npos){ total += y; input.erase(0,units[y].length()); break; } } cout << total << endl; total = 0; } for(int k=0; k < romanNum.length(); k++){ input[k] = romanNum[k]; } } return 0; int-convNum; int-total=0; 字符串罗马数字; 常量字符串单位[10]={“0”、“I”、“II”、“III”、“IV”、“V”、“VI”、“VII”、“VIII”、“IX”}; 常量字符串十[10]={“0”、“X”、“XX”、“XXX”、“XL”、“L”、“LX”、“LXX”、“XC”}; 常量字符串百[10]={“0”、“C”、“CC”、“CCC”、“CD”、“D”、“DC”、“DCC”、“DCCC”、“CM”}; 常量字符串千[4]={“0”、“M”、“MM”、“MMM”}; 字符串输入; 而(!cin.eof()){ 罗曼努姆; if(cin.fail()){ 打破 }否则{ 对于(int i=0;i 0;y--){ if(romanum.find(千[y])!=string::npos){ 合计+=y*1000; input.erase(0,千[y].length()); 打破 } } 对于(int y=9;y>0;y--){ if(romanum.find(百分之[y])!=string::npos){ 合计+=y*100; input.erase(0,数百[y].length()); 打破 } } 对于(int y=9;y>0;y--){ if(romanNum.find(tens[y])!=string::npos){ 总数+=y*10; input.erase(0,十[y].length()); 打破 } } 对于(int y=9;y>0;y--){ if(romanum.find(单位[y])!=string::npos){ 总+=y; input.erase(0,单位[y].length()); 打破 } } cout,c++,C++,看起来您有两个问题: 首先,当您删除找到的数字时,您是从输入字符串中删除,而不是从romanum字符串中删除。您应该从romanum字符串中删除: romanNum.erase(0, thousands[y].length()); 其次,它看起来像是在字符串中的任何位置搜索结果,而不仅仅是在开始处。因此,在“LIV”的示例中,当您在units列表中搜索时,它会在列表的末尾找到“V”,加上5,然后它会删除“I”(因为它总是从列表的前面删除。解决方法之一是只接受当前字符串开头的结果。因此,不要执行

看起来您有两个问题:

首先,当您删除找到的数字时,您是从
输入
字符串中删除,而不是从
romanum
字符串中删除。您应该从
romanum
字符串中删除:

romanNum.erase(0, thousands[y].length());
其次,它看起来像是在字符串中的任何位置搜索结果,而不仅仅是在开始处。因此,在“LIV”的示例中,当您在
units
列表中搜索时,它会在列表的末尾找到“V”,加上5,然后它会删除“I”(因为它总是从列表的前面删除。解决方法之一是只接受当前字符串开头的结果。因此,不要执行
!=string::npos
,只需执行
==0

if (romanNum.find(thousands[y]) == 0) {

我不会为您进行调试,我只确定三个问题

  • 您正在
    romanum
    中搜索,但正在删除从
    input
    中找到的字符。这不应该是同一个字符串吗

  • 您得到15是因为在数字中找到的第一个
    单元
    字符串是
    “V”
    ,而不是
    “IV”
    ,因为您以相反的顺序迭代

  • 您不应该查找作为数字前缀的字符串吗?不在其中的任何位置。您希望
    find
    方法返回0,而不是其他任何内容


  • 你可以使用不同的技术来实现它吗?对我来说,首先想到的是一个状态机。不,我不这么认为,(对不起,我甚至不知道状态机是什么)好的,我应该对我所有的For语句进行修改,对吗?(我知道这是一个明显的问题,我现在只是没有直接思考)不管我最后的回复是什么,我已经实施了这些更改,看起来很有效。谢谢你的帮助。