• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

need a postfix for calculation

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Fire me boy! Cool, soothing, shameless self promotion:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic