Conversion of roman letter to integer

Question: Conversion of roman letter to an integer.
Output:
Conversion of roman letter
Roman numerals are the numbers that were used in ancient Rome, which employed combinations of letters from the Latin alphabet (I, V, X, L, C, D, and M).
Numbers are represented by combinations of the following symbols:
Numbers are represented by putting the symbols into various combinations in different orders. The symbols are then added together, for example, I + I + I, written as III, is 3. To write 11 we add X (10) and I (1) and write it as XI. For 22 we add X and X and I and I, so XXII.
Roman numerals are usually written in order, from largest to smallest and from left to right, but more than three identical symbols never appear in a row. Instead, a system of subtraction is used: when a smaller number appears in front of a larger one, that needs to be subtracted, so IV is 4 (5 – 1) and IX is 9 (10 – 1).
The subtraction system is used in six cases:
⋅ I is placed before V and X: IV (4) and IX (9).
⋅ X is placed before L (50) and C (100): XL (40) and XC (90).
⋅ C is placed before D (500) and M (1000): CD (400) and CM (900)
For further reference about Roman Letter Conversion
Logic:
- Get the input from the user
- Check the element is number or not
- If not means go for the conversion in roman()
- Get the corresponding integer for each element using number()
- Compare the current and the next element
- If cur>=nxt means add into value otherwise subtract from the value
- Then print the integer for the corresponding roman
Program:
[code lang=”c”]
#include<stdio.h>
#include<string.h>
int number(char roman)
{
switch(roman) //return roman equivalent
{
case ‘I’:
return 1;
case ‘V’:
return 5;
case ‘X’:
return 10;
case ‘L’:
return 50;
case ‘C’:
return 100;
case ‘D’:
return 500;
case ‘M’:
return 1000;
default:
return 0;
}
}
int roman(char input[]) //calculates the numeral value
{
int j=0,cur,nxt,value=0;
cur=number(input[j]);
for(j=1;j<strlen(input)+1;j++)
{
nxt=number(input[j]);
if(cur>=nxt)
{
value+=cur;
}
else
{
value-=cur;
}
cur=nxt;
}
return value;
}
int main()
{
char input[][10]={"2","III","4","IV","XX","LIV"};
int i,value;
for(i=0;i<6;i++)
{
if(input[i][0]>=’0’&&input[i][0]<=’9′) //skips the numbers
{
continue;
}
else //prints the corresponding roman numerals
{
value=roman(input[i]);
printf("%s=>%d\n",input[i],value);
}
}
return 0;
}
[/code]
#include
using namespace std;
int number(char roman)
{
switch(roman) //return roman equivalent
{
case ‘I’:
return 1;
case ‘V’:
return 5;
case ‘X’:
return 10;
case ‘L’:
return 50;
case ‘C’:
return 100;
case ‘D’:
return 500;
case ‘M’:
return 1000;
default:
return 0;
}
}
int roman(string str)
{
int cur,nxt,val=0;
cur = number(str[0]);
val = cur;
for(int i=1;str[i] != ‘\0’;i++)
{
nxt = number(str[i]);
if(cur >= nxt)
val +=nxt;
else
val +=(nxt-cur)-1;
cur = nxt;
}
return val;
}
int isNumber(string str)
{
for(int i=0;str[i] != ‘\0’;i++)
{
if(!(str[i] >= ‘0’ && str[i] <= '9') )
return false;
}
return true;
}
int main()
{
string s[6] ={"2","III","4","IV","XX","LIV"};
int val;
for(int i=0;i<6;i++)
{
if(isNumber(s[i] ))
continue;
else
{
val = roman(s[i]);
cout<<s[i]<<" "<<val<<endl;
}
}
return 0;
}