General Description: You will write a program that converts Roman Numerals to a
ID: 3556684 • Letter: G
Question
General Description: You will write a program that converts Roman Numerals to a decimal
Details:
Your must define a class, called romanType, which has the following elements:
a function to store the Roman numeral, called storeRoman
a function to convert the Roman numeral to a decimal, and store the decimal, called convertRoman
a function to print the number, as a decimal and a Roman numeral, called printRoman
a variable to store the Roman numeral
a variable to store the decimal number
a default constructor, which sets the Roman numeral to I,
and the decimal value to 1
The class definition MUST be in a file called roman.h, and the class function definitions must be in a file called romanimp.cpp, both of which will be included in your project.
Your main program will define an object of type romanType, and then enter a loop asking you to input a Roman numeral. If you input a valid Roman numeral, it will store, convert, and print both forms. The loop will continue until the user indicates they no longer want to continue (however you want to implement this)
Input Validation
Explanation / Answer
C code for roman numbers to decimal
#include<stdio.h>
#include<string.h>
int digitValue(char);
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): ");
scanf("%s",roman_Number);
while(roman_Number[i]){
if(digitValue(roman_Number[i]) < 0){
printf("Invalid roman digit : %c",roman_Number[i]);
return 0;
}
if((strlen(roman_Number) -i) > 2){
if(digitValue(roman_Number[i]) < digitValue(roman_Number[i+2])){
printf("Invalid roman number");
return 0;
}
}
if(digitValue(roman_Number[i]) >= digitValue(roman_Number[i+1]))
number = number + digitValue(roman_Number[i]);
else{
number = number + (digitValue(roman_Number[i+1]) - digitValue(roman_Number[i]));
i++;
}
i++;
}
printf("Its decimal value is : %ld",number);
return 0;
}
int digitValue(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;
case '': value = 0; break;
default: value = -1;
}
return value;
}
Sample output:
Enter any roman number (Valid digits are I, V, X, L, C, D, M):
XVII
Its decimal value is: 17
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.