github项目地址
该计算器用java实现
更新:现已支持多位数的输入(利用正则表达式提取输入的数值,再压入数值栈中)
GUI用swing实现,可识别括号优先级是用了双栈,一个栈存取操作符,一个栈存取数据
界面巨丑,待我学到更多后来更新此计算器。
主要是拿来练手的(这是第一个自己用代码实现GUI的小程序!!!)
主界面:

CalculatorFrame.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/*计算器框架*/
public class CalculatorFrame {

private JFrame frame;
private JTextField number;
private JPanel panel;
private static String expression =""; //输入的数据转划为字符串

public CalculatorFrame(){
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setLocationRelativeTo(null);

number = new JTextField();
number.setEditable(false);
frame.add(number,BorderLayout.NORTH);

panel = new JPanel();
panel.setLayout(new GridLayout(6,3));
frame.add(panel,BorderLayout.CENTER);


for(int i =1;i<10;i++){
addButton(String.valueOf(i));
}

addButton("+");
addButton("0");
addButton("-");
addButton("*");
addButton("C"); //按钮C的作用是清空整个输入
addButton("/");
addButton("(");
addButton(")");

JButton equal = new JButton("=");
equal.setActionCommand("=");
equal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
number.setText(""+new Calculate().calculating(expression)); //显示计算得到的结果
expression = ""; // 计算后整个输入清空
}
});
panel.add(equal);
frame.add(panel);
frame.setResizable(false); //设置窗口大小不可改变
frame.setVisible(true);
}

/*当不为输入不为“C”和“=”时,将输入转化为字符串,为“C”时清空输入*/
private void addButton(String name ){
JButton temp = new JButton(name);
temp.setActionCommand(name);
if(!"C".equals(name))
temp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
expression += name;
number.setText(expression);

}
});
else
temp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
expression="";
}
});
panel.add(temp);
}
}

Calculate.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/*计算器框架*/
public class CalculatorFrame {

private JFrame frame;
private JTextField number;
private JPanel panel;
private static String expression =""; //输入的数据转划为字符串

public CalculatorFrame(){
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setLocationRelativeTo(null);

number = new JTextField();
number.setEditable(false);
frame.add(number,BorderLayout.NORTH);

panel = new JPanel();
panel.setLayout(new GridLayout(6,3));
frame.add(panel,BorderLayout.CENTER);


for(int i =1;i<10;i++){
addButton(String.valueOf(i));
}

addButton("+");
addButton("0");
addButton("-");
addButton("*");
addButton("C"); //按钮C的作用是清空整个输入
addButton("/");
addButton("(");
addButton(")");

JButton equal = new JButton("=");
equal.setActionCommand("=");
equal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
number.setText(""+new Calculate().calculating(expression)); //显示计算得到的结果
expression = ""; // 计算后整个输入清空
}
});
panel.add(equal);
frame.add(panel);
frame.setResizable(false); //设置窗口大小不可改变
frame.setVisible(true);
}

/*当不为输入不为“C”和“=”时,将输入转化为字符串,为“C”时清空输入*/
private void addButton(String name ){
JButton temp = new JButton(name);
temp.setActionCommand(name);
if(!"C".equals(name))
temp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
expression += name;
number.setText(expression);

}
});
else
temp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
expression="";
}
});
panel.add(temp);
}
}

run.java

1
2
3
4
5
6
7
8
9
10
11
12
import java.awt.*;

public class run {
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new CalculatorFrame();
}
});
}
}