Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char romanian_numbers[7] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
  5. int arabic_numbers[7] = {1, 5, 10, 50, 100, 500, 1000};
  6. char romanian_groups[14] = {'M', 'C', 'D', 'C', 'C', 'X', 'L', 'X', 'X', 'I', 'V', 'I', 'I'};
  7. int arabic_groups[14] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  8.  
  9. int romanian_number_validator(char number[]){
  10.     int i, j, is_valid, k, l;
  11.     i = 0;
  12.     is_valid = 0;                              // counter of symbols being romanian symbols
  13.     int counters[7] = {0,0,0,0,0,0,0};         // counter of the occurences of romanian symbols
  14.  
  15.     while (number[i] != (char)0 ){
  16.         for (j=0; j < 7; j++){
  17.             if(number[i] == romanian_numbers[j]){
  18.                 counters[j] += 1;
  19.                 is_valid += 1;
  20.                 if (j % 2 == 0 && counters[j] > 4){
  21.                     return 2;                 // it means that there is more than 4 of I, X, C or M
  22.                 }
  23.                 if (j % 2 == 1 && counters[j] > 2){
  24.                     return 3;                 // it means that there is more than 2 of V, L  or D
  25.                 }
  26.                 for (k=j-1; k > 0; k--){
  27.                     if (counters[k] > 1){
  28.                         return 4;             // it means that there is more than one number smaller than
  29.                                               // number[i] before this number (e.g. CCD)
  30.                     }
  31.                 }
  32.             }
  33.         }
  34.         i++;
  35.     }
  36.  
  37.     if (is_valid < i){                        // checks if romanian number contains only M,D,C,L,X,V,I
  38.         return 1;
  39.     }
  40.  
  41.     for (l=0; l < 15; l++){
  42.         if(number[l-3] != (char)0 && number[l-2] != (char)0 && number[l-1] != (char)0){
  43.             if (number[l-3] == number[l] && number[l-2] ==number[l] && number[l-1] == number[l]){
  44.                 return 5;
  45.             }
  46.         }
  47.     }
  48.  
  49.     return 0;
  50.  
  51. }
  52.  
  53. void validation_messenger(char number[]){
  54.     if (romanian_number_validator(number) == 1){
  55.         printf("Your number contains some not romanian symbols.");
  56.     }
  57.     if (romanian_number_validator(number) == 2){
  58.         printf("Your number contains more than 4 symbols from {I, X, C, M}.");
  59.     }
  60.     if (romanian_number_validator(number) == 3){
  61.         printf("Your number contains more than 2 symbols from {V, L, D}.");
  62.     }
  63.     if (romanian_number_validator(number) == 4){
  64.         printf("Before some number your input contains more than 1 smaller number. e.g. XXC");
  65.     }
  66.     if (romanian_number_validator(number) == 5){
  67.         printf("Your number has 4 the same numbers in a row.");
  68.     }
  69.     else{
  70.         if (romanian_to_arabic(number) == -1){
  71.             printf("You have wrong order of the symbols. e.g. CMD or XCL");
  72.         }
  73.         if (romanian_to_arabic(number) == -2){
  74.             printf("Before and after some number you have smaller one. e.g. XLX");
  75.         }
  76.         if (romanian_to_arabic(number) == -3){
  77.             printf("You have wrong smaller number before greater one.");
  78.         }
  79.         if (romanian_to_arabic(number) == -4){
  80.             printf("You have L, D or V before greater number.");
  81.         }
  82.     }
  83. }
  84.  
  85. int romanian_to_arabic(char number[]){
  86.     int i, j, k;
  87.     int arabic;
  88.     arabic = 0;
  89.     k=0;
  90.     int result[16];
  91.     for (i=0; i<16; i++){          // returns table of int, each int is the value of proper romanian symbol
  92.         for (j=0; j<7; j++){
  93.             if (romanian_numbers[j] == number[i]){
  94.                 result[i] = arabic_numbers[j];
  95.             }
  96.         }
  97.     result[i+1] = 0;
  98.     }
  99.  
  100.     while(result[k] > 0){
  101.         if (result[k] < result[k+1] && result[k+2] != 0){
  102.             if (0.5*(-result[k] + result[k+1]) <= result[k+2]){
  103.                 return -1;
  104.             }
  105.             if (result[k] == result[k+2]){
  106.                 return -2;
  107.             }
  108.         }
  109.  
  110.         if (result[k] >= result[k+1]){
  111.             arabic += result[k];
  112.         }
  113.  
  114.         if (result[k] < result[k+1] && result[k+1] != 0 && result[k] != 0){
  115.             if (result[k] != 5 && result[k] != 50 && result[k] != 500){
  116.                 if (result[k] == result[k+1] / 5 || result[k] == result[k+1]/10 ){
  117.                     arabic -= result[k];
  118.                 }
  119.                 else {
  120.                     return -3;
  121.                 }
  122.             }
  123.             else {
  124.                 return -4;
  125.             }
  126.         }
  127.         k++;
  128.     }
  129.     return arabic;
  130. }
  131.  
  132. void arabic_to_romanian(int arabic){
  133.     int i, j;
  134.     char result[20];
  135.     j = 0;
  136.     i = 0;
  137.  
  138.     for (i=0; i<13; i++){
  139.         while(arabic >= arabic_groups[i]){
  140.             if( i % 2 != 0 ){
  141.                 result[j] = romanian_groups[i];
  142.                 result[j+1] = romanian_groups[i-1];
  143.                 j += 1;
  144.             }
  145.             else{
  146.                 result[j] = romanian_groups[i];
  147.             }
  148.             arabic -= arabic_groups[i];
  149.             j++;
  150.         }
  151.     }
  152.     result[j] = (char)0;
  153.  
  154.     int k = 0;
  155.     while (result[k]!= (char)0){
  156.         printf("%c ", result[k]);
  157.         k++;
  158.     }
  159.     return;
  160. }
  161.  
  162. int main(int argc, char *argv[]){
  163.     const int length = 15; // length of max romanian number (MMMDCCCLXXXVIII) + 1
  164.     char number[length];
  165.     char letter;
  166.     int arabic;
  167.  
  168.     letter = 'X';
  169.  
  170.     printf ("This is converter from romanian numbers to arabic or otherwise.\n");
  171.  
  172.     if (argc == 1){
  173.         printf("Please insert A if you want to convert from arabic to romanian or R otherwise\n");
  174.         scanf("%c", &letter);
  175.     }
  176.  
  177.     if((argc > 1 && strcmp(argv[1], "R")==0) || letter == 'R'){
  178.         printf ("Please insert romanian number.");
  179.         scanf("%s", number);
  180.  
  181.         if (romanian_number_validator(number) == 0 && romanian_to_arabic(number) > 0){
  182.                 printf("%d", romanian_to_arabic(number));
  183.         }
  184.         else{
  185.             validation_messenger(number);
  186.             printf ("\nTry again.");
  187.         }
  188.     }
  189.  
  190.     if((argc > 1 && strcmp(argv[1], "A")==0) || letter== 'A'){
  191.         printf ("Please insert arabic number");
  192.         scanf("%d", &arabic);
  193.         if (arabic>3999){
  194.             printf("Please insert number lower than 4000");
  195.         }
  196.         else{
  197.             arabic_to_romanian(arabic);
  198.         }
  199.     }
  200.  
  201.  
  202.     if ((argc > 1 && (strcmp(argv[1], "R")!=0) && (strcmp(argv[1], "A")!=0)) && letter == 'X') {
  203.         printf("Please use command line to run program with correct letter: R or A");
  204.     }
  205.  
  206.     return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement