Advertisement
dmilicev

converting_roman_numerals_to_arabic_numerals.c

Oct 29th, 2019
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.23 KB | None | 0 0
  1. /*
  2.  
  3.     converting_roman_numerals_to_arabic_numerals.c
  4.  
  5.  
  6.  
  7.     As a reminder, the following tables contain Roman numerals:
  8.  
  9.     I   V   X   L   C   D   M
  10.     1   5   10  50  100 500 1000
  11.  
  12.     IV  IX  XL  XC  CD  CM
  13.     4   9   40  90  400 900
  14.  
  15.     Test case:
  16.  
  17.     2736 = MMDCCXXXVI
  18.  
  19.     or separated:
  20.  
  21.            2  7   3   6
  22.     2736 = MM DCC XXX VI
  23.  
  24.            3   4  4   4
  25.     3444 = MMM CD XL  IV
  26.  
  27.            3   9  9   9
  28.     3999 = MMM CM XC  IX
  29.  
  30. MDCCCLXXXVIII = 1000 + 500 + 100 + 100 + 100 + 50 + 10 + 10 + 10 + 5 + 1 + 1 + 1 = 1888
  31.  
  32. If the value of the left digit is less than the right,
  33. then the value of the left digit is subtracted from the value of the right.
  34.  
  35. MCMXCIX = M CM XC IX or 1000 + (1000 - 100) + (100 - 10) + (10 - 1) = 1999
  36.  
  37. MCMXCIX = M CM XC IX or 1000 - 100 + 1000 - 10 + 100 - 1 + 10 = 1999
  38.  
  39.  
  40.     I   V   X   L   C   D   M
  41.     1   5   10  50  100 500 1000
  42.  
  43.  
  44.     The number of thousands: the number of Ms at the beginning * 1000 is the number of thousands.
  45.  
  46.     0       1000        2000        3000    4000    5000    ...     11000
  47.     0       1           2           3       4       5       ...     11
  48.             M           MM          MMM     MMMM    MMMMM           MMMMMMMMMMM
  49.  
  50.     Number of hundreds:       <4 , =4 , >4
  51.  
  52.     0       100     200     300     400     500     600     700     800     900
  53.     0       1       2       3       4       5       6       7       8       9
  54.             C       CC      CCC     CD      D       DC      DCC     DCCC    CM
  55.  
  56.  
  57.     Number of tens:           <4 , =4 , >4
  58.  
  59.     0       10      20      30      40      50      60      70      80      90
  60.     0       1       2       3       4       5       6       7       8       9
  61.             X       XX      XXX     XL      L       LX      LXX     LXXX    XC
  62.  
  63.  
  64.     Number of ones            <4 , =4 , >4
  65.  
  66.     0       1       2       3       4       5       6       7       8       9
  67.             I       II      III     IV      V       VI      VII     VIII    IX
  68.  
  69.  
  70. */
  71.  
  72. #include <stdio.h>
  73. #include <string.h>
  74.  
  75. int get_roman_letter(char string[], int i) {
  76.  
  77.     int sign_plus_minus = 1;
  78.  
  79.     if (string[i] == 'I' && (string[i + 1] == 'V' || string[i + 1] == 'X'))
  80.         sign_plus_minus = -1;
  81.  
  82.     if (string[i] == 'X' && (string[i + 1] == 'L' || string[i + 1] == 'C'))
  83.         sign_plus_minus = -1;
  84.  
  85.     if (string[i] == 'C' && (string[i + 1] == 'D' || string[i + 1] == 'M'))
  86.         sign_plus_minus = -1;
  87.  
  88.     return sign_plus_minus;
  89. }
  90.  
  91. int get_digit(char c) {
  92.  
  93.     int digit;
  94.  
  95.     switch (c) {
  96.         case 'I': digit = 1;
  97.             break;
  98.         case 'V': digit = 5;
  99.             break;
  100.         case 'X': digit = 10;
  101.             break;
  102.         case 'L': digit = 50;
  103.             break;
  104.         case 'C': digit = 100;
  105.             break;
  106.         case 'D': digit = 500;
  107.             break;
  108.         case 'M': digit = 1000;
  109.             break;
  110.         default: digit = 0;
  111.             break;
  112.     }
  113.     return digit;
  114. }
  115.  
  116. int get_arabic_number(char roman[]) {
  117.  
  118.     int i, number = 0;
  119.  
  120.     for (i = 0; i < strlen(roman); i++) {
  121.         number += get_roman_letter(roman, i) * get_digit(roman[i]);
  122.     }
  123.     return number;
  124. }
  125.  
  126.  
  127. int main(void) {
  128.  
  129.     int arabic;
  130.     char roman[100];
  131.  
  132.     strcpy(roman,"MMDCCXXXVI");
  133.  
  134.     printf("\n roman to arabic \n");
  135.  
  136.     arabic = get_arabic_number(roman);
  137.  
  138.     printf("\n %s = %d \n", roman, arabic );
  139.  
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement