Part 1 - Balanced Symbols Check (10 points)
Determine and report whether an expression is balanced. { [ } ] is not balanced. [ ( ) ] is balanced and valid.
//* class ExpressionManagerInterface
{
public:
ExpressionManagerInterface(){}
virtual ~ExpressionManagerInterface(){}
/*
* Checks whether an expression is balanced on its parentheses
*
* - The given expression will have a space between every number or operator
*
* @return true if expression is balanced
* @return false otherwise
*/
*//
// ExpressionManager.h
// Lab3
// Created by Claudia Cruz on 5/20/14.
#ifndef __Lab3__ExpressionManager__
#define __Lab3__ExpressionManager__
#include <iostream>
#endif /* defined(__Lab3__ExpressionManager__) */
#include <stack>
using namespace std;
I need to check that "{" "[" and "(" match with "}" "]" and ")". They way I did it was creating an if statement inside of another if. Im a beginner and I actually need help with my code.
If you don't know what a stack is, imagine a stack of plates. If you want to add a plate to the stack it goes on top, and if you want to remove a plate you take it off the top. Basically, you can only add and remove items from one side.
A stack could be used here by iterating through the expression and adding a char to the stack if it's a {, [, or (. If you find a ), ], or }, check if it matches the corresponding item on top of the stack. If it does, remove that item and continue. If it doesn't, your expression is not balanced. If you reach the end of the expression and the stack is empty, the expression is balanced.