Assignment 4: Function overloading and coding style

This homework is a fair sight easier than the previous homeworks. This doesn't mean you should delay (any more than you have to because I didn't put it out until so late, sorry) in starting it.

A Rational RPN claculator

For this assignment you will be writing an RPN calculator. This is not your ordinary RPN calculator though, it is one that computes in rational numbers rather than real numbers. Rational numbers are number that can be represented as a fraction of integers. An rpn calculator is one that puts the operators at the end of the statment (post fix) like on an HP calculator. This means that to add two numbers, you punch in the first number, followed by the second number, followed by a plus. This is the easiest thing to parse in the world. Whenever you get a number, you puch it onto a stack. Each time you get an operator, you pop the last two numbers off the stack, and do the operator on them, pushing the result on the stack. The reason you push the result back on the stack is that you then can do more operations on it. For instance:
4 5 + 3 + 
is the same as:
4 + 5 + 3
in infix notation (what we normally use). The really cool thing about RPN calculators is that you don't ever need parenthesis. The order of the operators always determines which comes first. Consider:
(5 + 3) * 2
is the same as
2 5 3 + *
pretty neat, no?

This assignment has two main parts. First is the rational number class, and second is the stack. Then of course is using it, but that is pretty easy. I would reccommend getting your rational number class working before going on to the rest. Use some test mains for that.

Rational Numbers

Rational numbers are numbers with an integer numerator and an integer denominator. If the denominator is one then the rational number is also an integer. The denominator is never zero. For simplicity in mine, I made sure that the denominator was also always positive. If the number is negative I make the numerator be the negative one.

In the sample code you will notice a pretty cool function to allow you to output a rational number just like any other. You will need to overload a number of other operators (like + - etc...) in order to use your Rational number class like any other.

The rational number should always be in most reduced form. This is something that should not happen very often. It can be kind of expensive. Make sure you reduce your rational number efficiently. You may want to do it iteratively first. If there is a tricky part to the assignment this is it. One possible method of reducing the fraction utilizes Euclid's method of finding the greatest common denominator. Euclid stated around 300 BC that:

gcd(a,b) = gcd(b, a mod b)
The modulus operator in c++ is %. You should make it work first then try to make it efficient. e.g. finish the assignment with this part being recursive then come back and make it iterative.

One last function you might want is one which will return the decimal equivilent, if you want that functionality in your calculator.

Stack

C'mon. Make a stack of rational numbers! Each time you get a new number, you push a number on the stack. That means your push should take a number to push on the stack. Obviously then the pop will give a number back. This means no memory managment for us, it should all happen inside the stack. Couldn't be easier!

Parser

This time around we have a super simple parser. Just read in a string. If the first char is a digit, hand it to the number conversion function I am giving you.

Rational strtorat(const char *input)
{
	int totalNumerator = 0;
	int totalDenominator = 0;
	int i = 0;
	char ch = input[i];
	while (ch != 0 && ch != '/')
	{
		totalNumerator *= 10;
		totalNumerator += (ch - '0');
		i++;
		ch = input[i];
	}
	if (ch == '/')
	{
		i++;
		ch = input[i];
		while (ch != 0)
		{
			totalDenominator *= 10;
			totalDenominator += (ch - '0');
			i++;
			ch = input[i];
		}
	}
	else
	{
		totalDenominator = 1;
	}
	return Rational(totalNumerator, totalDenominator);
}

If not, then it is some command or another. Just do a switch or something, and do the right thing with the stack. This makes it easy, and looking like a normal RPN calculator which you need to hit enter after each number for. My calculator looks at the first letter to determine which operator it is, for instance p is the same as print stack. You must implement the following operators:

Others you might want to try: Here is some output from my calculator:
> 5
> 4
> +
9
> 5
> /
(9/5)
> 3
> 2
> /
(3/2)
> p
0: (3/2)
1: (9/5)
> +
(33/10)
> 2
> 10
> /
(1/5)
> +
(7/2)
> d
3.5
> 3
> 7
> /
(3/7)
> *
(3/2)
> n
(-3/2)
> c
Stack Cleared
>