본문 바로가기
category/android studio 앱 개발

android studio 계산기 #2

by 자운대고라니 2021. 4. 15.
반응형

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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package com.GWdeveloper.calculator;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
 
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "Main_Activity";
 
    private TextView ptResult;
    private TextView ptResult2;
    private String s = null;
    private Button btn[] = new Button[23];
    private ImageView backspace;
    private TextView pttestbox;
    private int bracketcount = 0//괄호 포인트 0이상일때 ( 쓰기 가능 ) 쓰면 1 포인트씩 차감
    private int dotcount = 1//점 포인트 1일때 쓸 수 있음 0일 때 못 씀
    private int i=0;
    private AdView mAdView;
    private String log = "LOG";
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        
        //광고관련
        MobileAds.initialize(thisnew OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });
 
 
        mAdView = findViewById(R.id.banner);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
 
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
                Log.d(log, "finishes loading.");
            }
 
            @Override
            public void onAdFailedToLoad(LoadAdError adError) {
                // Code to be executed when an ad request fails.
                Log.d(log, "request fails.");
            }
 
            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
                Log.d(log, "opens an overlay that, covers the screen.");
            }
 
            @Override
            public void onAdClicked() {
                // Code to be executed when the user clicks on an ad.
                Log.d(log, "user clicks on an ad.");
            }
 
            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
                Log.d(log, "the user has left the app.");
            }
 
            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
                Log.d(log, "user is about to return to the app after tapping on an ad.");
            }
        });
 
        init();
        initListener();
    }
 
    private void init(){
        btn[0]=findViewById(R.id.num_0);
        btn[1]=findViewById(R.id.num_1);
        btn[2]=findViewById(R.id.num_2);
        btn[3]=findViewById(R.id.num_3);
        btn[4]=findViewById(R.id.num_4);
        btn[5]=findViewById(R.id.num_5);
        btn[6]=findViewById(R.id.num_6);
        btn[7]=findViewById(R.id.num_7);
        btn[8]=findViewById(R.id.num_8);
        btn[9]=findViewById(R.id.num_9);
 
        btn[10]=findViewById(R.id.btn_plus);
        btn[11]=findViewById(R.id.btn_minus);
        btn[12]=findViewById(R.id.btn_multiple);
        btn[13]=findViewById(R.id.btn_slush);
        btn[14]=findViewById(R.id.btn_dot);
        btn[15]=findViewById(R.id.btn_per);
        btn[16]=findViewById(R.id.btn_left);
        btn[17]=findViewById(R.id.btn_right);
 
        btn[18]=findViewById(R.id.btn_clear);
        btn[19]=findViewById(R.id.btn_result);
 
        backspace = (ImageView)findViewById(R.id.backspace);
 
        ptResult=findViewById(R.id.et_result); //결과
        ptResult2=findViewById(R.id.et_result2); //연산
        //pttestbox=findViewById(R.id.testbox);
    }
 
 
 
    private void initListener(){
        //pttestbox.setText(ptResult.getText().toString());
        for (int i=0; i<10;i++){
            int I = i;
            btn[i].setOnClickListener((View view)-> {
                char lastchar = 'f';
                if(s!=null) {
                    lastchar = ptResult.getText().toString().charAt(ptResult.getText().toString().length() - 1);
                }
                if (lastchar != ')' || lastchar == 'f') {
                        if (I == 0) {
                            if (s == null && dotcount==1) {
                                ptResult.append("0");
                                ptResult.append(".");
                                dotcount = 0;
                                s += "0.";
                            } else {
                                Button btn = (Button) view;
                                ptResult.append(btn.getText().toString());
 
                                String result3 = ptResult.getText().toString();
                                double a = Double.valueOf(Eval.cal(result3));
                                int b = (int) a;
 
                                if (a == b) {
                                    ptResult2.setText(Integer.toString(b));
                                } else ptResult2.setText(Eval.cal(result3));
 
                                s += btn.getText().toString();
                            }
                        } else {
                            Button btn = (Button) view;
                            ptResult.append(btn.getText().toString());
 
                            String result3 = ptResult.getText().toString();
                            double a = Double.valueOf(Eval.cal(result3));
                            int b = (int) a;
 
                            if (a == b) {
                                ptResult2.setText(Integer.toString(b));
                            } else ptResult2.setText(Eval.cal(result3));
 
                            s += btn.getText().toString();
                            //pttestbox.setText(s);
                        }
                    }
            });
        }
 
        for (int i=10; i<16; i++) {
            int finalI = i;
 
            btn[i].setOnClickListener((View view) -> {
                int I = finalI;
                if(I==14) {
                    if(s!=null){
                        char a = ptResult.getText().toString().charAt(ptResult.getText().toString().length() - 1);
                        if(dotcount == 1) {
                            if(a=='0' || a=='1'|| a=='2'|| a=='3'|| a=='4'|| a=='5'|| a=='6'|| a=='7'|| a=='8'|| a=='9') {
                                Button btn = (Button) view;
                                s = null;
                                ptResult.append(btn.getText().toString());
                                dotcount = 0;
                            }
                        }
                    }
                }
                else {
                    char a = ptResult.getText().toString().charAt(ptResult.getText().toString().length() - 1);
                    if (a == '0' || a == '1' || a == '2' || a == '3' || a == '4' || a == '5' || a == '6' || a == '7' || a == '8' || a == '9' || a == ')') {
                        Button btn = (Button) view;
                        s = null;
                        ptResult.append(btn.getText().toString());
                        //
                        // pttestbox.setText(s);
                        dotcount = 1;
                    }
                }
            });
        }
 
        btn[16].setOnClickListener((View view) -> { //(를 넣었을때
            char bol = 'a';
            if(s!=null) {
                bol = ptResult.getText().toString().charAt(ptResult.getText().toString().length()-1); //식의 맨 마지막 문자를 따와서 연산자 혹은 ( 면 (가 입력되도록 한다.
            }
            if (bol=='+' || bol=='-' || bol=='/' || bol=='*' || bol=='%' || bol=='(' || s==null) {
                bracketcount++;
                ptResult.append("(");
                //pttestbox.setText("123");
                left_right();
            }
        });
        btn[18].setOnClickListener((View view) -> { //clear
            ptResult.setText("");
            ptResult2.setText("");
            s = null;
            bracketcount=0;
            dotcount = 1;
        });
 
        btn[19].setOnClickListener((View view)-> { //result
            if(s != null) {
                ptResult.setText(ptResult2.getText().toString());
                s = ptResult.getText().toString();
 
                //pttestbox.setText(ptResult.getText().toString());
                bracketcount = 0;
                dotcount = 1;
            }
        });
 
        backspace.setOnClickListener((View view)-> {
            if(ptResult.length()>=2) {
                String a = ptResult.getText().toString(); //ptResult 문자열로 받아오기
                String b = a.substring(0, a.length() - 1); //ptResult - 1
                ptResult.setText(b); //ptResult - 1 출력
 
                char f = b.charAt(b.length() - 1); //f는 지우기 전의 문자
                char j = a.charAt(a.length() - 1); //j는 지우는 문자
 
                if (f == '0' || f == '1' || f == '2' || f == '3' || f == '4' || f == '5' || f == '6' || f == '7' || f == '8' || f == '9' || f=='.' || f==')') {
                    if(j=='(') {
                        bracketcount--;
                        if(bracketcount==0) {
                            double c = Double.valueOf(Eval.cal(b));
                            int d = (int) c;
                            if (c == d) {
                                ptResult2.setText(Integer.toString(d));
                            } else ptResult2.setText(Eval.cal(b));
                            //pttestbox.setText(String.valueOf(j));
                        }
                    }
                    else if(j==')') {
                        bracketcount++;
                        if(bracketcount==0) {
                            double c = Double.valueOf(Eval.cal(b));
                            int d = (int) c;
                            if (c == d) {
                                ptResult2.setText(Integer.toString(d));
                            } else ptResult2.setText(Eval.cal(b));
                            //pttestbox.setText(String.valueOf(j));
                        }
                        left_right();
                    }
                    else {
                        if(bracketcount==0) {
                            if(j=='.') dotcount=1;
                            double c = Double.valueOf(Eval.cal(b));
                            int d = (int) c;
                            if (c == d) {
                                ptResult2.setText(Integer.toString(d));
                            } else ptResult2.setText(Eval.cal(b));
                            //pttestbox.setText(String.valueOf(j));
                        }
                    }
                }
 
                else {
                    if(j=='(') {
                        bracketcount--;
                        if(bracketcount==0) {
                            double g = Double.valueOf(Eval.cal(b.substring(0, b.length() - 1)));
                            int h = (int) g;
                            if (h == g) {
                                ptResult2.setText(Integer.toString(h));
                            } else ptResult2.setText(Eval.cal(Double.toString(g)));
                        }
                    }
                    else if(j==')') {
                        bracketcount++;
                        if(bracketcount==0) {
                            double g = Double.valueOf(Eval.cal(b.substring(0, b.length() - 1)));
                            int h = (int) g;
                            if (h == g) {
                                ptResult2.setText(Integer.toString(h));
                            } else ptResult2.setText(Eval.cal(Double.toString(g)));
                        }
                        left_right();
                    }
                    else {
                        if(bracketcount==0) {
                            if(j=='.') dotcount=1;
                            double g = Double.valueOf(Eval.cal(b.substring(0, b.length() - 1)));
                            int h = (int) g;
                            if (h == g) {
                                ptResult2.setText(Integer.toString(h));
                            } else ptResult2.setText(Eval.cal(Double.toString(g)));
                        }
                    }
                }
            }
            else if(ptResult.length()==1) {
                bracketcount = 0;
                dotcount = 1;
                ptResult.setText(null);
                ptResult2.setText(null);
                s = null;
            }
        });
    }
    //괄호를 열었을 때
    private void left_right() {
      //비공개  
    }
}
cs

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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingRight="10dp"
    android:paddingLeft="10dp"
    android:paddingTop="50dp"
    android:stretchColumns="*"
    tools:context=".MainActivity">
 
    <TableLayout
        android:layout_height="0dp"
        android:layout_weight="2">
 
        <TableRow
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="0dp">
 
            <TextView
                android:id="@+id/et_result2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:hint="R"
                android:textStyle="bold"
                android:textColor="@color/black"
                android:autoSizeTextType="uniform"
                />
 
        </TableRow>
 
 
        <TableRow
            android:layout_width="0dp"
            android:layout_weight="8"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/et_result"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_weight="1"
                android:maxLines="3"
                android:textStyle="bold"
                android:textSize="25sp"
                android:hint="C"
                />
 
        </TableRow>
 
        <TableRow
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="0dp">
            <TextView
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/testbox"
                android:textSize="30sp"/>
            <ImageView
                android:layout_height="wrap_content"
 
                android:id="@+id/backspace"
                android:src="@drawable/ic_round_keyboard_backspace_24"
                />
        </TableRow>
    </TableLayout>
 
    <!-- 첫번째 버튼 줄-->
 
    <TableLayout
        android:layout_height="0dp"
        android:layout_weight="5">
 
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/btn_clear"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="C" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_left"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="(" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_right"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text=")" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_per"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="%" />
        </TableRow>
 
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/num_7"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="7" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_8"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="8" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_9"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="9" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_plus"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="+" />
        </TableRow>
        <!--첫 번째 버튼 줄 종료-->
 
        <!-- 두 번째 버튼 줄 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/num_4"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:backgroundTint="@color/black"
                android:autoSizeTextType="uniform"
                android:text="4" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_5"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:backgroundTint="@color/black"
                android:autoSizeTextType="uniform"
                android:text="5" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_6"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:backgroundTint="@color/black"
                android:autoSizeTextType="uniform"
                android:text="6" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_minus"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:backgroundTint="@color/black"
                android:autoSizeTextType="uniform"
                android:text="-" />
        </TableRow>
        <!-- 두 번째 버튼 줄 종료-->
 
        <!--세 번째 버튼 줄 시작-->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/num_1"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="1" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_2"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="2" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_3"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="3" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_multiple"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="*" />
        </TableRow>
        <!-- 세 번째 버튼 줄 종료-->
 
        <!-- 네 번째 버튼 줄 시작-->
 
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/btn_dot"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="." />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_0"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="0" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_result"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="=" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_slush"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:backgroundTint="@color/black"
                android:text="/" />
        </TableRow>
 
 
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="4">
 
        </TableRow>
        <!-- 광고 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.1">
            <com.google.android.gms.ads.AdView
                xmlns:ads="http://schemas.android.com/apk/res-auto"
                android:id="@+id/banner"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                android:layout_weight="0.5"
                ads:adSize="SMART_BANNER"
                ads:adUnitId="ca-app-pub-">
            </com.google.android.gms.ads.AdView>
        </TableRow>
        <!-- 네번째 줄 종료-->
 
    </TableLayout>
</TableLayout>
 
cs

Eval.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.GWdeveloper.calculator;
 
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
 
public class Eval {
    public static String cal(String result){
        ScriptEngineManager manager=new ScriptEngineManager();
        ScriptEngine engine=manager.getEngineByName("js");
        try{
            return engine.eval(result).toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
cs

결과

특별한 오류 없이 잘 돌아간다.

다만 autosizetexttype을 활용하여 기기의 크기에 따라서 글자 크기를 바꾸고 싶었지만,

적용이 안되더라... 왜 그런지 모르겠다.


play store 다운로드 링크

play.google.com/store/apps/details?id=com.GWdeveloper.calculator

 

초간단 계산기 - Google Play 앱

간단한 디자인과 간단한 기능들로만 만들어진 계산기입니다. 덧셈, 뺄셈, 나눗셈, 곱셈, 나머지 기능들만 넣었습니다. 오류 사항이 있으면 즉각 수정하겠습니다. 감사합니다.

play.google.com


참고 블로그

itstudy-mary.tistory.com/179

 

안드로이드 스튜디오,java] 간단한 계산기 앱 만들기

출력 결과 : 안드로이드 스튜디오를 접한 사람이라면 먼저 자바나 코틀린을 접했을 것이다. 자바의 간단한 계산기는 eval함수를 이용하여 문자열을 계산해준다. 그 함수는 javax 함수 위에 있는데,

itstudy-mary.tistory.com


위의 코드를 실행시키기 위해선 밑의 사이트에 접속 후

jar 파일 다운로드 -> library에 추가해야 Eval class가 정상적으로 작동할 수 있다.

ScriptEngine, ScriptEngineManager download www.java2s.com/Code/Jar/j/Downloadjsr223jar.htm

 

Download jsr223.jar : jsr223 « j « Jar File Download

 

www.java2s.com

 

반응형

댓글