the code takes each character and stores where and how many times a roman numeral appears and the assess the integer. this is my first code im posting. anything to make the code better is most welcome.
roman numeral refers to
M=1000
D=500
C=100
L=50
X=10
V=5
I=1
and uses the standard method of assessing roman numeral.
roman numeral to decimal integer
//code to convert roman numeral to integer
#include<stdio.h>
#include<conio.h>
main()
{
int n=0,i=0,x=0,ax=0,bx=0,cx=0,dx=0,ex=0,fx=0,gx=0,g[10],a[10],b[10],c[10],d[10],e[10],f[10];
char r[10];
printf("please enter the roman numeral :");
scanf("%s",&r);
for(;i<10;i++)
{
switch(r[i])
{
case'I':
a[ax]=i;++ax;
break;
case'V':
b[bx]=i;++bx;
break;
case'X':
c[cx]=i;++cx;
break;
case'L':
d[dx]=i;++dx;
break;
case'C':
e[ex]=i;++ex;
break;
case'D':
f[fx]=i;++fx;
break;
case'M':
g[gx]=i;++gx;
break;
default:
;
}
}
for(i=0;r[i]!=0 ;i++)
{
x++;
}
n = 1000*(gx);
for(i=0;i<fx;i++)
{
if(gx==0)
n=500*fx;
else if(f[i]>g[gx-1])
n=n+500;
else
n=n-500;
}
for(i=0;i<ex;i++)
{
if(gx==0&fx==0)
n=100*ex;
else if(fx==0&gx!=0&e[i]<g[gx-1])
n=n-100;
else if(fx==0&gx!=0&e[i]>g[gx-1])
n=n+100;
else if(fx=!0&gx==0&e[i]<f[fx-1])
n=n-100;
else if(fx=!0&gx==0&e[i]>f[fx-1])
n=n+100;
else if(fx=!0&gx!=0&e[i]>f[fx-1]&e[i]>g[gx-1])
n=n+100;
else if(fx=!0&gx!=0&e[i]>g[gx-1]&e[i]<f[fx-1])
n=n-100;
}
n= ((50*dx)+n);
n= ((10*cx)+n);
for(i=0;i<cx;i++)
{
if(c[i]<e[ex-1]&c[i]<d[dx-1])
n = n-20;
}
n= ((5*bx)+n);
n= ((1*ax)+n);
for(i=0;i<ax;i++)
{
if(a[i]<b[bx-1]&a[i]<c[cx-1])
n = n-2;
}
printf("value is %d ",n);
getch();
return 0;
}
Salem 5,199 Posting Sage
Dave Sinkula commented: Well said. +13
naveenreddy61 commented: thank you +0
adnan.siddique -6 Light Poster
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.