/*一个简单的计算器程序,能做简单的连续算数运算。
* 总体性能良好,操作简单,功能完整。能满足基本运算要求。
* 但是,在做连续算术运算时有时会出现错误,而且在求平方
* 根是也会出现逻辑错误。在求百分数是当连续求一个数的百
* 分数时结果会有偏差。
* */
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextField;
class Form {
//声明、创建控件
private static JTextField tBoxNum=new JTextField(); //创建文本框用于显示输入的数字和运算结果
private static String sBtnText[]={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="}; //保存按钮上现实的内容
private static Button btnExit=new Button("退出"); //创建退出按钮
private static Button btnBackspace=new Button("Backspace"); //创建Backspace按钮
private static Button btnClear=new Button("C"); //创建清零按钮
public Form()
{
InitForm();
}
public void Form_Action()
{
Button_Click();
}
private static Frame frame; //声明窗体控件
private static Button btnNum[]; //声明数字和运算符按钮
private void InitForm()
{
//创建窗体
frame=new Frame("计算器");
frame.setBackground(Color.lightGray); //设置窗体背景颜色
frame.setForeground(Color.blue); //设置窗体前景颜色
//创建控件组pbtnNum
Panel pbtnNum=new Panel(); //创建数字和运算符按钮组
pbtnNum.setLayout(new GridLayout(4,6,4,4)); //指定为网格布局
//将数字和运算符按钮添加到pbtnNum组中
btnNum=new Button[sBtnText.length]; //创建数字和运算符按钮
for(int i=0;i<sBtnText.length;++i)
{
btnNum[i]=new Button(sBtnText[i]);
btnNum[i].setSize(60, 70);
pbtnNum.add(btnNum[i]);
}
//创建控件组pBMark
Panel pBMark=new Panel(); //创建操作按钮组
pBMark.setLayout(new GridLayout(1,1,9,4));
//将操作按钮添加到pBMark组中
pBMark.add(btnExit);
pBMark.add(btnBackspace);
pBMark.add(btnClear);
//添加窗体关闭事件
frame.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
frame.setResizable(false); //设置窗体尺寸不可改变
tBoxNum.setHorizontalAlignment(JTextField.RIGHT); //设置文本框数字右显示
frame.add(tBoxNum,BorderLayout.NORTH); //将文本框添加到窗体
frame.add(pBMark,BorderLayout.CENTER); //将pBMark组添加到窗体
frame.add(pbtnNum,BorderLayout.SOUTH); //经pbtnNum组添加到窗体
frame.pack(); //将窗体组包
frame.setVisible(true);
frame.setLayout(new BorderLayout(5,12));
}
private void Button_Click()
{
btnNum[0].addActionListener(new ImplementActionToNum()); //7
btnNum[1].addActionListener(new ImplementActionToNum()); //8
btnNum[2].addActionListener(new ImplementActionToNum()); //9
btnNum[3].addActionListener(new ImplementActionToOperator()); //除
btnNum[4].addActionListener(new ImplementActionToOperatorSqrt()); //sqrt
btnNum[5].addActionListener(new ImplementActionToNum()); //4
btnNum[6].addActionListener(new ImplementActionToNum()); //5
btnNum[7].addActionListener(new ImplementActionToNum()); //6
btnNum[8].addActionListener(new ImplementActionToOperator()); //*
btnNum[9].addActionListener(new ImplementActionToOperatorPercent()); // %
btnNum[10].addActionListener(new ImplementActionToNum()); //1
btnNum[11].addActionListener(new ImplementActionToNum()); //2
btnNum[12].addActionListener(new ImplementActionToNum()); //3
btnNum[13].addActionListener(new ImplementActionToOperator()); //-
btnNum[14].addActionListener(new ImplementActionToOperatorMultiplicative_inverse()); //1/x
btnNum[15].addActionListener(new ImplementActionToNum()); // 0
btnNum[16].addActionListener(new ImplementActionToOperatorNegative_number()); //负数
btnNum[17].addActionListener(new ImplementActionToOperatorPoint()); //小数点
btnNum[18].addActionListener(new ImplementActionToOperator()); //+
btnNum[19].addActionListener(new ImplementActionToOperatorEqual()); //=
btnClear.addActionListener(new ImplementActionToClear());
btnBackspace.addActionListener(new ImplementActionToBackspace());
btnExit.addActionListener(new ImplementActionToExit());
}
private static double sq;
private static int iRecordClickOfbtnEqual=1, iControlOperator=0; //iRecordClickOfbtnEqual用于记录单击“=”事件,为零时表示单击了“=”. iControlOperator用于控制运算符
//输入数字事件
static class ImplementActionToNum implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(0==iRecordClickOfbtnEqual)
{
tBoxNum.setText("");
iControlOperator=0;
}
iRecordClickOfbtnEqual=1;
iControlOperator=0;
if(e.getActionCommand().equals("0") && (tBoxNum.getText().equals("") || tBoxNum.getText().equals(""))) //0的处理
{
sq=0;
tBoxNum.setText("0");
}
else
{
if(tBoxNum.getText().equals("0"))
{
tBoxNum.setText(e.getActionCommand());
}
else
{
tBoxNum.setText(tBoxNum.getText()+e.getActionCommand());
}
}
}
}
private static String sRecordCurrentOperator="4",sRecordLast_timeOperate="4";//sRecordCurrentOperator用于记录当前运算符,sRecordLast_timeOperate用于记录上一次的运算
//单击清空文本框事件
static class ImplementActionToClear implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
tBoxNum.setText("0.");
iRecordClickOfbtnEqual=0;
iControlOperator=0;
sum=0;
sRecordCurrentOperator="4";
sRecordLast_timeOperate="4";
}
}
private static double sum=0;
//单击等于 事件
static class ImplementActionToOperatorEqual implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(0==iRecordClickOfbtnEqual)
{
tBoxNum.setText(tBoxNum.getText());
}
else
{
if(tBoxNum.getText().equals("")||tBoxNum.getText().endsWith("0.")||tBoxNum.getText().equals("0.0"))
{
tBoxNum.setText("0.0");
}
else
{
if(false==tBoxNum.getText().equals(""))
{
tBoxNum.setText(tBoxNum.getText());
}
}
if(sRecordCurrentOperator.equals("+"))
{
tBoxNum.setText((sum+Double.parseDouble(tBoxNum.getText()))+"");
}
if(sRecordCurrentOperator.equals("-"))
{
tBoxNum.setText((sum-Double.parseDouble(tBoxNum.getText()))+"");
}
if(sRecordCurrentOperator.equals("*"))
{
tBoxNum.setText((sum*Double.parseDouble(tBoxNum.getText()))+"");
}
if(sRecordCurrentOperator.equals("/"))
{
int i;
for(i=49;i<59;i++) //判断分母是否是0
{
if(tBoxNum.getText().indexOf((char)i)!=-1)
{
tBoxNum.setText((sum / Double.parseDouble(tBoxNum.getText()))+"");
break;
}
}
if(59==i)
{
tBoxNum.setText("分母是0");
}
}
iRecordClickOfbtnEqual=0;
sum=0;
iControlOperator=0;
}
}
}
//单击运算符事件
static class ImplementActionToOperator implements ActionListener
{
//得到运算符
public void actionPerformed(ActionEvent e)
{
if(true==tBoxNum.getText().equals("") || true==tBoxNum.getText().equals("0.") || tBoxNum.getText().equals("0") )
{
}
else
{
if(0==iControlOperator) //iControlOperator用于控制运算符 0表示没有按运算符
{
sRecordCurrentOperator=e.getActionCommand(); //得到当前的运算符
if(sRecordLast_timeOperate.equals("4"))
{
sum+=Double.parseDouble(tBoxNum.getText());
}
if(true==sRecordLast_timeOperate.equals("+"))
{
sum+=Double