Question: Write a C++ Program: Stack class For this program, you will not need DMA (dynamic memory allocation). For this program, you may not use container

Write a C++ Program:

Stack class

For this program, you will not need DMA (dynamic memory allocation).

For this program, you may not use container classes from the C++ STL (such as stack or vector, for example). I want you to write your own program logic.

Write a class named Stack that implements a stack of unsigneds.

Your Stack class should include a default ctor that initializes the stack to represent an empty stack (the "free" copy ctor, the "free" dtor, and the "free" assignment operator are probably fine, since we're not doing DMA) Your data should be private. (To hold the items of the stack, you'll probably declare an array of unsigneds.)

void push(unsigned item);

If the stack is already full, c&d; otherwise, push the item on the stack

unsigned pop();

If the stack is already empty, c&d; otherwise, remove the item on the top of the stack and return its value

unsigned top() const;

If the stack is already empty, c&d; otherwise, return the value on the top of the stack without changing the stack

void show() const;

Output the stack values (but not the array elements that are not currently stack values). I'm not fussy about the formatting, so don't spend significant time making your output pretty. Output the "bottom" of the stack first, and the "top" of the stack last

void size() const;

Return the number of items in the stack (which in general may be less than the number of elements in your array)

You decide the details of setting up your class's private data, but I assume that your data will include an array of unsigneds. Now that you have your Stack class, use your Stack class to implement the following Polish calculator.

Your main will input an expression, such as

# 3 # 5 # 4 ! - # 6 + # 10 ! * ! + .

and output the answer, in this case 73 == 3 + (5-4+6)*10

Your calculator will implement 3 binary operators:

* multiplication

+ addition

- subtraction

Your main doesn't have to do any error checking. (Your class checked for stack overflow and stack underflow, but your main can just assume without checking that:

every number is preceded by '#'

every operator is preceded by '!'

the line is terminated by a '.'

no stack overflow will happen

no arithmetic overflow will happen

we'll never subtract a bigger number from a smaller number

the user types a valid Polish postfix expression

I don't care about prompt and explanatory text; fine if you have them, fine if you don't.

Your program only has to solve one expression, it doesn't need to loop through several.

The #'s, !'s and period simplify your program: you won't need string streams, you can just use the #/!/. to see what will follow

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!