/*
Program reads an expression terminated by an s, and tells whether the
parentheses (), [], and {} match correctly or not
*/
#include <cstdlib>
#include <iostream>
#include "stack.h"
using namespace std;
int main(int argc, char *argv[])
{
stack equation; // stack of open parentheses
char ch; // current character
char popped; // an element popped off the stack
bool good; // true if everything matches
good = true;
cout << "Enter an equation followed by an s:\n";
cin >> ch;
// continue reading an processing characters until an s is read
while (ch != 's')
{
// if it is an open parenthesis, put on the stack
if (ch == '{' || ch == '[' || ch == '(')
{
equation.push (ch);
}
else
// if it is a close parenthesis, pop the stack and match
if (ch == '}' || ch == ']' || ch == ')')
{
// stuff on the stack, ok to pop
if (! equation.empty ())
{
popped = equation.pop ();
// if the characters don't match
if (! (popped == '{' && ch == '}' ||
popped == '[' && ch == ']' ||
popped == '(' && ch == ')'))
good = false;
}
else // nothing on the stack
good = false;
}
cin >> ch;
}
if (good && equation.empty ())
cout << "\n\nYes, it matched";
else
cout << "\n\nNo, it was bad!";
cout << "\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}