help me guys! i have a problem on how to use Stack! :((

Reply

Join Date: Dec 2008
Posts: 2
Reputation: bhob is an unknown quantity at this point 
Solved Threads: 0
bhob bhob is offline Offline
Newbie Poster

help me guys! i have a problem on how to use Stack! :((

 
0
  #1
Dec 20th, 2008
write a java program that checks if parenthesis and brackets are balanced in an arithmetic expression.

help me!! ( i dunno how to solve that problem.. is it the same as the calculator java code? the concept, i mean.. help me guys..
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,615
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: help me guys! i have a problem on how to use Stack! :((

 
0
  #2
Dec 20th, 2008
It's pretty simple.
  • Parse the expression entered and iterate over it on a character by character basis.
  • If the given character is not a parenthesis or bracket, move over to the next one. If it is an opening parenthesis or bracket, push it on to the stack and move on to the next one.
  • If the given character is a closing parenthesis or bracket, check its compatibility by peeking at character at the top of stack. If it matches, pop the top of the stack and move on to the next character. If it doesn't, throw an error.
  • If the stack contains extra characters even after the entire expression has been parsed, throw an error.
Though I have ignored the details out there, I am pretty sure you can take it from here. Implement the above logic and post the entire code if you have any problems.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 15
Reputation: awo is an unknown quantity at this point 
Solved Threads: 1
awo's Avatar
awo awo is offline Offline
Newbie Poster

Re: help me guys! i have a problem on how to use Stack! :((

 
0
  #3
Dec 20th, 2008
Am sorry I may not be of full help now cause am also new i JAVA but let me give you the algorithm that came to my mind on this
import java.util.Stack; read all the mathimatical equation into a String variable say mathString
convert the String to array of character using
mathStString.toCharArray(); assign this to a variable say
mathCharArray
create a Stack object eg
Stack stack= new Stack(); use a for loop to scan through mathCharArray
if there is an occurrence of character '(', it will be pushed onto the stack object using
stack.push(new Character('(')); if the char is ')' checkk
if the stack is not empty,
throw an Exception that there is a closing parenthesis with out a corresponding opening parenthesis.
otherwise
remove the character from the stack

after completing the loop cheek if stack is empty
if stack is not empty
throw an exception that there is an unclosed parenthesis
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,574
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 199
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: help me guys! i have a problem on how to use Stack! :((

 
0
  #4
Dec 22nd, 2008
^ Huh?


What S.o.S said is correct, and also, if I'm not mistaken, is what Eclipse and a lot of other IDE's and editors use to 'check' whether or not there is a syntax error (w/ parens and brackets).
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: bhob is an unknown quantity at this point 
Solved Threads: 0
bhob bhob is offline Offline
Newbie Poster

Re: help me guys! i have a problem on how to use Stack! :((

 
0
  #5
Jan 3rd, 2009
import java.util.EmptyStackException;
import java.util.Stack;

public class Balance {
Braces br = new Braces();

public boolean isBalanced(String string){
Stack<Character> stack = new Stack<Character>();
boolean bal = true;
int i = 0;

while(bal && (i < string.length())){
char ch = string.charAt(i);
i++;
if(br.open_bracket(ch)){
stack.push(ch);
}
else if(br.close_bracket(ch)){
try{
stack.pop();
}catch(EmptyStackException exception){
bal = false;
}
}

}
if(bal && stack.isEmpty()){
return true;
}
else{
return false;
}
}
}


---------------- is this ok? another problem, i dunno how to code if there are no parentheses..
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: help me guys! i have a problem on how to use Stack! :((

 
1
  #6
Jan 3rd, 2009
1. Use code tags to post code.
2. Instead of asking of us whether your code is ok or not you can check it yourself, if it does what it is intended to do you know what it is, whether it doesn't you still know what it is. Why do you need our approval.
@~s.o.s~ said : If the given character is not a parenthesis or bracket, move over to the next one
3. doesn't this pretty much explain what you got o do when there are no paranthesis at all ? You just keep on moving over all the characters till the end of the expression.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC