roman numeral to decimal integer

naveenreddy61 0 Tallied Votes 893 Views Share

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.

//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,138 Posting Sage

First, INDENT your code.

#include<stdio.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);
    return 0;
}

Second, don't use non-standard (and obsolete) headers like conio.h.

Third, use a decent compiler. I rather suspect this was tried on 20+ year old turbo c.
Modern compilers spot a lot more things, like

$ gcc -W -Wall -ansi -pedantic -O2 foo.c
foo.c:3: warning: return type defaults to ‘int’
foo.c: In function ‘main’:
foo.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[10]’
foo.c:56: warning: suggest brackets around comparison in operand of &
foo.c:58: warning: suggest brackets around comparison in operand of &
foo.c:58: warning: suggest brackets around comparison in operand of &
foo.c:60: warning: suggest brackets around comparison in operand of &
foo.c:60: warning: suggest brackets around comparison in operand of &
foo.c:62: warning: suggest brackets around comparison in operand of &
foo.c:62: warning: suggest brackets around comparison in operand of &
foo.c:62: warning: suggest brackets around assignment used as truth value
foo.c:64: warning: suggest brackets around comparison in operand of &
foo.c:64: warning: suggest brackets around comparison in operand of &
foo.c:64: warning: suggest brackets around assignment used as truth value
foo.c:66: warning: suggest brackets around comparison in operand of &
foo.c:66: warning: suggest brackets around comparison in operand of &
foo.c:66: warning: suggest brackets around comparison in operand of &
foo.c:66: warning: suggest brackets around assignment used as truth value
foo.c:68: warning: suggest brackets around comparison in operand of &
foo.c:68: warning: suggest brackets around comparison in operand of &
foo.c:68: warning: suggest brackets around comparison in operand of &
foo.c:68: warning: suggest brackets around assignment used as truth value
foo.c:74: warning: suggest brackets around comparison in operand of &
foo.c:80: warning: suggest brackets around comparison in operand of &
foo.c:8: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
foo.c:64: warning: array subscript is below array bounds
foo.c:66: warning: array subscript is below array bounds
foo.c:68: warning: array subscript is below array bounds

The array bound warnings are extremely troublesome.
Possibly ALL the places you used &, you should have used &&

Fourth, it doesn't work with the prefix notation.

$ ./a.out 
please enter the roman numeral :II
value is 2 $ 
$ ./a.out 
please enter the roman numeral :XIX
value is 21 $

The second one should print 19, not 21.

commented: Well said. +13
commented: thank you +0
adnan.siddique -6 Light Poster

i think i have the best one
that is

//code to convert roman numeral to integer
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int ret_level(char ch)
{
	switch (ch)
	{
	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 -1;
	}
}
int main()
{
	char str[10];
	scanf("%s",str);
	//printf("%s",str);
	int len = strlen(str),x,sum = 0;
	x = len-1;
	int last_level = ret_level(str[x--]);
	sum = last_level;
	//int * array = (int *)malloc(10*sizeof(int));
	while (x >= 0)
	{
		if (last_level <= ret_level(str[x]))
			sum = sum + ret_level(str[x]);
		else
			sum = sum - ret_level(str[x]);
		last_level = ret_level(str[x]);
		x--;
	}
	printf("%d",sum);
	return 0;
}
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

i think i have the best one

You violate Salem's 2nd rule and you think your solution is better? Think again.
And your switch statement is terrible. And you used scanf("%s",str); , too. Very dangerous...

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.