import java.io.*;
import java.util.*;
import javax.swing.*;
public class Calculator {
private static Stack operators = new Stack();
private static Stack operands = new Stack();
public static void main(String args[]) throws IOException {
JOptionPane.showMessageDialog(null, "Hello user! I hope you have a great time using this. = ]", "Welcome", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null, "Note: Enter Infix expressions with spaces in between.", "Welcome", JOptionPane.PLAIN_MESSAGE);
String infix = JOptionPane.showInputDialog("Enter Mathematical Expression Here: ");
String output = "The expression in Postfix is: " + convertToPostfix(infix);
JOptionPane.showMessageDialog(null, output);
String answer = "The answer to the equation: " + evaluate(convertToPostfix(infix));
JOptionPane.showMessageDialog(null, answer);
String options[] = {"Yes","No",};
int option = JOptionPane.showOptionDialog(null,"Do you want to evaluate another expression?", "Calculator", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,options,options[0]);
JOptionPane.showMessageDialog(null, "Thank you for using this calculator.\n Have a nice day! = ]");
}
private static String convertToPostfix(String infix) {
StringTokenizer s = new StringTokenizer(infix);
String symbol;
String postfix = "";
while (s.hasMoreTokens()) {
symbol = s.nextToken();
if (Character.isDigit(symbol.charAt(0)))
postfix = postfix + " " + (Integer.parseInt(symbol));
else if (symbol.equals("(")){
Character isOperator = new Character('(');
operators.push(isOperator);
}
else if (symbol.equals(")")) {
while (((Character)operators.peek()).charValue() != '(') {
postfix = postfix + " " + operators.pop();
}
operators.pop();
} else {
while (!operators.empty() && !(operators.peek()).equals("(") && precedence(symbol.charAt(0)) <= precedence(((Character)operators.peek()).charValue()))
postfix = postfix + " " + operators.pop();
Character isOperator = new Character(symbol.charAt(0));
operators.push(isOperator);
}
}
while (!operators.empty())
postfix = postfix + " " + operators.pop();
return postfix;
}
private static int evaluate(String postfix) {
StringTokenizer s = new StringTokenizer(postfix);
int value;
String symbol;
while (s.hasMoreTokens()) {
symbol = s.nextToken();
if (Character.isDigit(symbol.charAt(0))) {
Integer isOperand = new Integer(Integer.parseInt(symbol));
operands.push(isOperand);
}else{
int op2 = ((Integer)operands.pop()).intValue();
int op1 = ((Integer)operands.pop()).intValue();
int result = 0;
switch(symbol.charAt(0)){
case '*': {result = op1 * op2; break;}
case '+': {result = op1 + op2; break;}
case '-': {result = op1 - op2; break;}
case '/': {result = op1 / op2; break;}
case '%': {result = op1 % op2; break;}
}
Integer isOperand = new Integer(result);
operands.push(isOperand);
}
}
value = ((Integer)operands.pop()).intValue();
return value;
}
private static int precedence(char operator) {
if (operator == '+' || operator == '-' )
return 1;
else if (operator == '*' || operator == '/' || operator == '%')
return 2;
return 0;
}
}
In my main method, the part were it ask the user if it wants to evaluate another expression, if YES it must go back to "enter mathematical expression" and evaluates again and if NO it should exit and show "thank you for using this calculator". My problem is that i don't know how to loop it.. any ideas anyone??