ROMAN TO ARABIC AND VICE VERSA

a program that will convert roman number to arabic

Page 1 of 1

5 Replies - 23766 Views - Last Post: 20 March 2009 - 07:48 PM Rate Topic: -----

#1 long01shen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 09

ROMAN TO ARABIC AND VICE VERSA

Post icon  Posted 17 March 2009 - 12:52 AM

this code works fine, but i need to ask the user to choose w/c kind of convertion he like, roman or arabic? and after he chooses one example roman to arabic after that the user is again asked do you want to continue? and if yes ask again roman or arabic convertion.

need help pls


#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>


char *otoroman[4][9] = {"I","II","III","IV","V","VI","VII","VIII","IX","X",
					   "XX","XXX","XL","L","LX","LXX","LXXX","XC","C","CC","CCC","CD","D","DC","DCC","DCCC","CM",
					   "M","MM","MMM"};

char roman[7] = {'I','V','X','L','C','D','M'};
unsigned romanvalue[7] = {1,5,10,50,100,500,1000};

char *o_to_roman(int input);
int roman_to_o(char *input);

// convert hindu arabic numbers to roman
char *o_to_roman(int input)
{
	int count,point,r;
	char result[80]={"\0"};
		
	for(count=1000,point=3; count>=1; count=count/10,point--)
	{
		r = input/count;
		if (r >= 1)
		{
			input = input % count;
			strcat(result,otoroman[point][r-1]);
		}
	}
	return (result);
}

// convert roman to hindu_arabic
int roman_to_o(char *input)

{
	unsigned count,count1,total=0;

	// examine each character starting from the right most
	// character
	while(*input != NULL)
	{
		if(*input == roman[6])	// if the current character = M
			total += romanvalue[6];		// total1 = total + 1000
		else
		{
			for (count = 0; count<6; count++)
			{
				if (*input == roman[count])
				{
					// determine if the right hand character value
					// is bigger than the current one
					for (count1=count+1; count1<6; count1++)
						if (*(input+1)==roman[count1])
						{
							total +=(romanvalue[count1] - romanvalue[count]);
							input++;
							break;
						}
						//if the right hand character is smaller than the
						// current one, then total + (value of roman)
						if (count1==6)
							total += romanvalue[count];
						break;
				}// end if
			}// end for
		} // end else
		input++; // scan the next character
	}// end while
	return(total);
}

main()
{
	char input[80];
	int digit;
	printf("\nEnter a number,hindu-arabic or roman : ");
	scanf("%s",input);
	if (isdigit(input[0]))
	{
		digit = atoi(input);
		if(digit > 3999 || digit <=0)
			printf("\nInteger out of range ");
		else
			printf("\n The number in Roman is: %s",o_to_roman (digit));

	}
		else
			printf("\n The Roman number to decimal is %d",roman_to_o(input));
		return 0;
}



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: ROMAN TO ARABIC AND VICE VERSA

#2 tjust80   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 73
  • Joined: 01-December 08

Re: ROMAN TO ARABIC AND VICE VERSA

Posted 17 March 2009 - 03:06 AM

you can see code on

http://www.dreaminco...wtopic92975.htm

what you want man...
Was This Post Helpful? 0
  • +
  • -

#3 Hyper   User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: ROMAN TO ARABIC AND VICE VERSA

Posted 17 March 2009 - 01:21 PM

Here's a snippet of a (working) converter from Arabic to Roman.
Was This Post Helpful? 0
  • +
  • -

#4 long01shen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 09

Re: ROMAN TO ARABIC AND VICE VERSA

Posted 17 March 2009 - 07:36 PM

i dont need to change my code, all i ask is how to add a loop, like, asking first the user which convertion he wants,then after it ask again do u want to quit? if no, go back again to the first question which convertion he wants bla bla. . . . then loop again, the program only ends when the user do not want to continue anymore.
Was This Post Helpful? 0
  • +
  • -

#5 Hyper   User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: ROMAN TO ARABIC AND VICE VERSA

Posted 17 March 2009 - 08:27 PM

    bExit = false;
    while (!bExit) {

        /* do stuff */
        ....
        cout << "(0 no 1 yes) Exit? ";
        cin >> bExit;
    }

Was This Post Helpful? 0
  • +
  • -

#6 long01shen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 09

Re: ROMAN TO ARABIC AND VICE VERSA

Posted 20 March 2009 - 07:48 PM

i just finished editing it and here it goes:

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>

	char input[80];
	int digit;


char *otoroman[4][9] = {"I","II","III","IV","V","VI","VII","VIII","IX","X",
			"XX","XXX","XL","L","LX","LXX","LXXX","XC","C","CC",
			"CCC","CD","D","DC","DCC","DCCC","CM",
			"M","MM","MMM"};

char roman[7] = {'I','V','X','L','C','D','M'};
unsigned romanvalue[7] = {1,5,10,50,100,500,1000};

char *o_to_roman(int input);
int roman_to_o(char *input);

// convert hindu arabic numbers to roman
char *o_to_roman(int input)
{
	int count,point,r;
	char result[80]={"\0"};

	for(count=1000,point=3; count>=1; count=count/10,point--)
	{
		r = input/count;
		if (r >= 1)
		{
			input = input % count;
			strcat(result,otoroman[point][r-1]);
		}
	}
	return (result);
}

// convert roman to hindu_arabic
int roman_to_o(char *input)

{
	unsigned count,count1,total=0;

	// examine each character starting from the right most
	// character
	while(*input != NULL)
	{
		if(*input == roman[6])	// if the current character = M
			total += romanvalue[6];		// total1 = total + 1000
		else
		{
			for (count = 0; count<6; count++)
			{
				if (*input == roman[count])
				{
					// determine if the right hand character value
					// is bigger than the current one
					for (count1=count+1; count1<6; count1++)
						if (*(input+1)==roman[count1])
						{
							total +=(romanvalue[count1] - romanvalue[count]);
							input++;
							break;
						}
						//if the right hand character is smaller than the
						// current one, then total + (value of roman)
						if (count1==6)
							total += romanvalue[count];
						break;
				}// end if
			}// end for
		} // end else
		input++; // scan the next character
	}// end while
	return(total);
}

main()
{

  char input[80], A,B,C, ans;
	int digit,choice;

do{
C:
cout<<"\n*********************************************";
cout<<"\n**	   A Program That Converts		   **";
cout<<"\n**	Arabic Numbers to Roman Numerals	 **";
cout<<"\n**		   and Back Again				**";
cout<<"\n*********************************************";
cout<<"\n\nPress 1 to convert Arabic to Roman Numeral";
cout<<"\nPress 2 to convert Roman Numeral to Arabic";
cout<<"\n\n\n\nEnter Choice:  ";
cin>>choice;

switch(choice)
{
case 1:

printf("\nEnter an Arabic Number: ");
	scanf("%s",input);
	if (isdigit(input[0]))
	{
		digit = atoi(input);
		if(digit >= 3900|| digit <=0)
			printf("\nInteger out of range ");
		else
			printf("\nThe Equivalent Roman number is %s",o_to_roman (digit));

	}
		else
			cout<<"\nInvalid Input!!!!";
			
			goto D;

case 2:

{

printf("\nEnter a Roman Numeral: ");			 //use capital letters
cin>>input;
printf("\nThe Equivalent Arabic number is %d",roman_to_o(input));
getch();
goto D;

}
default:
	cout<<"\nInvalid Input";
	goto C;
}
D:
cout<<"\n\n\n\n\n\nDo you Want to Continue?";
cout<<"\nPress Y for YES, N for NO";
cout<<"\nCHOICE: ";
cin>>ans;

}
while
(ans=='y' || ans =='Y');

cout<<"\n\n\n\n\End Of Program";
getch();
return 0;
}


now it works fine.

*Mod Edit: Added code tags*

This post has been edited by OliveOyl3471: 09 January 2010 - 09:42 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1