/** RPN Calculator by Neil Moomey
* This calculator uses Reverse Polish Notation similar
* to the HP calculators.
*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Rpn6 extends Frame implements ActionListener{
	
	boolean numberFlag, isNumber;
	Stack myStack = new Stack();
		
	TextField t;
	GridBagLayout gbl = new GridBagLayout();
	
    Rpn6() {
        super("Neil's RPN Calculator");
	numberFlag=true;
	isNumber=false;
		
        setLayout(gbl);	
		
	//makeButton(String label, int x, int y, int w, int h)		
	int row = 0;		
	makeTextField("0", 0, row, 0, 1);
		
	row=1;
	makeButton("DN", 0, row, 1, 1);
        makeButton("LN", 1, row, 1, 1);
	makeButton("CSC", 2, row, 1, 1);
	makeButton("SEC", 3, row, 1, 1);
	makeButton("COT", 4, row, 1, 1);

	row=2;
	makeButton("PI", 0, row, 1, 1);
        makeButton("e^x", 1, row, 1, 1);
	makeButton("SIN", 2, row, 1, 1);
	makeButton("COS", 3, row, 1, 1);
	makeButton("TAN", 4, row, 1, 1);
		
	row=3;
	makeButton("x><y", 0, row, 1, 1);
        makeButton("INT", 1, row, 1, 1);
	makeButton("1/x", 2, row, 1, 1);
	makeButton("SQT", 3, row, 1, 1);
	makeButton("y^x", 4, row, 1, 1);
		
	row=4;
	makeButton("CHS", 0, row, 1, 1);
        makeButton("7", 1, row, 1, 1);
	makeButton("8", 2, row, 1, 1);
	makeButton("9", 3, row, 1, 1);
	makeButton("/", 4, row, 1, 1);
		
	row=5;
	makeButton("EEX", 0, row, 1, 1);
        makeButton("4", 1, row, 1, 1);
	makeButton("5", 2, row, 1, 1);
	makeButton("6", 3, row, 1, 1);
	makeButton("x", 4, row, 1, 1);
		
	row=6;
	makeButton("Enter", 0, row, 1, 2);  // Has a width of two cells
        makeButton("1", 1, row, 1, 1);
	makeButton("2", 2, row, 1, 1);
	makeButton("3", 3, row, 1, 1);
	makeButton("-", 4, row, 1, 1);	
		
	row=7;
	//makeButton("4", 0, row, 1, 1);
        makeButton("0", 1, row, 1, 1);
	makeButton(".", 2, row, 1, 1);
	makeButton("CLR", 3, row, 1, 1);
	makeButton("+", 4, row, 1, 1);        

        setSize(235, 280);
        show();
		
	System.out.println("t.getText()="+t.getText());
    }
	
	void makeButton(String label, int x, int y, int w, int h) {
        GridBagConstraints c = new GridBagConstraints();		
	setFont(new Font("Monospaced", Font.BOLD, 12));		
        Button b = new Button(label);
	if (label == "/") b = new Button("" + (char)247);
        c.gridx = x;
	c.gridy = y;
	c.gridwidth = w;
        c.gridheight = h;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
	gbl.setConstraints(b, c);
	b.addActionListener(this);
        add(b);
    }

       //makeTextField("0", 0, row, 0, 1);
    	void makeTextField(String label, int x, int y, int w, int h) {
        GridBagConstraints c = new GridBagConstraints();
        t = new TextField(label);
        c.gridx = x;
        c.gridy = y;
	c.gridwidth = w;
        c.gridheight = h;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;		
        gbl.setConstraints(t, c);
        t.addActionListener(this);
	add(t);
    	}	

    	public void actionPerformed(ActionEvent evt) {
    	String value = (String)evt.getActionCommand();
    	String newValue = "";
    	System.out.println("value = "+value);
    	String s = t.getText();
		
		
    	try {		
        	if (value == "Enter") {
		//push on stack	
		myStack.push(s);
		System.out.println("Enter = "+ myStack.peek());	
		numberFlag=false;
    	}
		
    	if ( (value == "0") || (value == "1") || (value == "2") || (value == "3") || (value == "4") || (value == "5") || (value == "6") || (value == "7") || (value == "8") || (value == "9") || (value == ".")) {
			    
    	if (s.equals("0") && (value != ".")) {
		newValue = value;
		t.setText(newValue);
		myStack.push(newValue);
	}
	else if (!numberFlag) {
		newValue = value;
		t.setText(newValue);
		myStack.push(newValue);
		numberFlag=true;
	} else {
		newValue = s + value;
		t.setText(newValue);
		myStack.pop();
		myStack.push(newValue);
	}

	System.out.println("newValue="+newValue);
	System.out.println("t.getText()="+t.getText());	
					
        }
		
	if (value == "+") {
		double xd = StringTodouble(myStack.pop().toString());
		double yd = StringTodouble(myStack.pop().toString());
		double total=yd+xd;
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}
		
	if (value == "-") {
		double xd = StringTodouble(myStack.pop().toString());
		double yd = StringTodouble(myStack.pop().toString());
		double total=yd-xd;
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}		
		
	if (value == "x") {
		double xd = StringTodouble(myStack.pop().toString());
		double yd = StringTodouble(myStack.pop().toString());
		double total=yd*xd;
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}
		
        String ch = "" + (char)247;
	if (value == ch) {
		double xd = StringTodouble(myStack.pop().toString());
		double yd = StringTodouble(myStack.pop().toString());
		double total=yd/xd;
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}
		
	if (value == "CHS") {
		double xd = StringTodouble(myStack.pop().toString());
		double total = -xd;
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}		
		
		
	if (value == "CLR") {
		double xd = StringTodouble(myStack.pop().toString());
		myStack.push("0");
		t.setText("0");
		numberFlag=true;
	}		
		
	if (value == "EEX") {
		double xd = StringTodouble(myStack.pop().toString());
		double yd = StringTodouble(myStack.pop().toString());
		double total=yd*Math.pow(10,xd);
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}		
		
	if (value == "x><y") {
		double xd = StringTodouble(myStack.pop().toString());
		double yd = StringTodouble(myStack.pop().toString());
		myStack.push(doubleToString(xd));
		myStack.push(doubleToString(yd));
		t.setText(doubleToString(yd));
		numberFlag=false;
	}	
		
	if (value == "1/x") {
		double xd = StringTodouble(myStack.pop().toString());
		double total = 1/xd;
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}	
		
	if (value == "SQT") {
		double xd = StringTodouble(myStack.pop().toString());
		double total = Math.sqrt(xd);
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}		
		
	if (value == "e^x") {
		double xd = StringTodouble(myStack.pop().toString());
		double total = Math.exp(xd);
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}
		
	if (value == "y^x") {
		double xd = StringTodouble(myStack.pop().toString());
		double yd = StringTodouble(myStack.pop().toString());
		double total = Math.pow(yd,xd);
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}		
		
	if (value == "INT") {
		double xd = StringTodouble(myStack.pop().toString());
		double total = Math.rint(xd);
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}			
		
	if (value == "LOG") {
		double xd = StringTodouble(myStack.pop().toString());
		double total = Math.log(xd);
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}

	if (value == "PI") {
		myStack.push("3.141592654");
		t.setText("3.141592654");
		numberFlag=false;
	}			

	if (value == "DN") {
		double xd = StringTodouble(myStack.pop().toString());
		double yd = StringTodouble(myStack.pop().toString());
		t.setText(doubleToString(yd));
		numberFlag=false;
	}		
		
	if (value == "CSC") {
		double xd = StringTodouble(myStack.pop().toString());
		double total = Math.asin(xd);
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}		
		
	if (value == "SEC") {
		double xd = StringTodouble(myStack.pop().toString());
		double total = Math.acos(xd);
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}

	if (value == "COT") {
		double xd = StringTodouble(myStack.pop().toString());
		double total = Math.atan(xd);
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}		
		
	if (value == "SIN") {
		double xd = StringTodouble(myStack.pop().toString());
		double total = Math.sin(xd);
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}

	if (value == "COS") {
		double xd = StringTodouble(myStack.pop().toString());
		double total = Math.cos(xd);
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}

	if (value == "TAN") {
		double xd = StringTodouble(myStack.pop().toString());
		double total = Math.tan(xd);
		myStack.push(doubleToString(total));
		t.setText(doubleToString(total));
		numberFlag=false;
	}	
		
        } catch (Exception e) {
		System.out.println(e);
	}
		
	System.out.println("");
    }
	
	//String to double
    public double StringTodouble(String s) {
	    double d = new Double(s).doubleValue();
	    return d;
	}

	//double to String
	public String doubleToString(double d) {
		String s = new Double(d).toString();
		return s;
	}
	
    static public void main(String[] args) {
        Rpn6 frame = new Rpn6();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
            public void windowActivated(WindowEvent e) {
            }
            public void windowOpened(WindowEvent e) {
            }			
        });	
    }
		
}
