Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- char rom[30];
- char * toRoman(int num) {
- int cur, romVal[] = {
- 1000, 500, 100, 99, 50, 49, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
- }, i, numOFF = 0;
- char aux[30];
- char romRep[][10] = {
- "M", "D", "C", "XCIX", "L", "XLIX", "X", "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I"
- };
- cur = num;
- strcpy(rom, "");
- for (i = 0; i < (sizeof(romVal)/sizeof(int)); i++) {
- numOFF = 0;
- while (cur >= romVal[i]) {
- cur -= romVal[i];
- numOFF++;
- }
- if (numOFF > 3) {
- strcpy(rom, "");
- for (int j = i; j >= 0; j--) {
- int OldI=i, resp;
- lFlager: resp = romVal[j]-romVal[i];
- if(resp==(num-cur)){
- strcat(rom, romRep[i]);
- strcat(rom, romRep[j]);
- } else if(i>0){
- i--;
- goto lFlager;
- }
- i=OldI;
- }
- } else if (numOFF > 0) {
- for (int j = numOFF; j > 0; j--) strcat(rom, romRep[i]);
- }
- }
- return rom;
- }
- int main() {
- int num;
- printf("Digite um numero para ser convertido em Romano: ");
- scanf("%d", &num);
- printf("\nRomano: %s\n", toRoman(num));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement