嗨,我无法在 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;
}
如果有人可以帮助我,我将不胜感激,因为我是初学者,编写这么多的 C++ 代码花了我大约 2 周的时间来编写代码。