• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need a postfix for calculation

 
d mib
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need helpwith setting this up
user types in a calculation i.e. 2+2. the program sends this to a stack and then converts it to a postfix i.e. 22+. it then displays the postix and results to the user.
how can i do this???
 
Rajeev Asthana
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by d mib:
i need helpwith setting this up
user types in a calculation i.e. 2+2. the program sends this to a stack and then converts it to a postfix i.e. 22+. it then displays the postix and results to the user.
how can i do this???

Get any book that talks about Data Structures or Algorithms. You will find this in either Stacks or in Expressions. Implementing in Java won't be difficult once you know how to do it.
Hope this helps.
 
d mib
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what i got done so far but need help desperatly
public class postfix{
private String postfixString, outputString;
private boolean isOperator(char c){
return (c=='+'||c=='-'||c=='*'||c=='/');
}
private boolean is Space(char c){
return(c=='');
}
public void interpretPostfix(){
Stack evalStack = new Stack();
double leftOperand, rightOperand;
char c;
StringTokenizer parser = new String Tokenizer(postfixString,"+-*/", true);
while(parser.hasMoreTokens()){
String token = parser.nextToken();
c = token.charAt(0);
if ((token.lenth()==1)&&isOperator(c){
rightOperand = ((Double) evalStack.pop()).doubleValue();
leftOperand = ((Double) evalStack.pop()).doubleValue();
switch (c){
case '+':evalStack.push(newDouble (leftOperand+rightOperand));
break;
case '-':evalStack.push(newDouble (leftOperand-rightOperand));
break;
case '*':evalStack.push(newDouble (leftOperand*rightOperand));
break;
case '/':evalStack.push(newDouble (leftOperand/rightOperand));
break;
default:
break;
}
}else if ((token.length()==1)&&isSpace(c)){
;
}else{
evalStack.push(Double.valueOf(token));
}
}
output("Value of postfix expression =" + evalStack.pop());
}
private void output(String s){
outputString = s;
}
public void setInput(String input){
postfixString = input;
}
public String getOutput(){
return outputString;
}

------------------------------------------------------------------------------
//stack code
public class Stack{
private int count;
private int capacity;
private int capacityIncrement;
private Object[]itemArray;
public Stack(){
count =0;
capacity = 10;
capacityIncrement = 5;
itemArray = new Object[capacity];
}
public boolean empty(){
return (count ==0);
}
public void push(Object X){
if (count==capacity){
capacity+=capacityIncrement;
Object[]tempArray = new Object[capacity];
for(int i = 0; i<count; i++){
tempArray[i] = itemArray[i];
}
itemArray = tempArray;
}
itemArray[count++] = X;
}
public Object pop(){
if(count ==0){
return null;
}else{
return itemArray[--count];
 
d mib
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and this is another attampt i did but i really need help
public class a{
public static int evaluate(String postfix)
{
int result = 0;
for(int i=0;i<postfix.length();i++){
String ch = postfix.substring(i,i+1);
//if ch is a constant push it onto the stack
if(!ch.equals("+")&&!ch.equals("-")&&!ch.equals("*")&&!ch.equals("/"))
stack.push(ch);
else{
if(ch.equals("+")){

try{
int op1 = Integer.parseInt((String)stack.pop());
int op2 = Integer.parseInt((String)stack.pop());
result = op1 + op2;
System.out.println(op1+"+"+op2+"="+result);
stack.push(new Integer(result).toString());
}//try
catch(StackUnderflowException ex){
System.out.println(ex);
}//catch
}
if(ch.equals("-")){
try{
int op1 = Integer.parseInt((String)stack.pop());
int op2 = Integer.parseInt((String)stack.pop());
result = op2 - op1;
System.out.println(op2+"-"+op1+"="+result);
stack.push(new Integer(result).toString());
}//try
catch(StackUnderflowException ex){
System.out.println(ex);
}//catch
}
if(ch.equals("*")){
try{
int op1 = Integer.parseInt((String)stack.pop());
int op2 = Integer.parseInt((String)stack.pop());
result = op1 * op2;
System.out.println(op1+"*"+op2+"="+result);
stack.push(new Integer(result).toString());
}//try
catch(StackUnderflowException ex){
System.out.println(ex);
}//catch
}

if(ch.equals("/")){
try{
int op1 = Integer.parseInt((String)stack.pop());
int op2 = Integer.parseInt((String)stack.pop());
result = op2 /op1;
System.out.println(op2+"/"+op1+"="+result);
stack.push(new Integer(result).toString());
}//try
catch(StackUnderflowException ex){
System.out.println(ex);
}//catch
}
}//else
}//for
try{
result = Integer.parseInt((String)stack.pop());
}
catch(StackUnderflowException ex){
System.out.println(ex);
}
return result;
}//evaluate();
}//Expressions
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic