回答編集履歴
7
enum Operation 定義名変更
test
CHANGED
@@ -109,8 +109,8 @@
|
|
109
109
|
}
|
110
110
|
|
111
111
|
enum Operation {
|
112
|
-
|
112
|
+
ADD("+") { BigDecimal execute(BigDecimal a, BigDecimal b) { return a.add(b); } },
|
113
|
-
|
113
|
+
SUB("-") { BigDecimal execute(BigDecimal a, BigDecimal b) { return a.subtract(b); } },
|
114
114
|
MUL("*") { BigDecimal execute(BigDecimal a, BigDecimal b) { return a.multiply(b); } },
|
115
115
|
DIV("/") { BigDecimal execute(BigDecimal a, BigDecimal b) { return a.divide(b); } }; //ゼロ割 無視中
|
116
116
|
|
@@ -164,12 +164,12 @@
|
|
164
164
|
addButton("1", concatListener);
|
165
165
|
addButton("2", concatListener);
|
166
166
|
addButton("3", concatListener);
|
167
|
-
addButton(Operation.
|
167
|
+
addButton(Operation.SUB, calculator);
|
168
168
|
|
169
169
|
addButton("0", concatListener);
|
170
170
|
addButton(".", concatListener);
|
171
171
|
addButton("=", e -> calculator.calculation());
|
172
|
-
addButton(Operation.
|
172
|
+
addButton(Operation.ADD, calculator);
|
173
173
|
}
|
174
174
|
|
175
175
|
private void addButton(String text, ActionListener listener) {
|
6
修正
test
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
>3. 演算子や小数点を連続で押せないように(押しても反応しないように?)したい。
|
9
9
|
|
10
10
|
物理的(?)には、最後に押したボタンが何か(演算子かそれ以外か)を保存し、演算子を押した時にその直前に押したボタンも演算子だったら、今押されたボタンは無視する…ようにする感じでしょうか。
|
11
|
-
ただ、演算子の連続入力は、演算子の押し間違いの修正の機能として活用できますので、修正の方向を検討する
|
11
|
+
ただ、演算子の連続入力は、演算子の押し間違いの修正の機能として活用できますので、修正の方向を検討する余地があるかもしれません。
|
12
12
|
小数点は、現在の表示に既に小数点があったら無視するのが簡単に思います。
|
13
13
|
|
14
14
|
>4.計算結果が〇.0の時に小数点を表示させないようにしたい。
|
5
追加
test
CHANGED
@@ -20,6 +20,11 @@
|
|
20
20
|
|
21
21
|
Swing にしたり BigDecimal にしたり enum とかしていたら大分変わってしまいました。
|
22
22
|
役に立たないかも知れませんが、とりあえず動作を見るような参考程度ということで。
|
23
|
+
|
24
|
+
確認の為、ディスプレイ右上に実行する演算子を表示するようにしています。
|
25
|
+
演算子の連続入力時は最後の演算子を実行するようにしました。
|
26
|
+
1+2+3+・・・= と連続出来ます。また、1+2= で 3 の表示後に +4= と入力で 7 と計算するようにしています。
|
27
|
+
(簡単に確認しているだけですので、バグはあるかもしれません。)
|
23
28
|
|
24
29
|
Calc.java
|
25
30
|
```java
|
4
コード追加
test
CHANGED
@@ -15,3 +15,168 @@
|
|
15
15
|
|
16
16
|
double でやるならば、文字列化して小数があったら右から一文字ずつ"0"以外が出るまで"0"を消すというような文字レベルの操作をすることになりそうです。
|
17
17
|
BigDecimal を用いるなら stripTrailingZeros() というメソッドがやってくれます。
|
18
|
+
|
19
|
+
---
|
20
|
+
|
21
|
+
Swing にしたり BigDecimal にしたり enum とかしていたら大分変わってしまいました。
|
22
|
+
役に立たないかも知れませんが、とりあえず動作を見るような参考程度ということで。
|
23
|
+
|
24
|
+
Calc.java
|
25
|
+
```java
|
26
|
+
import java.awt.*;
|
27
|
+
import java.awt.event.ActionListener;
|
28
|
+
import java.math.BigDecimal;
|
29
|
+
|
30
|
+
import javax.swing.*;
|
31
|
+
|
32
|
+
public class Calc extends JFrame {
|
33
|
+
public static void main(String[] args) {
|
34
|
+
SwingUtilities.invokeLater(() -> new Calc().setVisible(true));
|
35
|
+
}
|
36
|
+
|
37
|
+
Calc() {
|
38
|
+
super("Calc");
|
39
|
+
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
40
|
+
|
41
|
+
DisplayPanel display = new DisplayPanel();
|
42
|
+
add(display, BorderLayout.NORTH);
|
43
|
+
|
44
|
+
Calculator calculator = new Calculator(display);
|
45
|
+
|
46
|
+
KeyPanel keyPanel = new KeyPanel(display, calculator);
|
47
|
+
add(keyPanel, BorderLayout.CENTER);
|
48
|
+
|
49
|
+
pack();
|
50
|
+
setLocationRelativeTo(null);
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
class DisplayPanel extends JPanel {
|
55
|
+
private JLabel operationIndicator;
|
56
|
+
private JLabel mainDisplay;
|
57
|
+
private String input = "";
|
58
|
+
private Operation op;
|
59
|
+
|
60
|
+
DisplayPanel() {
|
61
|
+
super(new BorderLayout());
|
62
|
+
|
63
|
+
operationIndicator = new JLabel(" ");
|
64
|
+
operationIndicator.setHorizontalAlignment(SwingConstants.RIGHT);
|
65
|
+
operationIndicator.setFont(new Font("Courier", Font.BOLD, 10));
|
66
|
+
add(operationIndicator, BorderLayout.NORTH);
|
67
|
+
|
68
|
+
mainDisplay = new JLabel("0");
|
69
|
+
mainDisplay.setHorizontalAlignment(SwingConstants.RIGHT);
|
70
|
+
mainDisplay.setPreferredSize(new Dimension(50, 30));
|
71
|
+
mainDisplay.setFont(new Font("Courier", Font.BOLD, 15));
|
72
|
+
add(mainDisplay, BorderLayout.CENTER);
|
73
|
+
}
|
74
|
+
|
75
|
+
void setText(String value) {
|
76
|
+
mainDisplay.setText(value);
|
77
|
+
input = "";
|
78
|
+
}
|
79
|
+
|
80
|
+
void concat(String c) {
|
81
|
+
if(c.equals(".")) {
|
82
|
+
if(input.contains(".")) return; //多重小数点 ガード
|
83
|
+
if(input.isEmpty()) input = "0"; //"." だけの入力は "0." とする
|
84
|
+
} else if(input.equals("0")) {
|
85
|
+
input = ""; //ZERO ガード
|
86
|
+
}
|
87
|
+
input += c;
|
88
|
+
mainDisplay.setText(input);
|
89
|
+
}
|
90
|
+
|
91
|
+
String getText() {
|
92
|
+
return mainDisplay.getText();
|
93
|
+
}
|
94
|
+
|
95
|
+
void setOperation(Operation op) {
|
96
|
+
this.op = op;
|
97
|
+
operationIndicator.setText(op == null ? " " : op.text);
|
98
|
+
input = "";
|
99
|
+
}
|
100
|
+
|
101
|
+
Operation getOperation() {
|
102
|
+
return op;
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
enum Operation {
|
107
|
+
PLUS("+") { BigDecimal execute(BigDecimal a, BigDecimal b) { return a.add(b); } },
|
108
|
+
MINUS("-") { BigDecimal execute(BigDecimal a, BigDecimal b) { return a.subtract(b); } },
|
109
|
+
MUL("*") { BigDecimal execute(BigDecimal a, BigDecimal b) { return a.multiply(b); } },
|
110
|
+
DIV("/") { BigDecimal execute(BigDecimal a, BigDecimal b) { return a.divide(b); } }; //ゼロ割 無視中
|
111
|
+
|
112
|
+
final String text;
|
113
|
+
Operation(String text) {
|
114
|
+
this.text = text;
|
115
|
+
}
|
116
|
+
|
117
|
+
abstract BigDecimal execute(BigDecimal a, BigDecimal b);
|
118
|
+
}
|
119
|
+
|
120
|
+
class Calculator {
|
121
|
+
private DisplayPanel display;
|
122
|
+
private BigDecimal a;
|
123
|
+
|
124
|
+
Calculator(DisplayPanel display) {
|
125
|
+
this.display = display;
|
126
|
+
}
|
127
|
+
|
128
|
+
void setOperation(Operation operation) {
|
129
|
+
if(display.getOperation() != null) calculation(); //演算子の優先順位は無視中
|
130
|
+
a = new BigDecimal(display.getText());
|
131
|
+
display.setOperation(operation);
|
132
|
+
}
|
133
|
+
|
134
|
+
void calculation() {
|
135
|
+
BigDecimal b = new BigDecimal(display.getText());
|
136
|
+
Operation op = display.getOperation();
|
137
|
+
display.setText((op == null ? b : op.execute(a, b)).stripTrailingZeros().toPlainString()); //余計な "0" は削除
|
138
|
+
display.setOperation(null);
|
139
|
+
}
|
140
|
+
}
|
141
|
+
|
142
|
+
class KeyPanel extends JPanel {
|
143
|
+
KeyPanel(DisplayPanel display, Calculator calculator) {
|
144
|
+
super(new GridLayout(4, 4));
|
145
|
+
setFont(new Font("Courier", Font.BOLD, 20));
|
146
|
+
|
147
|
+
ActionListener concatListener = e -> display.concat(e.getActionCommand());
|
148
|
+
|
149
|
+
addButton("7", concatListener);
|
150
|
+
addButton("8", concatListener);
|
151
|
+
addButton("9", concatListener);
|
152
|
+
addButton(Operation.DIV, calculator);
|
153
|
+
|
154
|
+
addButton("4", concatListener);
|
155
|
+
addButton("5", concatListener);
|
156
|
+
addButton("6", concatListener);
|
157
|
+
addButton(Operation.MUL, calculator);
|
158
|
+
|
159
|
+
addButton("1", concatListener);
|
160
|
+
addButton("2", concatListener);
|
161
|
+
addButton("3", concatListener);
|
162
|
+
addButton(Operation.MINUS, calculator);
|
163
|
+
|
164
|
+
addButton("0", concatListener);
|
165
|
+
addButton(".", concatListener);
|
166
|
+
addButton("=", e -> calculator.calculation());
|
167
|
+
addButton(Operation.PLUS, calculator);
|
168
|
+
}
|
169
|
+
|
170
|
+
private void addButton(String text, ActionListener listener) {
|
171
|
+
JButton b = new JButton(text);
|
172
|
+
b.addActionListener(listener);
|
173
|
+
add(b);
|
174
|
+
}
|
175
|
+
|
176
|
+
private void addButton(Operation op, Calculator calculator) {
|
177
|
+
JButton b = new JButton(op.text);
|
178
|
+
b.addActionListener(e -> calculator.setOperation(op));
|
179
|
+
add(b);
|
180
|
+
}
|
181
|
+
}
|
182
|
+
```
|
3
追加
test
CHANGED
@@ -8,6 +8,7 @@
|
|
8
8
|
>3. 演算子や小数点を連続で押せないように(押しても反応しないように?)したい。
|
9
9
|
|
10
10
|
物理的(?)には、最後に押したボタンが何か(演算子かそれ以外か)を保存し、演算子を押した時にその直前に押したボタンも演算子だったら、今押されたボタンは無視する…ようにする感じでしょうか。
|
11
|
+
ただ、演算子の連続入力は、演算子の押し間違いの修正の機能として活用できますので、修正の方向を検討するに値するかもしれません。
|
11
12
|
小数点は、現在の表示に既に小数点があったら無視するのが簡単に思います。
|
12
13
|
|
13
14
|
>4.計算結果が〇.0の時に小数点を表示させないようにしたい。
|
2
追記
test
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
※ご提示のコードの動作を完全には把握しておりません。
|
2
|
+
|
1
3
|
>1.最初に表示される0を、数字を入力すると消えるようにしたい。
|
2
4
|
>2.最初に0を続けて入力出来ないようにしたい。
|
3
5
|
|
1
追加
test
CHANGED
@@ -6,6 +6,7 @@
|
|
6
6
|
>3. 演算子や小数点を連続で押せないように(押しても反応しないように?)したい。
|
7
7
|
|
8
8
|
物理的(?)には、最後に押したボタンが何か(演算子かそれ以外か)を保存し、演算子を押した時にその直前に押したボタンも演算子だったら、今押されたボタンは無視する…ようにする感じでしょうか。
|
9
|
+
小数点は、現在の表示に既に小数点があったら無視するのが簡単に思います。
|
9
10
|
|
10
11
|
>4.計算結果が〇.0の時に小数点を表示させないようにしたい。
|
11
12
|
|