import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Programm5 extends Applet implements ActionListener {
  Button[] Zahlen = new Button[10];
  Button Add = new Button("+"), Sub = new Button("-"),
         Mult = new Button("*"), Div = new Button("/"),
         R = new Button("R"), C = new Button("C"),
         Calc = new Button("=");
  TextField Eingabe = new TextField();
  Panel Ziffernblock, Operatoren, Rechenzeichen;
  int ans, ein;
  String Op;

  public Programm5() {
    setLayout(new BorderLayout(5,5));
    setBackground(Color.RED);
    add(BorderLayout.NORTH,Eingabe);
    Ziffernblock = new Panel(new GridLayout(4,3,3,3));
    Ziffernblock.setBackground(Color.YELLOW);
    for(int i=9;i>=0;i--) {
      Zahlen[i] = new Button(""+i);
      Ziffernblock.add(Zahlen[i]);
      Zahlen[i].addActionListener(this);
    }
    Ziffernblock.add(R);
    Ziffernblock.add(C);
    R.addActionListener(this);
    C.addActionListener(this);
    add(BorderLayout.CENTER,Ziffernblock);
    Operatoren = new Panel(new GridLayout(2,1,3,3));
    Operatoren.setBackground(Color.BLUE);
    Rechenzeichen = new Panel(new GridLayout(2,2,3,3));
    Rechenzeichen.setBackground(Color.GREEN);
    Rechenzeichen.add(Add);
    Rechenzeichen.add(Sub);
    Rechenzeichen.add(Mult);
    Rechenzeichen.add(Div);
    Add.addActionListener(this);
    Sub.addActionListener(this);
    Mult.addActionListener(this);
    Div.addActionListener(this);
    Operatoren.add(Rechenzeichen);
    Operatoren.add(Calc);
    Calc.addActionListener(this);
    add(BorderLayout.EAST,Operatoren);
    ans=0;
  }

  public int rechne() {
    if(Op=="+") return ans+ein;
    if(Op=="-") return ans-ein;
    if(Op=="*") return ans*ein;
    if(Op=="/" && ein!=0) return (int)(ans/ein);
    else return ein;
  }

  public void actionPerformed(ActionEvent Ereignis) {
    try { ein=Integer.decode(Eingabe.getText()).intValue(); }
    catch(NumberFormatException Fehler) { ein=0; }
    if(Ereignis.getActionCommand()=="+") {
      ans=rechne();
      Op="+";
      Eingabe.setText("");
    }
    else if(Ereignis.getActionCommand()=="-") {
      ans=rechne();
      Op="-";
      Eingabe.setText("");
    }
    else if(Ereignis.getActionCommand()=="*") {
      ans=rechne();
      Op="*";
      Eingabe.setText("");
    }
    else if(Ereignis.getActionCommand()=="/") {
      ans=rechne();
      Op="/";
      Eingabe.setText("");
    }
    else if(Ereignis.getActionCommand()=="=") {
      ans=rechne();
      Op="=";
      Eingabe.setText(""+ans);
    }
    else if(Ereignis.getActionCommand()=="R") {
      Op="R";
      Eingabe.setText(""+ans);
    }
    else if(Ereignis.getActionCommand()=="C") {
      ans=0;
      Op="C";
      Eingabe.setText("");
    }
    else {
      Eingabe.setText(Eingabe.getText()+Ereignis.getActionCommand());
    }
  }
}

