/*********************************************** * Section : 15 * Machine No. : N * Roll No. : 19CS100XY * Name : Aritra Hazra * Assignment No : 6B * Description : Roman String to Decimal Numbers ***********************************************/ #include #include #include #define MAX_SIZE 1000 /* function to return the decimal equivalent value of a roman number character */ int getRomanValue(char r) { int val; switch(r) { case 'I': val = 1; break; case 'V': val = 5; break; case 'X': val = 10; break; case 'L': val = 50; break; case 'C': val = 100; break; case 'D': val = 500; break; case 'M': val = 1000; break; default : val = -1; break; // illegal case } return val; } /* function to convert the roman number into decimal equivalent, and also print the conversion breakup values */ unsigned long int romanToDecimal(char *romanNum) { unsigned long int decimalNum = 0; unsigned int i = 0, romanLen = strlen(romanNum), romanVal1, romanVal2; for(i=0; i= romanVal2) { /* incremental decimal values obtained from consecutive roman characters addition (e.g. XI) */ decimalNum += romanVal1; if(i>0) printf(" +"); printf(" %c [%u]", romanNum[i], romanVal1); } else { /* decremental decimal values obtained from consecutive roman characters subtraction (e.g. IX) */ decimalNum += romanVal2 - romanVal1; if(i>0) printf(" +"); printf(" %c%c [%u]", romanNum[i], romanNum[i+1], romanVal2-romanVal1); i++; } } else { /* end of roman string with one character to convert */ decimalNum += romanVal1; if(i>0) printf(" +"); printf(" %c [%u]", romanNum[i], romanVal1); } } return decimalNum; } int main () { char roman[MAX_SIZE]; unsigned long int decimal; printf("Enter Roman String: "); scanf("%s", roman); printf("Decimal Conversion Breakup:"); decimal = romanToDecimal(roman); printf("\nEquivalent Decimal Number : %lu\n", decimal); return 0; }