import java.util.EventListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.lang.String;

/**
 * This class contains a method for calculating calculations
 * written in post fix notation, and a GUI to help acess this
 * calculator.
 * 
 * @author Michael Thomas Rivers
 * @version 1.0
 **/
public class Main
{
// no constructor is necessary so it is omitted

    /*
     * The main method creates a GUI to access the calculator
     */
    public static void main(String [ ] args)
    {
        final GUI gui = new GUI();
        gui.button.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent e) 
            {
                gui.input = gui.inputField.getText();
                gui.output = evaluatePostFixInput(gui.input);
                gui.outputField.setText(gui.output);
            }
        });
    }
    
    /*
     * This evaluates a calculation written in post fix notation
     */
    public static String evaluatePostFixInput(String input)
    {
        String output = calculate(input);
        
        return output;
    }
    
    /*
     * This calculates the string in post fix notation
     * and returns the result
     */
    public static String calculate(String input)
    {
        String output = null;
        ArrayStack stack = new ArrayStack();
        StreamTokenizer streamTokenizer = new StreamTokenizer(
            new StringReader(input));
        streamTokenizer.parseNumbers();
        streamTokenizer.ordinaryChar('/');
    
        int end = StreamTokenizer.TT_EOF;
        int tokenType = end + 1;
        try
        {
            while (tokenType != end)
            {
                tokenType = streamTokenizer.nextToken();
                if (tokenType == StreamTokenizer.TT_NUMBER)
                {                    
                    System.out.println("number: " + streamTokenizer.nval);
                    Double x = new Double(streamTokenizer.nval);
                    stack.push(x);
                    output = x.toString();
                }
                else if (tokenType == StreamTokenizer.TT_WORD)
                {
                    System.out.println("word:   " + streamTokenizer.sval + " not calculated");
                }
                else
                {// Must be an operator or some other character.
                    String symbol = String.valueOf((char) tokenType);
                    if (tokenType != StreamTokenizer.TT_EOF)
                    {
                        System.out.println("other:  " + symbol);
                        if( symbol.equals("*") )
                        {
                            Double x = (Double)stack.pop();
                            double x1 = x.doubleValue();
                            Double y = (Double)stack.pop();
                            double y1 = y.doubleValue();
                            double z1 = y1 * x1;
                            Double z = new Double(z1);
                            stack.push(z);
                            output = z.toString();
                        }
                        else if( symbol.equals("/") )
                        {
                            Double x = (Double)stack.pop();
                            double x1 = x.doubleValue();
                            Double y = (Double)stack.pop();
                            double y1 = y.doubleValue();
                            double z1 = y1 / x1;
                            Double z = new Double(z1);
                            stack.push(z);
                            output = z.toString();
                        }
                        else if( symbol.equals("+") )
                        {
                            Double x = (Double)stack.pop();
                            double x1 = x.doubleValue();
                            Double y = (Double)stack.pop();
                            double y1 = y.doubleValue();
                            double z1 = y1 + x1;
                            Double z = new Double(z1);
                            stack.push(z);
                            output = z.toString();
                        }
                        else if( symbol.equals("-") )
                        {
                            Double x = (Double)stack.pop();
                            double x1 = x.doubleValue();
                            Double y = (Double)stack.pop();
                            double y1 = y.doubleValue();
                            double z1 = y1 - x1;
                            Double z = new Double(z1);
                            stack.push(z);
                            output = z.toString();
                        }
                    }
                }
            }// End while
        }
        catch (Exception err)
        {}
        return output;
    }
}
