| | |
stacks problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
the program i have to write involves a dynamically linked stack to evaluate if the grouping symbols '(' , ')' , '{' , '}' , '[' , ']' in expressions are balanced. The expression is input through keyboard.
For example:
expression: {A+(B-C)*D} is balanced
expression: T+{(R+M)/G-V*L is not balanced
expression: )A( is not balanced
expression: (A) is balanced
I do not have to do anything to the variables and operators but just determine if the grouping symbols are balanced. The stipulations are that i have to use a stack and that i can not use a counter to simply count the number of grouping symbols because that would evaluate )A( as balanced.
what iv come up with so far is to take the input as a string, convert it to individual characters, input to a stack using a for loop with the length of the string as the cutout.
what i can't seem to figure out is how to evaluate if the expression is balanced using only the pop, push, and top functions allowed with stacks. The only thing i can come up with is a complicated nested if,switch, and while statements that i am only half sure will work so there must be a simpler way.
any help or point into the right direction is greatly appreciated
For example:
expression: {A+(B-C)*D} is balanced
expression: T+{(R+M)/G-V*L is not balanced
expression: )A( is not balanced
expression: (A) is balanced
I do not have to do anything to the variables and operators but just determine if the grouping symbols are balanced. The stipulations are that i have to use a stack and that i can not use a counter to simply count the number of grouping symbols because that would evaluate )A( as balanced.
what iv come up with so far is to take the input as a string, convert it to individual characters, input to a stack using a for loop with the length of the string as the cutout.
what i can't seem to figure out is how to evaluate if the expression is balanced using only the pop, push, and top functions allowed with stacks. The only thing i can come up with is a complicated nested if,switch, and while statements that i am only half sure will work so there must be a simpler way.
any help or point into the right direction is greatly appreciated
loop throught all the elements on the stack, check what it is either ()[]{} and use 3 different boolean values, 1 for each brace type ie 1 for () 1 for [] and 1 for {}. then toggle states...if that makes sense and if the state wouldn't chage then you know they are not balanced.
That was poorly worded i think sorry
Chris
That was poorly worded i think sorry

Chris
Knowledge is power -- But experience is everything
•
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
This is easy.... Count
If you get below zero then you have failed,
and if you get not-zero at the end you have failed.
You might want to check scope bridges. I.e.
but that is easy to modify the code below to include.
Example of pseudo code for JUST ( and ).
[/code]
Additionally, I am sorry BUT you cannot use boolean values as suggested by freaky_chris since
the expression
If you get below zero then you have failed,
and if you get not-zero at the end you have failed.
You might want to check scope bridges. I.e.
(a{b+c)} which I would pass as fail. but that is easy to modify the code below to include.
Example of pseudo code for JUST ( and ).
c++ Syntax (Toggle Plain Text)
string A = "(expession) with ) ( "; int b(0); for(int i=0;i<A.length();i++) { if (A[i]=='(') b++; if (A[i]==')') { b--; if (b<0) failed(); } } if (b!=0) failed(); success();
Additionally, I am sorry BUT you cannot use boolean values as suggested by freaky_chris since
the expression
((x+y)) would fail. And that is not acceptable. Last edited by StuXYZ; Dec 5th, 2008 at 6:30 pm. Reason: added comment on freaky_chris's quicker comment.
•
•
•
•
This is easy.... Count
If you get below zero then you have failed,
and if you get not-zero at the end you have failed.
You might want to check scope bridges. I.e.
(a{b+c)}which I would pass as fail.
but that is easy to modify the code below to include.
Example of pseudo code for JUST ( and ).
[/code]c++ Syntax (Toggle Plain Text)
string A = "(expession) with ) ( "; int b(0); for(int i=0;i<A.length();i++) { if (A[i]=='(') b++; if (A[i]==')') { b--; if (b<0) failed(); } } if (b!=0) failed(); success();
Additionally, I am sorry BUT you cannot use boolean values as suggested by freaky_chris since
the expression((x+y))would fail. And that is not acceptable.
Chris
Knowledge is power -- But experience is everything
•
•
Join Date: Nov 2008
Posts: 32
Reputation:
Solved Threads: 1
You can traverse through the string using a pointer ofcourse :-
-if u find any open braces (and i mean OPEN ' (,{,[ ') Push it to the stack.
-While u still traversing the expression if u find any CLOSED braces(and i mean Closed ' ) , } , ]' Pop the stack and compare the popped element with the closed brace u found if it is correct then the expression is balanced if it doesn't then it is invalid expression.
Note :- I think that is the way that some compilers check for the syntax errors that occur because the braces.
-if u find any open braces (and i mean OPEN ' (,{,[ ') Push it to the stack.
-While u still traversing the expression if u find any CLOSED braces(and i mean Closed ' ) , } , ]' Pop the stack and compare the popped element with the closed brace u found if it is correct then the expression is balanced if it doesn't then it is invalid expression.
Note :- I think that is the way that some compilers check for the syntax errors that occur because the braces.
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
so i got it kinda working in the sense that no matter what is input the program evaluates the expression as unbalanced here is the code
this is the function definition and you can assume that the member functions to the stack class are all correct with the "isEmptyStack" returning a value of true if the stack is empty.
any help is appriciated
C++ Syntax (Toggle Plain Text)
void eval() { string exp; int num; linkedStackType<char>stack; cout << endl << "Input evaluation: "; cin >> exp; cout << endl << exp; num = exp.length(); for(int x=0; x<num; x++) { if(exp[x] == '(' || exp[x] == '{' || exp[x] == '[') { stack.push(exp[x]); } else if(exp[x] == ')' || exp[x] == '}' || exp[x] == ']') { if(exp[x] == stack.top()) { stack.pop(); } } } if(stack.isEmptyStack()) { cout << endl << "is balanced" << endl << endl; } else { cout << endl << "is not balanced" << endl << endl; } }
this is the function definition and you can assume that the member functions to the stack class are all correct with the "isEmptyStack" returning a value of true if the stack is empty.
any help is appriciated
Last edited by kyosuke0; Dec 6th, 2008 at 3:40 am.
Not too basd, the reason is this code here
This should be a comparison to the opposite of stack.top() for example you are comparing like this -> ']' == '[' which will never be true. You will need to check that the brace type is the same and is opposite to the one of the top of the stack, if thats true then pop. if it isn't true, then break out of your loop since it is unbalanced.
Chris
C++ Syntax (Toggle Plain Text)
if(exp[x] == stack.top()) { stack.pop(); }
Chris
Knowledge is power -- But experience is everything
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
now whenever an expression that starts with a close group symbol it asserts an error at "else if(exp[x] == '}')" for all close group symbols.
it does this for expressions like:
)A(
AEW}de{
here is the updated code
other than that everything else works fine
it does this for expressions like:
)A(
AEW}de{
here is the updated code
C++ Syntax (Toggle Plain Text)
void eval() { string exp; int num; linkedStackType<char>stack; cout << endl << "Input evaluation: "; cin >> exp; cout << endl << exp; num = exp.length(); for(int x=0; x<num; x++) { if(exp[x] == '(' || exp[x] == '{' || exp[x] == '[') { stack.push(exp[x]); } else if(exp[x] == ')' || exp[x] == '}' || exp[x] == ']') { if(exp[x] == ')') { if(stack.top()=='(') { stack.pop(); } } else if(exp[x] == '}') { if(stack.top()=='{') { stack.pop(); } } else if(exp[x]==']') { if(stack.top()=='[') { stack.pop(); } } } } if(stack.isEmptyStack()) { cout << endl << "is balanced" << endl << endl; } else { cout << endl << "is not balanced" << endl << endl; } }
other than that everything else works fine
![]() |
Similar Threads
- hello i have a problem (VB.NET)
- problem in creating a tree (C)
- Stacks using doubly linked lists (C++)
- Stacks - balanced parentheses (C++)
- Runtime error!! ACM problem (C++)
- stack palindrome problem? (C++)
- Implementation of Stacks and Queues (C)
Other Threads in the C++ Forum
- Previous Thread: Quick sort and merge sort together using both vectors and arrays.
- Next Thread: Free C++ IDE for Windows XP
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





