#include #include #include using namespace std; int length; int memory[10]; char in[10000]; bool mem[10]; void deleteMem() { for( int i = 0; i < 10; i++ ) memory[i] = 0; for( int i = 0; i < 10; i++ ) mem[i] = false; } int prevod( char letter ) { switch( letter ) { case 'O': return 0; case 'I': return 1; case 'V': return 5; case 'X': return 10; case 'L': return 50; case 'C': return 100; case 'D': return 500; case 'M': return 1000; } } int charToInt( char a ) { switch(a) { case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; case '0': return 0; default:return -1; } } int getNumFromChar( char *maro ) { int lll = strlen(maro)-1; int *pole = new int[lll+1]; for (int i=0; i<= lll; i++) pole[i] = prevod(maro[i]); int temp=0; for (int i=0; i<=lll; i++) temp = pole[i]+temp; for (int i=0; i<=lll-1; i++) if (pole[i] length ) break; str[l-i] = in[l]; l++; if( in[l] == '+' || in[l] == '-' ) break; } str[l] = '\0'; i = l-1; temp += getNumFromChar(str); } else { if( mem[charToInt( in[i] )] ) temp += memory[charToInt( in[i] )]; else { failed = true; break; } } state = 1; } else if( state == 1 ) { if( in[i] == '-' ) state = 2; else state = 0; } else if( state == 2 ) // nic { if( charToInt( in[i] ) == -1 ) { int l = i; while( charToInt( in[l] ) == -1 ) { if( l > length ) break; str[l-i] = in[l]; l++; if( in[l] == '+' || in[l] == '-' ) break; } i = l-1; str[l] = '\0'; temp -= getNumFromChar(str); } else { if( mem[charToInt( in[i] )] ) temp -= memory[charToInt( in[i] )]; else { failed = true; break; } } state = 1; } } if( temp > 10000 ) failed = true; else if( temp < 0 ) failed = true; if( failed ) { printf("Error\n"); } else { memory[index] = temp; mem[index] = true; printf("%i=", index ); int aaa = memory[index]; if( aaa == 0 ) printf("0"); else while( aaa > 0 ) { if( aaa >= 1000 ) { printf("M"); aaa -= 1000; } else if( aaa >= 900 ) { printf("CM"); aaa -= 900; } else if( aaa >= 500 ) { printf("D"); aaa -= 500; } else if( aaa >= 400 ) { printf("CD"); aaa -= 400; } else if( aaa >= 100 ) { printf("C"); aaa -= 100; } else if( aaa >= 90 ) { printf("XC"); aaa -= 90; } else if( aaa >= 50 ) { printf("L"); aaa -= 50; } else if( aaa >= 40 ) { printf("XL"); aaa -= 40; } else if( aaa >= 10 ) { printf("X"); aaa -= 10; } else if( aaa >= 9 ) { printf("IX"); aaa -= 9; } else if( aaa >= 5 ) { printf("V"); aaa -= 5; } else if( aaa >= 4 ) { printf("IV"); aaa -= 4; } else if( aaa >= 1 ) { printf("I"); aaa -= 1; } } printf("\n"); } } int main() { deleteMem(); while( true ) { scanf( "%s", in); length = strlen(in); in[length] = '\0'; if(in[0] == 'Q') { printf("Bye\n"); break; } prekladac(); } return 0; }