Creating a Program using Stacks

I have to write a program that takes as input an arithmetic expression followed by a semicolon. I have no idea how to write a program using stacks and my professor doesn't give her students any extra help. She gave us a Pseudocode for this, but it honestly doesn't help me. Here's the Pseudocode:

In main()
1. Create a stack of type “char” with 100 elements
stackType(int stackSize = 100);
//constructor
//Create an array of the size stackSize to hold
//the stack elements. The default stack size is 100.
//Postcondition: The variable list contains the base
// address of the array, stackTop = 0, and
// maxStackSize = stackSize.
2. Prompt user for an expression
3. Read in ONE character
4. While (the character is not ‘;’) do
a. Print the character
b. Switch on the character
• If the character is {, [ or (
Push it into the stack (be sure the stack is not full)
• If the character is }, ] or )
o When stack is not empty
get the top of the stack
pop the stack
check if this top of the stack is NOT the same as the input character
print a not match message
o When the stack is empty
print a not match message
• Read in ONE character
5. Finally, check if the stack is not empty
print a not match message
else
print a match message
You are going to have to be a little more specific. What don't you understand? From what I can tell, your teacher has listed the program word-for-word in clear English. All you have to do is implement it in code.
Here's the instructions for the assignment:

Write a program that takes as input an arithmetic expression. The program outputs whether the expression contains matching grouping symbols. For example, the arithmetic expressions {25 + (3 – 6) * 8} and 7 + 8 * 2 contain matching grouping symbols. However, the expression 5 + {(13 + 7) / 8 - 2 * 9 does not contain matching grouping symbols.
In my mind, I just have to write a code that prompts the user to enter numbers. However, that's not the case. It seems that I have to write a code where I prompt the user to enter in values and have it where it puts in the parentheses and operator symbols.
Ah, so its a basic program to test whether grouping symbols are balanced. So basically, what you have to do is prompt the user to enter a string. This string is going to be the entire expression ( for example, "{25 + (3 – 6) * 8}" ). Then, you are going to iterate through each character of the string. If it is a opening grouping character, you will push said character onto the stack. If you encounter a closing grouping symbol, pop off the character that is at the top of the stack.

This way, at the end of the loop (when it reaches the ';'), if you have an empty stack, that means the expression contains balanced grouping symbols, but if your stack still has elements in it that weren't popped off, that means that those grouping symbols were not balanced and therefore do not have a match.
It still doesn't make sense to me. First, would I make it so that after having the user input a character/value, do I make so it keeps asking the user to input characters until they input a semicolon?
Second, I don't understand the push and pop. From what I know, wouldn't pop delete an element? So how would it input a ], }, or )?
1. Create a stack of type “char” with 100 elements

Sorry, I misunderstood what you were asking in the last question. It's clearer now that this is what you needed:

std::stack<char> as (100, 0);

EDIT:

You can see how to better use std::stack here:

http://www.cplusplus.com/reference/stack/stack/stack/
Last edited on
do I make so it keeps asking the user to input characters until they input a semicolon?

The user enters a line of text. Your program reads the line one char at a time until it reads a semicolon. So something like:

1
2
3
4
5
std::cout << "Expression: ";
for (char ch: std::cin.get(ch) && ch != ';'; )
{
    //...
}

wouldn't pop delete an element? So how would it input a ], }, or )?

Only the opening brackets are pushed onto the stack. When you encounter a closing bracket, you remove the top member of the stack (i.e., the last opening bracket pushed) and see if it matches the closing bracket.
Last edited on
Topic archived. No new replies allowed.