public class Postfix {
public static void main( String args[] ) {
String Math = "2 2 2 * * 3 % 1 +";
String[] a = Math.split( " " );
LinkedListStack<Float> test = new LinkedListStack<Float>(a.length);
for(int i = 0; i<a.length; i++){
if(a[i].equals("-")==true){
float p=test.top();
float b=test.pop();
float n=b-p;
test.pop();
test.push(n);
}else
if(a[i].equals("+")==true){
float p=test.top();
float b=test.pop();
float n=b+p;
test.pop();
test.push(n);
}else
if(a[i].equals("*")==true){
float p=test.top();
float b=test.pop();
float n=b*p;
test.pop();
test.push(n);
}else
if(a[i].equals("/")==true){
float p=test.top();
float b=test.pop();
float n=b/p;
test.pop();
test.push(n);
}else
if(a[i].equals("%")==true){
float p=test.top();
float b=test.pop();
float n=b%p;
test.pop();
test.push(n);
}else{test.push( Float.parseFloat(a[i]) );}
}
System.out.println( "Answer is "+test.top());
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น