//Roman numbers to decimal conversion ,c++ program
#include<iostream.h>
#include<process.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
int s=0,beg=0,k,end=k,i,c=0,mid,m[50],p,j;
char a[50];
clrscr();
cout<<"ENTER THE ROMAN NUMBER\n";
gets(a);
k=strlen(a);
for(i=0;i<k;i++)
{
switch(a[i])
{
case 'I':
p=1;
m[i]=p;
c++;
break;
case 'V':
p= 5;
m[i]=p;
c++;
break;
case 'X':
p=10;
m[i]=p;
c++;
break;
case 'L':
p=50;
m[i]=p;
c++;
break;
case 'C':
p=100;
m[i]=p;
c++;
break;
case 'D':
p=500;
m[i]=p;
c++;
break;
case 'M':
p=1000;
m[i]=p;
c++;
break;
default:cout<<"sorry INVALID\n";
getch();
exit(0);
}
}
for(i=0;i<k;i++)
{
while(beg<end)
{
mid=(end+beg)/2;
end=mid;
if(beg==end)
{
s=s+m[beg];
beg=beg+1;
end=k;
}
else if((end-beg)==1)
{
if(m[end]>m[beg])
{
s=s+(m[end]-m[beg]);
beg=end+1;
end=k;
}
}
}
}
cout<<"Decimal value "<<s;
getch();
}
No comments:
Post a Comment