#include #include #include using namespace std; int romToInt(string &rom) { int result = 0; while (rom != "") { switch (rom[0]) { case 'M': result += 1000; break; case 'D': result += 500; break; case 'L': result += 50; break; case 'V': result += 5; break; case 'C': if (rom.length() >= 2) { if (rom[1] == 'M') { result += 900; rom = rom.substr(1, rom.length() - 1); } else if (rom[1] == 'D') { result += 400; rom = rom.substr(1, rom.length() - 1); } else result += 100; //odstran 1. znak } else result += 100; break; case 'X': if (rom.length() >= 2) { if (rom[1] == 'C') { result += 90; rom = rom.substr(1, rom.length() - 1); } else if (rom[1] == 'L') { result += 40; rom = rom.substr(1, rom.length() - 1); } else result += 10; //odstran 1. znak } else result += 10; break; case 'I': if (rom.length() >= 2) { if (rom[1] == 'X') { result += 9; rom = rom.substr(1, rom.length() - 1); } else if (rom[1] == 'V') { result += 4; rom = rom.substr(1, rom.length() - 1); } else result += 1; //odstran 1. znak } else result += 1; break; } //odstran 1. znak rom = rom.substr(1, rom.length() - 1); } rom = ""; return result; } string intToRom(int n) { if (n == 0) return "O"; string result = ""; while (n >= 1000) { result += 'M'; n -= 1000; } while (n >= 900) { result += "CM"; n -= 900; } while (n >= 500) { result += 'D'; n -= 500; } while (n >= 400) { result += "CD"; n -= 400; } while (n >= 100) { result += "C"; n -= 100; } while (n >= 90) { result += "XC"; n -= 90; } while (n >= 50) { result += "L"; n -= 50; } while (n >= 40) { result += "XL"; n -= 40; } while (n >= 10) { result += "X"; n -= 10; } while (n >= 9) { result += "IX"; n -= 9; } while (n >= 5) { result += "V"; n -= 5; } while (n >= 4) { result += "IV"; n -= 4; } while (n >= 1) { result += "I"; n -= 1; } return result; } int main(void) { string command; map memory; do { getline(cin, command); //cout << "answer to command "<> regNum; // = char c; cmdStr >> c; bool wasError = false; bool nextOpIsPlus = true; int result = 0; string currentRoman = ""; //zjistit, jestli cislo/registr while (cmdStr >> c) { if ((c >= '0') && (c <= '9')) { if (memory.count(c-48) == 0) { wasError = true; break; } result += memory[c-48] * (nextOpIsPlus ? 1 : -1); } else if (c == '-') { result += romToInt(currentRoman) * (nextOpIsPlus ? 1 : -1); nextOpIsPlus = false; } else if (c == '+') { result += romToInt(currentRoman) * (nextOpIsPlus ? 1 : -1); nextOpIsPlus = true; } else { currentRoman = currentRoman + c; } } result += romToInt(currentRoman) * (nextOpIsPlus ? 1 : -1); if ((result > 10000) || (result < 0)) { wasError = true; } if (wasError) { cout << "Error" << endl; } else { //cout << "result = " << result << endl; cout << regNum << "=" << intToRom(result) << endl; memory[regNum] = result; } } } while (command != "QUIT"); cout << "Bye" << endl; return 0; }