• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

problem implementing stack to solve postfix expressions

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello..

I've been stuck on at this particular point for a few days now, and I feel I've researched forums and documentations as best I could, finding some topics to relate to the subject of my questions but not completely..

I've written a stack class in order to read into a text file containing postfix expressions and evaluate them..

stack class code
------------------------

public class stack {

private int MAX, count;
private int[]data;

public stack(){ //constructor
MAX = 100;
data = new int[MAX];
count = 0; //keep track of integers in box
}
boolean isEmpty(){
return count == 0;
}

public void push(int x){
data[count] = x;
++count;
}
public int pop(){
if(!isEmpty()){
return data[--count];
}else{
System.out.println("Error! Empty Stack");
return -1;

}

}
public int top(){
if(!isEmpty()){
return data[count - 1];
}else{
System.out.println("Error!! Empty Stack");
return -1;
}

}
}
-------------------------------------

I feel like I have that correct. but my other problem is knowing what to do with the other class with the main method. i know the program should scan the file left to right, and if its an operand push, if its an operator pop twice, but yet i cant figure out in the first if statement if im supposed to parse a string to an int or not. thats really all i want to know. thanks, hope i've been thorough or at least not TOO thorough..
 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Seth Hensler:
i cant figure out in the first if statement if im supposed to parse a string to an int or not.



Which if statement are you talking about??...
And if you want to know how to resolve the postfix expression,(whether by int or String)....Then it all depends on your requirement. If the program is supposed to SHOW the resolved postfix expressions with algebraic expressions, Strings will be used...And if the program is supposed to EVALUATE the expression, then int's are used...

Does it help??
Ramy...
 
Seth Hensler
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh ok, sorry I didnt realise how unclear that was. by "first if statement" I meant (some psuedo code)--> if(item is operand) then push..

heres where im stuck :

stack stk = new stack();

Scanner scan = new Scanner(new File("input.txt"));

String item;



while(scan.hasNext()){
data = scan.next();

if(item.???) //need to see if next item is operand

}
}
------------------------------
Right now im only concerned with the result of the expression being displayed. but that makes sense.Im just trying to figure out how to get it to recognize an int in a string. I've tried parsing it and even isDigit but nothings worked so far. im just stuck on : if(item.is operand) part.
Thanks for your help, I do appreciate it.
 
Ramender Mall
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One solution can be to use your own function..

like
if(item.isOperand()){}

where isOperand() takes character in the string we are scanning and then compare it to the given operands,(operands are going to be very few, so switch-case might do)..

Ramy....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!