Homework 4

Part 1

Vote. Bring evidence.

Part 2: The Calculator

In this assignment, you will create a calculator that uses a stack to store values. You should read in a value from the user. If it is a number, push it on the stack. If it is an operator, pop the top two values off the stack, apply the operator, and push on the result. A sample interaction looks like this:
>5
>2.6
>+
7.6
>2
>/
3.8
>1
>9
>-
8
>+
11.8
>+
error: not enough elements
>1
>+
12.8
>q
Thank you for using the calculator
For reasons described below, your strings should not be the string.h type here, but character pointers (arrays of characters). An example of this is below. You will read in a string and use a switch statement to determine whether it is a number, an operator, or a quit command. Think about how you might determine if something is a number. Remember, character pointers are also arrays, and you can access individual elements of the array.

The Stack

Make a stack of doubles to hold the value. Stack code from class is linked under lectures. Feel free to use and expand from there.

And a little help...

Now, since you're reading in strings from the user, you'll need to be able to convert them to doubles to do the math. To do that, you use a function called atof in the stdlib.h file. However, in order for that to work, you need to represent your strings as character pointers. Here is some sample code. It shows the library you need to include, and how to use it.
#include <stdlib.h>
#include <iostream>

int main() {
	char* x = "1.2";
	cout << atof(x);  
}