0
Well, I created this code as a draft form for the project I have in which it turns certain words into decimal values, but I did it with Roman numerals pq is easier that way for now, but there’s a problem, every time I do XL
that should give the value of 40
, is calculated as 60
, and when I do LX
that should give 60
gives -40
, likely to be a logic problem, but I’m not able to find where I’m missing in this code, I thank you already.
PS: The rest works good, IV
gives 4
right and IX
gives 9
also normal, for example.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cot2dec(char s[100]){
int i, c = 0, valor;
char s2[100];
for(i=0; i<strlen(s); i++){
if(s[i] > s[i+1]){
switch(s[i]){
case 'I':
valor=1;
break;
case 'V':
valor=5;
break;
case 'X':
valor=10;
break;
case 'L':
valor=50;
break;
default:
printf("insira um valor valido.");
break;
}
c+=valor;
} else if(s[i] < s[i+1]){
switch(s[i]){
case 'I':
valor=1;
break;
case 'V':
valor=5;
break;
case 'X':
valor=10;
break;
case 'L':
valor=50;
break;
default:
printf("insira um valor valido.");
break;
}
c-=valor;
} else if(s[i] == s[i+1]){
switch(s[i]){
case 'I':
valor=1;
break;
case 'V':
valor=5;
break;
case 'X':
valor=10;
break;
case 'L':
valor=50;
break;
default:
printf("insira um valor valido.");
break;
}
c+=valor;
}
}
return c;
}
int main() {
char cad[100];
int cotinter;
printf("Insira um algarismo romano: ");
scanf("%s", &cad);
cotinter = cot2dec(cad);
printf("O valor decimal de %s e %d", cad, cotinter);
return 0;
}
I think it’s complex, I answered some questions about: https://answall.com/search?q=user%3A101+romano But I don’t know if any will do. It seems to me that logic is disregarding the correct positions that I Roman changes, but I have not analyzed profitably.
– Maniero
yes, but the problem seems to me to be in the combination of X and L, since the other combinations work normally.
– Jeremy Luckas