您好我無法在C++中將羅馬數字轉換爲正常數字,但代碼在一定程度上工作,但是如果輸入數字(XIV 14或LIV等)把15或55. 我試圖實現查找聲明,但我沒有真正的想法如何使用它來解決我的問題,這裏是我的代碼的副本到目前爲止;使用C++中的查找語句將羅馬數字轉換爲數字
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個星期的代碼。
哇,2周?! :O – Doorknob
您是否允許使用不同的技術來執行此操作?對我來說,想到的第一個是狀態機。 –
不,我不這麼認爲,(對不起,我不知道什麼是狀態機) – Brenton