In: Computer Science
Problem Statement Create a program that will convert a decimal value into roman numeral, and output the result. The program will ask for an integer input, assume all inputs are valid, and the program should not end unless the user tells you to.
Refactoring Once your program is running correctly, put your conversion algorithm into a function named d_to_r(), which takes a string as parameter and return an int. You are allowed to create more helper functions if needed.
Link for Roman numerals: http://sierra.nmsu.edu/morandi/coursematerials/RomanNumerals.html
//I am little bit confused for your question as Refactoring Once your program is running correctly, put your conversion algorithm into a function named d_to_r(), which takes a string as parameter and return an int. SO i have provide roman to decimal also.
If any more reQuirement kindly ask.
PROGRAM
#include<stdio.h>
#include<stdlib.h>
int digit(char c);
void dtor(int num){
int decimal[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; //base
values
char *symbol[] =
{"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
//roman symbols
int i = 0;
while(num){ //repeat process until num is not 0
while(num/decimal[i]){ //first base value that divides num is
largest base value
printf("%s",symbol[i]); //print roman symbol equivalent to largest
base value
num -= decimal[i]; //subtract largest base value from num
}
i++; //move to next base value to divide num
}
}
//METHOD to convert the roman numbers to decimal
//take argument as string and returns integer value
long int d_to_r(char roman_Number[1000])
{
int i=0;
long int number =0;
//loop to extract one by one characters from the string
while(roman_Number[i]){
//condition for invalid digit
if(digit(roman_Number[i]) < 0){
printf("Invalid roman digit : %c",roman_Number[i]);
return 0;
}
//condition for invalid roman number
if((strlen(roman_Number) -i) > 2){
if(digit(roman_Number[i]) < digit(roman_Number[i+2])){
printf("Invalid roman number");
return 0;
}
}
//condition to form the decimal numbers by using digit()
method
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++;
}
//return the number
return number;
}
//method to set the decimal value for corresponding 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;
case '': value = 0; break;
default: value = -1;
}
//return the numeric value
return value;
}
int main()
{
int n;
char ch;
char roman_Number[1000];
int i=0;
long int number =0;
//infinite loop for decimal to roman
while(1)
{
printf(" Enter a decimal value");
fflush(stdin);
scanf("%d",&n);//ask for decimal
value
printf("%d = ",n);
dtor(n);//call the method to display the
roman equivalent
//ask for further convertion to
user
printf(" Do you want to
continue[Y/N]");
fflush(stdin);
scanf("%c",&ch);
if(ch=='n' || ch=='N')
break;
}
//infinite loop for roman to decimal
while(1)
{
//ask for roman value
printf(" Enter any roman number (Valid digits are I,
V, X, L, C, D, M): ");
scanf("%s",roman_Number);
//call the method to convert roman to decimal
number=d_to_r(roman_Number);
printf("Its decimal value is :
%ld",number);
//ask for further convertion to
user
printf(" Do you want to
continue[Y/N]");
fflush(stdin);
scanf("%c",&ch);
if(ch=='n' || ch=='N')
break;
}
}
OUTPUT