/* Put E in screen if more than MAX_VALUE. How?
Change from int to Long
*/
import java.lang.*;
import java.awt.*;
import java.applet.Applet;

public class Calc2c extends Applet {

    CalcFrame calc;
    public void init() {
      calc = new CalcFrame("Calculator");
      add(calc);
    }
       

    public void start(){
	calc.resize(200,350);
	calc.show();
    }

    public void stop() {
    }
}

class CalcFrame extends Frame {
    Panel sub1;
    Panel sub2;
    Panel sub2a;
    Panel sub2b;
    TextField screen;
    TextField modulus;

    public CalcFrame(String title) {
        super(title);
        setFont(new Font("Helvetica", Font.PLAIN, 20));
	this.setLayout(new PackerLayout());
	sub1 = new Panel();
	sub1.setLayout(new PackerLayout());
	sub1.add("label1;side=top;pady=2", new Label("Minnesota Instruments  MI-01"));
	sub1.add("label2;side=top;pady=2", new Label("Modular Calculator"));
	screen = new TextField("0",20);
	sub1.add("screen;side=top;pady=2", screen);
	add("subpanel1;side=top;expand=true", sub1);
	sub1.resize(200,100);
	sub1.show();

	sub2 = new Panel();
	sub2.setLayout(new PackerLayout());

	sub2a = new Panel();
        sub2a.setLayout(new GridLayout(6,3,2,2));
        sub2a.add(new Button("7"));
        sub2a.add(new Button("8"));
        sub2a.add(new Button("9"));
        sub2a.add(new Button("4"));
        sub2a.add(new Button("5"));
        sub2a.add(new Button("6"));
        sub2a.add(new Button("1"));
        sub2a.add(new Button("2"));
        sub2a.add(new Button("3"));
        sub2a.add(new Button("0"));
        sub2a.add(new Button("(-)"));
        sub2a.add(new Button("C"));
        sub2a.add(new Button("+"));
        sub2a.add(new Button("x"));
        sub2a.add(new Button("^"));
        sub2a.add(new Button("-"));
        sub2a.add(new Button("/"));
        sub2a.add(new Button("="));

        sub2a.resize(100,250);
	sub2a.show();
	sub2.add("subpanel;side=left;padx=4;expand=true", sub2a);
	this.add("subpanel;side=bottom;expand=true", sub2);
	sub2.resize(200,250);
	sub2.show();

	sub2b = new Panel();
	sub2b.setLayout(new PackerLayout());

        sub2b.add("label3;side=top", new Label("Modulus"));
	modulus = new TextField("12",10);
        sub2b.add("modulus;side=top;pady=2", modulus);
        sub2b.add("buttondemo;side=top;pady=4;expand=true", new Button("Show Demo"));
        sub2.add("subpanel;side=right;padx=4;expand=true", sub2b);
        sub2b.resize(100,250);
        sub2b.show();

    }

// Action buttons (+, =, etc.) have type 2; number buttons have type 1
    int buttonType(Button b) {
        int f;
        String l = b.getLabel();
        if ((l == "=") || (l == "+") || (l == "-") || (l == "x") || 
            (l == "/") || (l == "^") || (l == "C") || (l == "(-)")) {
            f = 2;  // "action" button
          }
        else try {
            int m = java.lang.Integer.parseInt(b.getLabel());
            if ((m >= 0) && (m < 10)) {
              f = 1;  // "number" button
              }
              else {
                f = 0;
              }
            }
        catch (java.lang.NumberFormatException e) {
                  f = 0;
            }
      return f;
      }

/*Converts the string in the TextField screen to an integer, 
or 0 if not a number*/
    int getValue(TextField s) {
        int f;
        try {
            f = java.lang.Integer.parseInt(s.getText());
/* Can't write to s
            if (java.lang.Math.abs(f) >= java.lang.Integer.MAX_VALUE) {
              s.setText("Too big"); */
            }  
        } catch (java.lang.NumberFormatException e) {
            f = 0;
            }
        return f;
    }


    public boolean handleEvent(Event e) {

// When the user clicks a button...

        if (e.id == Event.ACTION_EVENT)  {
            if (e.target instanceof Button ) {

                String b = ((Button)e.target).getLabel();
// If it was a number key, the digit is appended to the string on the screen

                if (buttonType((Button)e.target) == 1) {
                    if (getValue(screen) == 0) {
                        screen.setText(b);
                      }
                    else {
                        screen.setText(screen.getText() + b);
                       }
                }
                else if (buttonType((Button)e.target) == 2) {
                    if (b == "=") {
                      int m = getValue(modulus);
                      if ((m > 0) && (m < 65536)) {
                        int ans = getValue(screen)%m;
                        if (ans < 0) {
                          ans = ans + m;
                        }
                        screen.setText(java.lang.Integer.toString(ans));
                        /* The integer (base 10) in the screen is reduced mod m */
                      }
                      else {
                        modulus.setText("Error");
                          /* "Error" appears in the modulus textfield */
                      }
                    }
                    else if (b == "C") {
                      screen.setText("0");
                        /* clears the screen */
                    }
                    else if (b == "(-)") {
                      String s = screen.getText();
                      if (s.startsWith("-")) {
                        screen.setText(s.substring(1, s.length()));
                      }
                      else {
                        screen.setText("-" + screen.getText() );
                      }
                    }
                    else if (b == "+") {
                    }
                    else if (b == "-") {
                    }
                    else if (b == "x") {
                    }
                    else if (b == "^") {
                    }

//end insert
                }
          }
          else if (e.target instanceof TextField) {
              screen.setText(screen.getText());
              modulus.setText(modulus.getText());
                /* Can also type into the TextFields */
          }


     //System.out.println(java.lang.Integer.toString(getValue(screen)%12));
               return true;
          }
                       return false;
        }
}
