(Thanks~~~...This is first time i found this page and post my problem..so....sorry if i did anything wrong.......This code is changing infix to postfix and then calculate out the result)
/** The <code>Stack</code> interface specifies the basic operations
* of a last-in-first-out (LIFO) containers.
*/
public interface Calculation {
public void push(Object object);
/** return the element number in this stack
*/
public double pop();
/**Adds the specified element to the top of this stack.
* @param object the element to be pushed onto this stack.
*/
public Object peek();
/**
* Removes and returns the element at the top of this stack.
* @return the element at the top of this stack.
* @throws IllegalStateException if this stack is empty.
*/
public void resize();
}
(Above is my interface...^^^)
public abstract class ArrayCalculation implements Calculation{
private Object[] stack;
private int top;
private int size;
public void push(){
top++;
stack[top]=top;
}
public double pop(){
Object result = stack[top];
top--;
return (Double) result;
}
public Object top(){
return stack[top];
}
public boolean isFull(){
return(top == size);
}
public boolean isEmpty(){
return(top == -1);
}
public int size(){
return(top + 1);
}
}
import java.util.Scanner;
public class TestArrayCalculation {
public static void main(String[] args)
{
char ch;
ArrayCalculation s;
double operan1, operan2, Result;
System.out.println("The postfix expression is: ");
Scanner scanner = new Scanner(System.in);
String e = scanner.nextLine();
s = new ArrayCalculation(); <<<how should i correct it??
int i = 0;
double trueValue;
while (ch == System.in.read() && ch != '\n') {
ch = e.charAt(i);
i++;
trueValue = ch - 48;
if (Character.isDigit(ch) ){
s.push(trueValue);
}
else {
operan2 = s.pop();
operan1 = s.pop();
switch (ch) {
case '+' : s.push(operan1+operan2);
break;
case '-' : s.push(operan1-operan2);
break;
case '*' : s.push(operan1*operan2);
break;
case '/' : s.push(operan1+operan2);
break;
}
}
}
trueValue = s.pop();
System.out.println(" = " + trueValue);
}
}