问题描述
错误:
该代码对于正常的罗马到数字的转换很有效,但是如果有任何随机字符串输入,则不会显示预期的输出。
string convert_string( string input){
for(int i=0; i < input.length(); i++){
input[i] = toupper(input[i]);
}
return input;
}
int roman_to_int(string RomanChars){
RomanChars = convert_string(RomanChars);
int total = 0;
int max_value= 0;
std::map<char,int> conversion;
conversion['M']=1000;
conversion['D']=500;
conversion['C']=100;
conversion['L']=50;
conversion['X']=10;
conversion['V']=5;
conversion['I']=1;
for( size_t i = RomanChars.length()-1; i != std::string::npos; --i){
auto val = conversion.find(RomanChars[i]);
if(val != conversion.end())
{
if(val->second >= max_value)
{
total += val->second;
max_value = val->second;
} else {
total -= val->second;
}
}
}
return total;
}
int main() {
string character;
int conversion = 0;
while(cin >> character){
conversion = roman_to_int(character);
cout << conversion <<endl;
}
return 0;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)