Hi all,
I am working on an assignment on creating a program that recognizes whether an expression has balanced parentheses or not. The program has to use ADT (Abstract Data Type) stacks.
The user should input all data in form of an algebraic expression, fx (a+b)/(d-e) or ((a+b). The program should then determine whether the parantheses in the expression are balanced.
I have written a program, ... and it works and gives the correct output. So why am I writing?
Well, I would appreciate any opinion on the program. I just started learning about stacks so I am uncertain if I solved it the best way.
Here comes the entire code - and many thanks for your help.
#include <stdlib.h>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
const int maxstack = 100;
typedef char stackitems;
// ************ STRUCT STACK ****************
struct stack
{
stackitems data[maxstack] ; // array where data is stored
int stacktop ; // when it is -1, stack is empty
};
stack s;
// ************ FUNCTION INITSTACK ****************
void initstack(stack & s) // empty the stack
{
s.stacktop = -1; // set stacktop to be empty
}
// ************ FUNCTION ISEMPTY ****************
bool isEmpty(const stack& s) // returns true or false
{
return s.stacktop==-1; // function is true if stacktop has value of -1
}
// ************ FUNCTION POP ****************
void pop ( stack & s, stackitems & x) // removes the top item
{
if (isEmpty(s)) // if stack is empty, cannot remove data
{
cout << "Stack underflow"<<endl;
exit(1); //terminate the program
}
else
{
x = s.data[s.stacktop]; // retrieve data first
s.stacktop--; // remove from stacktop
cout << "Has popped data '" << x << "' from stack.\n"; // display info to the user
}
}
// ************ FUNCTION PUSH ****************
void push( stack & s, const stackitems & x)
// places element x on the stack
{
if (s.stacktop == maxstack -1) // not more than maxstack-1 is allowed
{
cout<<"Stack overflow"; // items on the stack.
exit(1); //terminate the program
}
else
{
s.stacktop++; // move the top of the stack up
s.data[s.stacktop] = x; // add new item,x
cout << "Has added data '" << x << "' to stack.\n"; // display message to the user
}
}
// ************ FUNCTION TOP ****************
stackitems top(const stack& s)
//returns the top item from the stack in parameter x
{
if (isEmpty(s))
{
cout<<"Stack underflow"<<endl;
exit(1);
}
else
return s.data[s.stacktop] ;
}
// ************ FUNCTION CHECK ****************
void check()
{
const char leftparen = '(';
const char rightparen = ')';
bool error = false;
char ch;
cout << "Please enter an algebraic expression: ";
cin.get(ch);
while (( ch != '\n') && (!error) )
{
if (ch == leftparen)
push(s,ch);
if (ch == rightparen)
if (isEmpty(s))
error = true;
else
pop(s,ch);
cin.get(ch);
}
if ((!error) && isEmpty(s))
cout<<"\nResult: This expression is valid. \n"<<endl;
else
cout <<"\nResult: This expression is invalid. \n"<<endl;
}
// ************ FUNCTION MAIN ****************
void main ()
{
initstack(s); // initialising stack to -1; i.e. it is empty
check();
} // end main