PDA

View Full Version : Roman Calculator in C


malcolmmatheson
02-28-2005, 03:27 PM
Could anyone help me create a roman calculator in C language. (Not C++).
The following is a simple calculator in decimal form. I need to converter that converts Roman to decimal, and then decimal back to roman after the calculation has been carried out. The user inputs a roman number, and then outputs a roman number. (Eg. IV + V = IX)

Thanks

#include <stdio.h>


int main( void ) {
int i, j, calculation;
char c;
printf("Enter your calculation using + - / *\n");
scanf("%d",&i);
scanf("%c",&c);
scanf("%d",&j);

switch ( c ) {
case'+':calculation = i + j; break;
case '-':calculation = i - j; break;
case '*':calculation = i * j; break;
case '/':if ( j == 0 ) printf("Error, cant divide by zero");
else {calculation = i / j;}
if (j>i)
printf("First number must be higher then second!");
{
default: printf("Not Accepted operator");break;
return 0;
}
}
if (j==0)
printf(".");
else{
printf("The result is: %d\n",calculation);
} }

Mod Edit: It's a good idea to take advantage of the vb [code] tags to maintain legibility. Take 'em for a spin. :)

abhishek.in
02-28-2005, 03:44 PM
well, two things can be done.

FIRST: this method is less (least) brainy, and certainly not good programming.
u can build a look-up table for all (almost..) the numbers and replace all the decimals u use by the corresponding roman numeral.
but then, this is an easy way out for ur cause.
as romans did not use any "solid rules" for number representation, (or we didn't understand) this might work well.
its fool proof too.

SECOND: this is a better method frm the programming pt of view.
u set up ur own rules, same as we set up productions for a particular grammar.
here, the produtions might be::

I :: 1
V :: 5
X :: 10

and so on...
along with...

*I = *+1;
*II = *+2;
*III = *+3;

I* = *-1;

now, when productions are drawn, u write down conditionals to decide the way in which u want to represent a number.

... easy to say, it was.
bye.

malcolmmatheson
02-28-2005, 03:59 PM
Thanks for the help, but I need more than that. :D The actual code, if possible as I am still learning C. I know the ideas, but not sure how to impliment them.
Any help would be greatly appriciated.
I have slightly changed the code, as the previous one I posted does not work.
Thanks

#include <stdio.h>


int main( void ) {
float i, j, calculation;
char c;
printf("Enter your calculation using + - / *\n");
scanf("%f",&i);
scanf("%c",&c);
scanf("%f",&j);

switch ( c ) {
case'+':calculation = i + j; break;
case '-':calculation = i - j; break;
case '*':calculation = i * j; break;
case '/':if ( j == 0 ) printf("INVALID! Error, cant divide by zero");
calculation = i / j;
if (j>i)
printf("INVALID! First number must be higher then second!");
else {
printf("The result is: %.2f\n",calculation); }
return 0;

{
default: printf("INVALID! Not Accepted operator");break;
return 0;
}
}
if (j==0)
printf(".");
else{
printf("The result is: %.2f\n",calculation);
return 0;
} }

Mhtml
02-28-2005, 08:28 PM
Bah to C I say, if only you had taken to C++. Classes and operator overloading would solve your problem.