#include <stdio.h>
#include <string.h>
void replace(int, char*);
int main(){
char input[32]; //存放輸入的羅馬數字
char output[100][16] = {""}; //存放結果
int i, j, k; //控制迴圈的變數
for(k = 0;k < 100;k++){
fgets(input, 32, stdin); //輸入 **換行符號在此也被存入input
if (input[0] == '#') //若讀取到#則輸出結果
break;
//Step1. 將輸入的羅馬數字轉換成十進位
int rome_to_decimal[2] = {0}; //存放轉換後的十進位數字
for (i = 0, j = 0; input[i] != '\n'; i++) {
//若讀取到空格,則改為存入下一列
if (input[i] == ' '){
j++;
continue;
}
//若讀取到的不是空格,則進行轉換
switch (input[i]){
case 'M':
rome_to_decimal[j] += 1000;
break;
case 'D':
rome_to_decimal[j] += 500;
break;
case 'C':
switch (input[i+1]) {
case 'M':
rome_to_decimal[j] -= 100;
break;
case 'D':
rome_to_decimal[j] -= 100;
break;
default:
rome_to_decimal[j] += 100;
}
break;
case 'L':
rome_to_decimal[j] += 50;
break;
case 'X':
switch (input[i+1]) {
case 'L':
rome_to_decimal[j] -= 10;
break;
case 'C':
rome_to_decimal[j] -= 10;
break;
default:
rome_to_decimal[j] += 10;
}
break;
case 'V':
rome_to_decimal[j] += 5;
break;
default:
switch (input[i+1]){
case 'V':
rome_to_decimal[j] -= 1;
break;
case 'X':
rome_to_decimal[j] -= 1;
break;
default:
rome_to_decimal[j] += 1;
}
}
}
//將兩個轉譯後的數字相減
int Result;
Result = rome_to_decimal[0] - rome_to_decimal[1];
//取絕對值,若為0則直接紀錄ZERO並進行下一次輸入
if (Result == 0){
strcat(output[k], "ZERO");
continue;
}
else if(Result < 0)
Result *= -1;
//將各位數分別儲存在整數陣列digit內
int digit[4];
for(i = 1000, j = 0; j < 4; i /= 10, j++) {
digit[j] = Result / i;
Result %= i;
}
// 記錄各位數的羅馬數字,並儲存到output中
for(i = 0; i < digit[0] ; i++) // 印出千位數
strcat(output[k], "M");
int m;
for (i = 1; i < 4; i++){
switch (digit[i]) { //印出百位數到個位數
case 1: case 2: case 3:
for(m = 0; m < digit[i]; m++)
strcat(output[k], "x");
break;
case 4:
strcat(output[k], "xy");
break;
case 5:
strcat(output[k], "y");
break;
case 6: case 7: case 8:
strcat(output[k], "y");
for (m = 5; m < digit[i]; m++)
strcat(output[k], "x");
break;
case 9:
strcat(output[k], "xz");
break;
}
replace(i, output[k]);
}
}
for(i = 0; i < k; i++)
printf("%s\n", output[i]);
}
void replace(int m, char* ch){
char a, b, c;
switch(m){
case 1:
a = 'C'; b = 'D'; c = 'M';
break;
case 2:
a = 'X'; b = 'L'; c = 'C';
break;
case 3:
a = 'I'; b = 'V'; c = 'X';
break;
default:
a = b = c = '\0';
}
int i;
char *ptr = ch;
for(i = 0; ch[i] != '\0'; i++){
switch(ch[i]){
case 'x':
*(ptr+i) = a;
break;
case 'y':
*(ptr+i) = b;
break;
case 'z':
*(ptr+i) = c;
break;
}
}
}