Advertisement
rdsedmundo

Romanos.c

May 11th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char rom[30];
  5.  
  6. char * toRoman(int num) {
  7.     int cur, romVal[] = {
  8.         1000, 500, 100, 99, 50, 49, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
  9.     }, i, numOFF = 0;
  10.     char aux[30];
  11.     char romRep[][10] = {
  12.         "M", "D", "C", "XCIX", "L", "XLIX", "X", "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I"
  13.     };
  14.  
  15.     cur = num;
  16.     strcpy(rom, "");
  17.  
  18.     for (i = 0; i < (sizeof(romVal)/sizeof(int)); i++) {
  19.         numOFF = 0;
  20.         while (cur >= romVal[i]) {
  21.             cur -= romVal[i];
  22.             numOFF++;
  23.         }
  24.         if (numOFF > 3) {
  25.             strcpy(rom, "");
  26.             for (int j = i; j >= 0; j--) {
  27.                 int OldI=i, resp;
  28.  
  29.                 lFlager: resp = romVal[j]-romVal[i];
  30.  
  31.                 if(resp==(num-cur)){
  32.                     strcat(rom, romRep[i]);
  33.                     strcat(rom, romRep[j]);
  34.                 } else if(i>0){
  35.                     i--;
  36.                     goto lFlager;
  37.                 }
  38.  
  39.                 i=OldI;
  40.             }
  41.         } else if (numOFF > 0) {
  42.             for (int j = numOFF; j > 0; j--) strcat(rom, romRep[i]);
  43.         }
  44.     }
  45.     return rom;
  46. }
  47.  
  48. int main() {
  49.     int num;
  50.  
  51.     printf("Digite um numero para ser convertido em Romano: ");
  52.     scanf("%d", &num);
  53.     printf("\nRomano: %s\n", toRoman(num));
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement