菠萝菠萝蜜 – 坑爹的作业从大一到大三一直TM是这个

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator implements ActionListener{

TextField CalculatorTextField = new TextField(30);
public static Frame Calculator = new Frame(“Calculator Test Windows”);
String[] NumButtonArrays = new String[12];
Panel PanelTop = new Panel();
Panel PanelButtom = new Panel();
Panel PanelOperation = new Panel();
GridLayout NumButtonLayout = new GridLayout(4,4);
GridLayout OperButtonLayout = new GridLayout(1,4);
Button[] NumButtonObjArrays = new Button[12];
Button[] OperObjButtonArrays = new Button[4];
String pre = “0”;
String tmp = “0”;
String next = “0”;

public Calculator()
{

PanelButtom.setLayout(NumButtonLayout);
PanelOperation.setLayout(OperButtonLayout);

PanelTop.add(CalculatorTextField);

for(int i = 0;i < 10;i ++)
{
NumButtonArrays[i] =String.valueOf(i);
}

NumButtonArrays[10] = “.”;
NumButtonArrays[11] = “=”;

String[] OperButtonArrays = {“+”,”-“,”*”,”/”};

for(int i = 0;i < 12;i ++)
{
NumButtonObjArrays[i] = new Button(NumButtonArrays[i]);
PanelButtom.add(NumButtonObjArrays[i]);
NumButtonObjArrays[i].addActionListener(this);
}

for(int i = 0;i < 4;i ++)
{
OperObjButtonArrays[i] = new Button(OperButtonArrays[i]);
PanelOperation.add(OperObjButtonArrays[i]);
OperObjButtonArrays[i].addActionListener(this);
}

Calculator.add(PanelTop,BorderLayout.NORTH);
Calculator.add(PanelButtom,BorderLayout.CENTER);
Calculator.add(PanelOperation,BorderLayout.SOUTH);

Calculator.pack();
Calculator.setVisible(true);

}

public void actionPerformed(ActionEvent e) {

String cmd = e.getActionCommand();
if(cmd.equals(“.”))
{
if(“.”.indexOf(pre) == -1)
pre = pre + “.”;
else pre = “Fuck You”;
}
else
{
if(cmd.equals(“+”) || cmd.equals(“-“) || cmd.equals(“*”) || cmd.equals(“/”))
{
next = pre;
OperatorAchtion(cmd);
pre = cmd;
next = cmd;
}
else
{
if(cmd.equals(“=”))
{
OperatorAchtion(next);
pre = tmp;
next = cmd;
}
else
{
if(pre.equals(“+”) || pre.equals(“-“) || pre.equals(“*”) || pre.equals(“/”) || pre.equals(“0”))
{
pre = cmd;
}
else pre = pre + cmd;
}
}
}

this.CalculatorTextField.setText(pre);
}

public void OperatorAchtion(String Operator)
{
if(“+”.equals(Operator))
tmp = String.valueOf(Double.parseDouble(tmp) + Double.parseDouble(pre));
else if(“-“.equals(Operator))
tmp = String.valueOf(Double.parseDouble(tmp) – Double.parseDouble(pre));
else if(“*”.equals(Operator))
tmp = String.valueOf(Double.parseDouble(tmp) * Double.parseDouble(pre));
else if(“/”.equals(Operator))
tmp = String.valueOf(Double.parseDouble(tmp) / Double.parseDouble(pre));
else;

}

public static void main(String args[]){
new Calculator();
}
}