C++ 将罗马数字转换成阿拉伯数字
/* A Roman to Decimal converter */ #include <iostream> int romToDec(char *p); int valueOf(char c); int main() { char rom[50]; std::cout << "Roman: "; std::cin.getline(rom, 50); std::cout << "Decimal: " << romToDec(rom) << std::endl; std::cin.ignore(1000, '\n'); return 0; } int romToDec(char *p) { int result = 0; for(; *p; p++) { int current = valueOf(*p); int next = valueOf(*(p+1)); if(current >= next) result += current; else result -= current; } return result; } int valueOf(char c) { switch(c) { case 'M': return 1000; case 'D': return 500; case 'C': return 100; case 'L': return 50; case 'X': return 10; case 'V': return 5; case 'I': return 1; default: return 0; } }