/***************************************
* 计算器程序
***************************************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculationer extends JFrame implements ActionListener {
// 窗口组件
private JPanel panel = new JPanel();
private JTextField text = new JTextField();
private JButton[] number = new JButton[10];
private JButton bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8;
String string = "";
// "画"窗口
public Calculationer() {
setTitle("Calculationer");
setResizable(false);
for (int i = 0; i < 10; i++) {
number[i] = new JButton("" + i);
number[i].addActionListener(this);
}
text.setEditable(false);
bt1 = new JButton(".");
bt1.addActionListener(this);
bt2 = new JButton("=");
bt2.addActionListener(this);
bt3 = new JButton("+");
bt3.addActionListener(this);
bt4 = new JButton("-");
bt4.addActionListener(this);
bt5 = new JButton("*");
bt5.addActionListener(this);
bt6 = new JButton("/");
bt6.addActionListener(this);
bt7 = new JButton("√");
bt7.addActionListener(this);
bt8 = new JButton("C");
bt8.addActionListener(this);
panel.setLayout(new GridLayout(6, 3, 7, 7));
for (int i = 7; i <= 9; i++)
panel.add(number[i]);
for (int i = 4; i <= 6; i++)
panel.add(number[i]);
for (int i = 1; i <= 3; i++)
panel.add(number[i]);
panel.add(number[0]);
panel.add(bt1);
panel.add(bt7);
panel.add(bt4);
panel.add(bt5);
panel.add(bt2);
panel.add(bt3);
panel.add(bt6);
panel.add(bt8);
BorderLayout blayout;
setLayout(blayout = new BorderLayout());
add(text, blayout.NORTH);
add(panel, blayout.CENTER);
}
// 实现接口,逻辑判断器,为运算器提供详细信息(思维中,"记忆"很重要)
public void actionPerformed(ActionEvent e) {
JButton bevent = (JButton) e.getSource();// 获得按钮事件
// 复制为全局性按钮
double result = 0;
string += bevent.getText();
text.setText(string);
if (string.endsWith("C")) {
string = "";
result = 0;
text.setText(String.valueOf(result));
}
if (string.endsWith("=")) { /* "="号触发运算 */
char[] ch = string.toCharArray();
char[] chi;// 运算符数组
String[] str;// 数值数组(以字符串形式储存)
int n = 0, ci = 0;// 辅助常量
// 确定数据个数 :n+1
try {
for (int yi = 0; yi < ch.length; yi++) {
if (ch[yi] == '*' || ch[yi] == '/' || ch[yi] == '+'
|| ch[yi] == '-')
n++;
}
// 确定符号个数 :ci
for (int yi = 0; yi < ch.length; yi++) {
if (ch[yi] == '*' || ch[yi] == '+' || ch[yi] == '/'
|| ch[yi] == '-' || ch[yi] == '√' || ch[yi] == '=')
ci++;
}
// 初始化数值数组和运算符数组
chi = new char[ci];
str = new String[n + 1];
for (int hi = 0; hi < 1 + n; hi++) {
str[hi] = "";
}
// 遍历识别字符串并提取数据和运算符
for (int cii = 0, ni1 = 0, ti = 0; ti < ch.length; ti++) {
if (ch[ti] != '*' && ch[ti] != '-' && ch[ti] != '/'
&& ch[ti] != '+' && ch[ti] != '=' && ch[ti] != '√')
str[ni1] += ch[ti];// 提取数据
else {
chi[cii] = ch[ti];
cii++;// 提取运算符
if (ch[ti - 1] != '√')
ni1++;//
}
}// System.out.println(chi);
double[] number = new double[n + 1];
for (int pi = 0; pi <= n; pi++)
number[pi] = 0;
for (int oi = 0; oi <= n; oi++)
number[oi] = Double.valueOf(str[oi]);
// 运算
result = number[0];
for (int ni = 0, ri = 0; ri < ci; ri++) {
ni++;
if (chi[ri] == '*')
result *= number[ni];
else if (chi[ri] == '/')
result /= number[ni];
else if (chi[ri] == '+')
result += number[ni];
else if (chi[ri] == '-')
result -= number[ni];
else if (chi[ri] == '√') {
ni--;
result = Math.sqrt(result);
} else if (chi[ri] == '=')
break;
}
text.setText(string);
text.setText(String.valueOf(result));
result = 0;
string = "";
} catch (Exception el) {
text.setText("You input error!");
result = 0;
string = "";
}
}
}
// 主方法
public static void main(String srgs[]) {
Calculationer calculationer = new Calculationer();
calculationer.setSize(300, 250);
calculationer.setVisible(true);
calculationer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calculationer.setLocation(500, 200);
}
}
评论0