Javaの基本的な知識はありますが、C++は初めてです。ローマ数字の入力を変換し、同等のアラビア数字を見つけて出力するプログラムを作成しようとしています。ただし、たとえば、「X」を個別にチェックして削除しているため、ローマ数字で特定の接頭辞を見つける際に正しい順序を見つける方法を見つけるという問題があります。私のプログラムは、実際には 9 である入力 "IX" を 11 に変換しています。これは、プログラムが文字列内の接頭辞を見つける順序に関係していることはわかっていますが、修正方法がわかりません。それについての助けは素晴らしいでしょう。
現在のコード:
#include <cstdlib>
#include <iostream>
#include <cctype>
using namespace std;
/*
*
*/
int main() {
string roman_digits [] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
string roman_tens [] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
string roman_hundreds [] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
string roman_thousands [] = {"", "M", "MM", "MMM"};
string line, substr;
int arabic = 0;
// MCCCXXXVII
cout << "Type in a Roman numeral: ";
// Loops through inputted Roman Numerals.
while (cin >> line) {
if (!cin.eof()) {
int i = 0;
// Loops through a Roman numeral and changes it to uppercase.
while (line[i]) {
char c;
c = line[i];
c = (toupper(c));
line[i] = c;
i++;
}
// Loops through checking roman numeral with the thousands array and if there is a match prints out the equivalent arabic number.
for (int i = 0; i < 4; i++) {
if (line.find("MMM") != string::npos) {
unsigned pos = line.find("MMM");
substr = line.substr(pos, 3);
line.erase(pos, 3);
} else if (line.find("MM") != string::npos) {
unsigned pos = line.find("MM");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("M") != string::npos) {
unsigned pos = line.find("M");
substr = line.substr(pos, 1);
line.erase(pos, 1);
}
if (roman_thousands[i] == substr){
arabic = arabic + (i * 1000);
}
}
// Loops through checking roman numeral with the hundreds array and if there is a match prints out the equivalent arabic number.
for (int i = 0; i < 10; i++) {
if (line.find("CM") != string::npos){
unsigned pos = line.find("CM");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("DCCC") != string::npos){
unsigned pos = line.find("DCCC");
substr = line.substr(pos, 4);
line.erase(pos, 4);
} else if (line.find("DCC") != string::npos){
unsigned pos = line.find("DCC");
substr = line.substr(pos, 3);
line.erase(pos, 3);
} else if (line.find("DC") != string::npos){
unsigned pos = line.find("DC");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("D") != string::npos){
unsigned pos = line.find("D");
substr = line.substr(pos, 1);
line.erase(pos, 1);
} else if (line.find("CD") != string::npos){
unsigned pos = line.find("CD");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("CCC") != string::npos){
unsigned pos = line.find("CCC");
substr = line.substr(pos, 3);
line.erase(pos, 3);
}else if (line.find("CC") != string::npos){
unsigned pos = line.find("CC");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("C") != string::npos){
unsigned pos = line.find("C");
substr = line.substr(pos, 1);
line.erase(pos, 1);
}
if (roman_hundreds[i] == substr) {
arabic = arabic + (i * 100);
}
}
// Loops through checking roman numeral with the tens array and if there is a match prints out the equivalent arabic number.
for (int i = 0; i < 10; i++) {
if (line.find("XC") != string::npos){
unsigned pos = line.find("XC");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("LXXX") != string::npos){
unsigned pos = line.find("LXXX");
substr = line.substr(pos, 4);
line.erase(pos, 4);
}else if (line.find("LXX") != string::npos){
unsigned pos = line.find("LXX");
substr = line.substr(pos, 3);
line.erase(pos, 3);
} else if (line.find("LX") != string::npos){
unsigned pos = line.find("LX");
substr = line.substr(pos, 2);
line.erase(pos, 2);
}else if (line.find("L") != string::npos){
unsigned pos = line.find("L");
substr = line.substr(pos, 1);
line.erase(pos, 1);
}else if (line.find("XL") != string::npos){
unsigned pos = line.find("XL");
substr = line.substr(pos, 2);
line.erase(pos, 2);
}else if (line.find("XXX") != string::npos){
unsigned pos = line.find("XXX");
substr = line.substr(pos, 3);
line.erase(pos, 3);
}else if (line.find("XX") != string::npos){
unsigned pos = line.find("XX");
substr = line.substr(pos, 2);
line.erase(pos, 2);
}else if (line.find("X") != string::npos){
unsigned pos = line.find("X");
substr = line.substr(pos, 1);
line.erase(pos, 1);
}
if (roman_tens[i] == substr) {
arabic = arabic + (i * 10);
}
}
// Loops through checking roman numeral with the digits array and if there is a match prints out the equivalent arabic number.
for (int i = 0; i < 10; i++) {
if (line.find("IX") != string::npos){
unsigned pos = line.find("IX");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("VIII") != string::npos){
unsigned pos = line.find("VIII");
substr = line.substr(pos, 4);
line.erase(pos, 4);
} else if (line.find("VII") != string::npos){
unsigned pos = line.find("VII");
substr = line.substr(pos, 3);
line.erase(pos, 3);
} else if (line.find("VI") != string::npos){
unsigned pos = line.find("VI");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("V") != string::npos){
unsigned pos = line.find("V");
substr = line.substr(pos, 1);
line.erase(pos, 1);
} else if (line.find("IV") != string::npos){
unsigned pos = line.find("IV");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("III") != string::npos){
unsigned pos = line.find("III");
substr = line.substr(pos, 3);
line.erase(pos, 3);
} else if (line.find("II") != string::npos){
unsigned pos = line.find("II");
substr = line.substr(pos, 2);
line.erase(pos, 2);
}else if (line.find("I") != string::npos){
unsigned pos = line.find("I");
substr = line.substr(pos, 1);
}
if (roman_digits[i] == substr) {
arabic = arabic + i;
}
}
cout << "The Arabic equivalent of " << line << " is: " << arabic << endl;
arabic = 0;
} else {
cout << "Invalid Roman numeral." << endl;
}
}
return 0;
}
どんな助けでも大歓迎ですありがとう。