balanced parantheses
Apr 21, 2014 at 2:10am UTC
I dont no how to fix. Can someone help me out plz. (Please see the picture)
[IMG]
http://i61.tinypic.com/2r2cfnq.png[/IMG]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#ifndef DYNINTSTACK_H
#define DYNINTSTACK_H
class DynIntStack
{
private :
// Structure for stack nodes
struct StackNode
{
int value; // Value in the node
StackNode *next; // Pointer to the next node
};
StackNode *top; // Pointer to the stack top
public :
// Constructor
DynIntStack()
{ top = NULL; }
// Destructor
~DynIntStack();
// Stack operations
void push(int );
void pop(int &);
bool bracketsame(char ,char );
bool balancedparantheses(string); //checks if parentheses are
bool isEmpty();
};
#endif
#include "DynIntStack.h"
#include <iostream>
#include <string>
using namespace std;
// Main to run program
int main()
{
string expression;
cout<<"Enter an expression to check: " ;
cin>>expression;
if (DynIntStack::balancedparantheses(expression))
cout<<"Balanced expression \n" ;
else
cout<<"Expression is not balanced \n" ;
}
bool bracketsame(char opening,char closing) // function checks if opening and closing brackets are same
{
if (opening == '(' && closing == ')' ) return true ;
else if (opening == '{' && closing == '}' ) return true ;
else if (opening == '[' && closing == ']' ) return true ;
return false ;
}
bool balancedparantheses(string exp) //checks if parentheses are
{ //balanced or not
stack<char > StackNode;
for (int i =0;i<exp.length();i++)
{
if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[' )
StackNode.push(exp[i]);
else if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']' )
{
if (StackNode.empty() || !bracketsame(StackNode.top(),exp[i]))
return false ;
else
StackNode.pop();
}
}
return StackNode.empty() ? true :false ;
}
Apr 21, 2014 at 11:30am UTC
#include <stack>
But that wont solve all your problems
You have declared
1 2
bool bracketsame(char ,char );
bool balancedparantheses(string); //checks if parentheses are
as part of a DynIntStack and defined them as standalone? Need to add
DynIntStack::
to them then declare a variable of DynIntStack then call the function as
DynIntStack.whatever
Topic archived. No new replies allowed.