[안드로이드 스튜디오 APK] 계산기 Calculator 오픈소스
아래 영상은 밑에 보이는 소스를 응용해서 추가적인 기능을 넣어서 영상을 찍은 것입니다.
기본적인 계산기 소스는 이렇게 짜봤습니다.
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
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="7">
<TextView
android:textColor="#000"
android:textSize="70sp"
android:textAlignment="viewEnd"
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#5d5d5d"
android:orientation="horizontal"
android:weightSum="4">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="4">
<Button
android:textSize="30sp"
android:id="@+id/btn7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5d5d5d"
android:text="7"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:textSize="30sp"
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5d5d5d"
android:text="4"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:textSize="30sp"
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5d5d5d"
android:text="1"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:textSize="30sp"
android:id="@+id/Plus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5d5d5d"
android:text="+"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="4">
<Button
android:textSize="30sp"
android:id="@+id/btn8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5d5d5d"
android:text="8"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:textSize="30sp"
android:id="@+id/btn5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5d5d5d"
android:text="5"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:textSize="30sp"
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5d5d5d"
android:text="2"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:textSize="30sp"
android:id="@+id/btn0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5d5d5d"
android:text="0"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="4">
<Button
android:textSize="30sp"
android:id="@+id/btn9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5d5d5d"
android:text="9"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:textSize="30sp"
android:id="@+id/btn6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5d5d5d"
android:text="6"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:textSize="30sp"
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5d5d5d"
android:text="3"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:textSize="30sp"
android:id="@+id/Minus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5d5d5d"
android:text="-"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#aaaaaa"
android:orientation="vertical"
android:weightSum="4">
<Button
android:textSize="30sp"
android:id="@+id/AC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#e0e0e0"
android:text="←/AC"
android:textStyle="bold" />
<Button
android:textSize="30sp"
android:id="@+id/Multiple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#e0e0e0"
android:text="x"
android:textStyle="bold" />
<Button
android:textSize="30sp"
android:id="@+id/Divide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#e0e0e0"
android:text="÷"
android:textStyle="bold" />
<Button
android:textSize="30sp"
android:id="@+id/equal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#e0e0e0"
android:text="="
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
| cs |
MainActivity.java
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; import java.util.Stack;
import java.util.StringTokenizer; import static com.example.pita.calculator.R.layout.activity_main;
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn1; private Button btn2; private Button btn3; private Button btn4; private Button btn5; private Button btn6; private Button btn7; private Button btn8; private Button btn9; private Button btn0; private Button AC; private Button Plus; private Button Minus; private Button Multiple; private Button Divide; private Button equal; private TextView result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(activity_main); btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(this); btn2 = (Button) findViewById(R.id.btn2); btn2.setOnClickListener(this); btn3 = (Button) findViewById(R.id.btn3); btn3.setOnClickListener(this); btn4 = (Button) findViewById(R.id.btn4); btn4.setOnClickListener(this); btn5 = (Button) findViewById(R.id.btn5); btn5.setOnClickListener(this); btn6 = (Button) findViewById(R.id.btn6); btn6.setOnClickListener(this); btn7 = (Button) findViewById(R.id.btn7); btn7.setOnClickListener(this); btn8 = (Button) findViewById(R.id.btn8); btn8.setOnClickListener(this); btn9 = (Button) findViewById(R.id.btn9); btn9.setOnClickListener(this); btn0 = (Button) findViewById(R.id.btn0); btn0.setOnClickListener(this); AC = (Button) findViewById(R.id.AC); AC.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (result.getText().length() != 0) { result.setText(result.getText().subSequence(0, result.getText().length() - 1)); } } }); AC.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { if (result.getText().length() != 0) { result.setText("0"); } return true; } }); Multiple = (Button) findViewById(R.id.Multiple); Multiple.setOnClickListener(this); Divide = (Button) findViewById(R.id.Divide); Divide.setOnClickListener(this); Plus = (Button) findViewById(R.id.Plus); Plus.setOnClickListener(this); Minus = (Button) findViewById(R.id.Minus); Minus.setOnClickListener(this); equal = (Button) findViewById(R.id.equal); equal.setOnClickListener(this); result = (TextView) findViewById(R.id.result); } @Override public void onClick(View v) { if (v.equals(btn1)) { if (result.getText().length() == 1 && "0".equals(result.getText())) { result.setText("1"); } else { result.setText(result.getText() + "1"); } } else if (v.equals(btn2)) { if (result.getText().length() == 1 && "0".equals(result.getText())) { result.setText("2"); } else { result.setText(result.getText() + "2"); } } else if (v.equals(btn3)) { if (result.getText().length() == 1 && "0".equals(result.getText())) { result.setText("3"); } else { result.setText(result.getText() + "3"); } } else if (v.equals(btn4)) { if (result.getText().length() == 1 && "0".equals(result.getText())) { result.setText("4"); } else { result.setText(result.getText() + "4"); } } else if (v.equals(btn5)) { if (result.getText().length() == 1 && "0".equals(result.getText())) { result.setText("5"); } else { result.setText(result.getText() + "5"); } } else if (v.equals(btn6)) { if (result.getText().length() == 1 && "0".equals(result.getText())) { result.setText("6"); } else { result.setText(result.getText() + "6"); } } else if (v.equals(btn7)) { if (result.getText().length() == 1 && "0".equals(result.getText())) { result.setText("7"); } else { result.setText(result.getText() + "7"); } } else if (v.equals(btn8)) { if (result.getText().length() == 1 && "0".equals(result.getText())) { result.setText("8"); } else { result.setText(result.getText() + "8"); } } else if (v.equals(btn9)) { if (result.getText().length() == 1 && "0".equals(result.getText())) { result.setText("9"); } else { result.setText(result.getText() + "9"); } } else if (v.equals(btn0)) { if (result.getText().length() == 1 && "0".equals(result.getText())) { result.setText("0"); } else { result.setText(result.getText() + "0"); } } else if (v.equals(Divide)) { result.setText(result.getText() + "/"); } else if (v.equals(Plus)) { result.setText(result.getText() + "+"); } else if (v.equals(Minus)) { result.setText(result.getText() + "-"); } else if (v.equals(Multiple)) { result.setText(result.getText() + "*"); } else if (v.equals(equal)) { result.setText(Calc(result.getText().toString())); } } private String Calc(String str) { if (str.indexOf('(') != -1) { int fs = str.indexOf('('); int ls = str.lastIndexOf(')'); String s = Calc(str.substring(fs + 1, ls)); str = str.substring(0, fs) + s + str.substring(ls + 1, str.length()); } int cnt = 0; Stack<Integer> Stk_Num = new Stack<Integer>(); StringTokenizer ST_Num = new StringTokenizer(str, "+-/* "); StringTokenizer ST_Oper = new StringTokenizer(str, "1234567890 "); Stk_Num.push(Integer.parseInt(ST_Num.nextToken())); while (ST_Num.hasMoreTokens()) { char oper = ST_Oper.nextToken().charAt(0); String num = ST_Num.nextToken(); int a; if (oper == '*') { a = Stk_Num.pop(); a *= Integer.parseInt(num); Stk_Num.push(a); } else if (oper == '/') { a = Stk_Num.pop(); a /= Integer.parseInt(num); Stk_Num.push(a); } else if (oper == '+') { Stk_Num.push(Integer.parseInt(num)); } else if (oper == '-') { Stk_Num.push(-1 * (Integer.parseInt(num))); } } while (!Stk_Num.isEmpty()) { cnt += Stk_Num.pop(); } return Integer.toString(cnt); } }
댓글
댓글 쓰기