In: Computer Science
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Stack;
public class Main {
public static int evaluatePostfix(String exp) {
Stack<Integer> stack=new
Stack<>();
for(int
i=0;i<exp.length();i++)
{
char c =
exp.charAt(i);
if(Character.isDigit(c))
stack.push(c - '0');
else {
int val1 = stack.pop();
int val2 = stack.pop();
switch(c) {
case '+':
stack.push(val2+val1);
break;
case '-':
stack.push(val2- val1);
break;
case '/':
stack.push(val2/val1);
break;
case '*':
stack.push(val2*val1);
break;
}
}
}
return
stack.pop();
}
public static void main(String[] args) throws
IOException {
if (args.length != 1) {
System.out.println("Invalid command line arguments....");
} else {
String fileName
= args[0];
String exp =
"231*+9-";
BufferedWriter
writer = new BufferedWriter(new FileWriter(fileName));
writer.write("Postfix evaluation of " + exp + " is : " +
evaluatePostfix(exp));
writer.close();
}
}
}