import java.awt.*;
import java.applet.*;

public class calculator extends Applet{
  TextField display;
  double firstnum = 0;
  char funct=' ';

  // event-styring
  public boolean action(Event evt,Object obj){

    // Hvis events sker ved knapperne s… l†s hvilken
    if (evt.target instanceof Button) {
      char val = ((Button) evt.target).getLabel().charAt(0);
      switch(val){
      case 'C':	firstnum=0;double seknum=0;	/* knap C skal slette alt */
	display.setText("0");
	funct=' ';break;
      case '+':						/* +, -, *, / skal huske operation */
      case '-':
      case '*':
      case '/':	firstnum=Double.valueOf(display.getText()).doubleValue();
	funct=val;
	display.setText("");
	break;
      case '=':	seknum=0;		/* = skal udf°re opration */
	double r=0;
	seknum=Double.valueOf(display.getText()).doubleValue();
	if (funct != ' '){
	  
	  //n…r = efterf°lges af en operation
	  // skal resultatet huskes og operationen huskes
	  switch(funct){			
	  case '+':	r=firstnum + seknum;break;
	  case '-':	r=firstnum - seknum;break;
	  case '*':	r=firstnum * seknum;break;
	  case '/':	r=firstnum / seknum;break;
	  }
	  seknum=r;
	  display.setText(String.valueOf(r));
	}
	break;
	
	// saet default for switch. Paa display'et skal der staa 0,
	// og naar der kommer et tal skal denne slettes mens det naeste
	// tal skal s†ttes efter.
      default:	if (Double.valueOf(display.getText()+"0").doubleValue()==0){
	display.setText("");}
      display.setText(display.getText() + val);
      }
      return true;
    }
    return false;
  }
  
  // interface
  public void init() {
    setLayout(null);
    
    // Hent hoejde og bredde paa applettens parametre udefra.
    int width = Integer.parseInt (getParameter ("width"));
    int height= Integer.parseInt (getParameter ("height"));
    
    //initier tekstfelt, med strengen 0
    display = new TextField("0");
    
    //lav knapper
    Button et = new Button("1"); Button to = new Button("2"); Button tre = new Button("3");
    Button fire = new Button("4"); Button fem = new Button("5"); Button seks = new Button("6");
    Button syv = new Button("7"); Button otte = new Button("8"); Button ni = new Button("9");
    Button nul = new Button("0"); 
    Button plus = new Button("+"); Button minus = new Button("-");
    Button div = new Button("/"); Button mul = new Button("*");
    Button c = new Button("C"); Button comma = new Button (".");
    Button eq = new Button ("=");
    
    add(display);
    add(et);add(to);add(tre);add(fire);add(fem);add(seks);add(syv);add(otte);add(ni);add(nul);
    add(plus);add(minus);add(div);add(mul);add(c);add(comma); add(eq);
    
    //Opstil knapperne paent efter hinanden, med display foerst og knapper nedenunder
    // tallene skal vaere i samme raekkefoelge som ved en lommeregner.
    display.reshape(0,0,width,height / 6);
    syv.reshape(0, height / 6, width / 4, height / 6); otte.reshape(width / 4, height / 6, width / 4, height / 6);
    ni.reshape(2 * width / 4, height / 6, width / 4, height / 6); div.reshape(3 * width/ 4, height / 6, width / 4, height / 6);
    fire.reshape(0, 2*height / 6, width / 4, height / 6);fem.reshape(width / 4, 2*height / 6, width / 4, height / 6);
    seks.reshape(2 * width / 4, 2*height / 6, width / 4, height / 6);
    mul.reshape(3 * width / 4, 2*height / 6, width / 4, height / 6);
    et.reshape(0, 3 * height / 6, width / 4, height / 6);
    to.reshape(width / 4, 3 * height / 6, width / 4, height / 6);
    tre.reshape(2 * width / 4, 3 * height / 6, width / 4, height / 6);
    minus.reshape(3 * width / 4, 3 * height / 6, width / 4, height / 6);
    nul.reshape(0, 4 * height / 6, width / 4, height / 6);
    comma.reshape(width / 4, 4 *  height / 6, width / 4, height / 6);
    eq.reshape(2 * width / 4, 4 * height / 6, width / 4, height / 6);
    plus.reshape(3 * width / 4, 4* height / 6, width / 4, height / 6);
    c.reshape(0, 5 * height / 6, width, height / 6);
  }
}
