[ activity_main.xml ]
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290 |
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mijinpark.calculator.MainActivity">
<!-- 창 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/invisivleView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="0"/>
</LinearLayout>
<!-- 버튼 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:orientation="horizontal">
<!-- 숫자부분 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<!-- 첫째줄 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/back"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="←"/>
<Button
android:id="@+id/CE"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="CE"/>
<Button
android:id="@+id/C"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C"/>
</LinearLayout>
<!-- 둘째줄 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/seven"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"/>
<Button
android:id="@+id/eight"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"/>
<Button
android:id="@+id/nine"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9"/>
</LinearLayout>
<!-- 셋째줄 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/four"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"/>
<Button
android:id="@+id/five"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"/>
<Button
android:id="@+id/six"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6"/>
</LinearLayout>
<!-- 넷째줄 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/one"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"/>
<Button
android:id="@+id/two"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"/>
<Button
android:id="@+id/three"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"/>
</LinearLayout>
<!-- 다섯째줄 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/zero"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="0"/>
<Button
android:id="@+id/point"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="."/>
</LinearLayout>
</LinearLayout>
<!-- 연산부분 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">
<!-- 첫째줄 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/reverse"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="±"/>
<Button
android:id="@+id/root"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="√"/>
</LinearLayout>
<!-- 둘째줄 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/div"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="/"/>
<Button
android:id="@+id/per"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="%"/>
</LinearLayout>
<!-- 셋째줄 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/mul"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="*"/>
<Button
android:id="@+id/reciprocal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1/x"/>
</LinearLayout>
<!-- 넷째줄 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/minus"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="-"/>
<Button
android:id="@+id/plus"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="+"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/equal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="="/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout> |
cs |
[ Calculator.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 |
package com.example.mijinpark.calculator;
/**
* Created by MiJin Park on 2016-05-28.
*/
public class Calculator {
double number1;
double number2;
String op;
double tempResult;
public Calculator(){
//의미없는 숫자로 초기화
number1 = -0.023478;
number2 = -0.023478;
op = "";
}
public void setNumber1(double number1){
this.number1 = number1;
}
public double getNumber1(){
return number1;
}
public void setNumber2(double number2){
this.number2 = number2;
}
public double getNumber2(){
return number2;
}
public void setOp(String op){
this.op = op;
}
public String getOp(){
return op;
}
// 계산
public double calc() {
try {
if (op.equals("+")) {
tempResult = number1 + number2;
} else if (op.equals("-")) {
tempResult = number1 - number2;
} else if (op.equals("*")) {
tempResult = number1 * number2;
} else if (op.equals("/")) {
try {
tempResult = number1 / number2;
} catch (ArithmeticException e) {
e.printStackTrace();
}
}else{
tempResult = 0;
}
return tempResult;
}catch (Exception e) {
e.printStackTrace();
}
return tempResult;
}
} |
cs |
[ MainActivity.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 |
package com.example.mijinpark.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView view;
TextView viewInvisible;
Button btnBack;
Button btnCE;
Button btnC;
Button btnRev;
Button btnRoot;
Button btnDiv;
Button btnPer;
Button btnMul;
Button btnRec;
Button btnMinus;
Button btnPlus;
Button btnEqu;
Button btnPoint;
Button btn0;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
Button btn6;
Button btn7;
Button btn8;
Button btn9;
Calculator cal = new Calculator();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (TextView) findViewById(R.id.view);
viewInvisible = (TextView) findViewById(R.id.invisivleView);
btnBack = (Button) findViewById(R.id.back);
btnCE = (Button) findViewById(R.id.CE);
btnC = (Button) findViewById(R.id.C);
btnRev = (Button) findViewById(R.id.reverse);
btnRoot = (Button) findViewById(R.id.root);
btnDiv = (Button) findViewById(R.id.div);
btnPer = (Button) findViewById(R.id.per);
btnMul = (Button) findViewById(R.id.mul);
btnRec = (Button) findViewById(R.id.reciprocal);
btnMinus = (Button) findViewById(R.id.minus);
btnPlus = (Button) findViewById(R.id.plus);
btnEqu = (Button) findViewById(R.id.equal);
btnPoint = (Button) findViewById(R.id.point);
btn0 = (Button) findViewById(R.id.zero);
btn1 = (Button) findViewById(R.id.one);
btn2 = (Button) findViewById(R.id.two);
btn3 = (Button) findViewById(R.id.three);
btn4 = (Button) findViewById(R.id.four);
btn5 = (Button) findViewById(R.id.five);
btn6 = (Button) findViewById(R.id.six);
btn7 = (Button) findViewById(R.id.seven);
btn8 = (Button) findViewById(R.id.eight);
btn9 = (Button) findViewById(R.id.nine);
/**************숫자 키**************/
btn0.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setNumber("0");}
});
btn1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setNumber("1");}
});
btn2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setNumber("2");}
});
btn3.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setNumber("3");}
});
btn4.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setNumber("4");}
});
btn5.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setNumber("5");}
});
btn6.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setNumber("6");}
});
btn7.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setNumber("7");}
});
btn8.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setNumber("8");}
});
btn9.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setNumber("9");}
});
btnPoint.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
viewInvisible.setText(viewInvisible.getText().toString() + ".");
}
});
/**************사칙연산 키**************/
btnMinus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.setText(view.getText()+viewInvisible.getText().toString()+"-");
setCal("-");
}
});
btnPlus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.setText(view.getText()+viewInvisible.getText().toString()+"+");
setCal("+");
}
});
btnDiv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.setText(view.getText()+viewInvisible.getText().toString()+"/");
setCal("/");
}
});
btnMul.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.setText(view.getText()+viewInvisible.getText().toString()+"*");
setCal("*");
}
});
/**************±,= 연산 키**************/
btnRev.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
viewInvisible.setText(((-1)*Double.parseDouble(viewInvisible.getText().toString()))+"");
}
});
btnEqu.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
cal.setNumber2(Double.parseDouble(viewInvisible.getText().toString()));
view.setText("");
viewInvisible.setText(cal.calc()+"");
cal.setNumber1(-0.023478);
cal.setNumber2(-0.023478);
cal.setOp("");
}
});
/**************기능 키**************/
btnBack.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
int len = viewInvisible.getText().length();
if(len>0){
viewInvisible.setText(viewInvisible.getText().toString().substring(0,len-1));
}
}
});
//C는 완전히 초기화시키는 버튼
btnC.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
view.setText("");
viewInvisible.setText("0");
cal.setNumber1(-0.023478);
cal.setNumber2(-0.023478);
cal.setOp("");
}
});
//CE는 현재 쓰고있는 값 (viewInvisible에 있는 값)만 초기화하는 버튼
btnCE.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//view에 값이 없으면 완전 초기화
if(view.getText().toString().equals("")){
cal.setNumber1(-0.023478);
cal.setNumber2(-0.023478);
cal.setOp("");
}
viewInvisible.setText("0");
}
});
}
// 숫자 입력 function
public void setNumber(String i){
if(viewInvisible.getText().equals("0")){
viewInvisible.setText(i);
}else {
viewInvisible.setText(viewInvisible.getText().toString() + i);
}
}
// 계산 function
public void setCal(String op){
if(viewInvisible.getText().equals("")){
int len = view.getText().length();
try{
if(len>0){
view.setText((view.getText().toString().substring(0,len-2))+op);
}
}catch (Exception e){
view.setText(view.getText().toString().substring(0,0)+op);
}
}else{
if (cal.getNumber1()==-0.023478){
try{
cal.setNumber1(Double.parseDouble(viewInvisible.getText().toString()));
}catch (Exception e){
cal.setNumber1(0);
}
}else if(cal.getNumber2()==-0.023478){
try{
cal.setNumber2(Double.parseDouble(viewInvisible.getText().toString()));
}catch (NumberFormatException e){
//여기에 항등원넣으면 됨
if(op.equals("+")||op.equals("-")){
cal.setNumber2(0);
}else if (op.equals("*")||op.equals("/")){
cal.setNumber2(1);
}
}
cal.setNumber1(cal.calc());
cal.setNumber2(-0.023478);
}
}
cal.setOp(op);
viewInvisible.setText("");
}
} |
cs |
실행 결과
- 루트, %, 1/x 은 아직 구현하지 못했습니다..ㅜㅜ
'Android > Android' 카테고리의 다른 글
Android(7)_Push (0) | 2016.06.01 |
---|---|
Android(5)_Intent (0) | 2016.05.31 |
Android(4)_고급Widget (0) | 2016.05.30 |
Android(3)_Layout (0) | 2016.05.27 |
Android(2)_Activity (0) | 2016.05.26 |