Question

.data # This data will serve as our map between the roman # and the decimal numbers. Here, each list will # serve as our...

.data # This data will serve as our map between the roman # and the decimal numbers. Here, each list will # serve as our makeshift "array", where the conversion # from decimal to roman will be according to each indice, # i.e. decimalNumeral[i] will dictate romanNumeral[i]. # This also will be useful when converting roman to # decimal, as romanNumeral[i] will dictate decimalNumeral[i] decimalNumeral: .word 1000 100 50 10 5 1 # the decimal map romanNumeral: .asciiz "MDCLXVI" # the roman map # Request the user for a roman numeral. req: .asciiz "Roman Numeral: " # The error message that will be displayed if the # format is invalid or contains a token that is not # a valid roman/decimal numeral. err: .asciiz "Input contains invalid tokens or format" .text .globl main main: jal conversion conversion: this mips program doesn't ask for input when i run it

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<string.h>

//returns the decimal number according to the roman character
int digit(char c){

int value=0;

switch(c){
case 'I': value = 1; break;
case 'V': value = 5; break;
case 'X': value = 10; break;
case 'L': value = 50; break;
case 'C': value = 100; break;
case 'D': value = 500; break;
case 'M': value = 1000; break;
default: value = -1;
}

return value;
}

int main(){

char roman_Number[1000];
int i=0;
long int number =0;

printf("Enter any roman number (Valid digits are I, V, X, L, C, D, M): \n");
scanf("%s",roman_Number);

while(roman_Number[i]){

if(digit(roman_Number[i]) < 0){ //for checking whether the given number is a roman or not
printf("Invalid roman digit : %c",roman_Number[i]);
return 0;
}

if((strlen(roman_Number) -i) > 2){ //for checking the order of roman numbers
if(digit(roman_Number[i]) < digit(roman_Number[i+2])){
printf("Invalid roman number");
return 0;
}
}

if(digit(roman_Number[i]) >= digit(roman_Number[i+1]))
number = number + digit(roman_Number[i]);
else{
number = number + (digit(roman_Number[i+1]) - digit(roman_Number[i]));
i++;
}
i++;
}

printf("decimal number is : %ld",number);

return 0;

}

//In your problem roman value for 500 is not mentioned as D->500

Add a comment
Know the answer?
Add Answer to:
.data # This data will serve as our map between the roman # and the decimal numbers. Here, each list will # serve as our...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT