5
5
import android .os .Vibrator ;
6
6
import android .view .View ;
7
7
import android .widget .Button ;
8
+ import android .widget .Toast ;
8
9
import androidx .appcompat .app .AppCompatActivity ;
9
10
import com .example .kalkulator .R ;
10
11
import com .example .kalkulator .listeners .DigitOnClickListener ;
@@ -27,6 +28,7 @@ protected void onCreate(Bundle savedInstanceState)
27
28
super .onCreate (savedInstanceState );
28
29
setContentView (R .layout .activity_advanced_calculator );
29
30
31
+ CalculatorHandler .setContextForToastMessages (this );
30
32
CalculatorHandler .setVibrator ((Vibrator ) getSystemService (Context .VIBRATOR_SERVICE ));
31
33
CalculatorHandler .setValueTextView (findViewById (R .id .value_text_view ));
32
34
CalculatorHandler .setOperationTextView (findViewById (R .id .operation_text_view ));
@@ -125,11 +127,20 @@ public void percentOnClick(View v)
125
127
126
128
double result = OperationOnClickListener .calculate (prevValue , currValue );
127
129
128
- prevValueTextView .setText ("" );
129
- operationTextView .setText ("" );
130
- valueTextView .setText (DECIMAL_FORMAT .format (result ).replace ('.' , ',' ));
130
+ String formattedOutput = DECIMAL_FORMAT .format (result ).replace ('.' , ',' );
131
+
132
+ if (formattedOutput .equals (INFINITY_SYMBOL ) || formattedOutput .equals (MINUS_NAN ) || formattedOutput .equals (NAN ))
133
+ {
134
+ Toast .makeText (this , "Cannot divide by 0" , Toast .LENGTH_SHORT ).show ();
135
+ }
136
+ else if (!isOutputTooLong (formattedOutput ))
137
+ {
138
+ prevValueTextView .setText ("" );
139
+ operationTextView .setText ("" );
140
+ valueTextView .setText (formattedOutput );
131
141
132
- newValueFlag = true ;
142
+ newValueFlag = true ;
143
+ }
133
144
}
134
145
135
146
makeStandardVibration ();
@@ -141,9 +152,16 @@ private void logOnClick(View view)
141
152
142
153
Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
143
154
144
- currValue = Math .log10 (currValue );
155
+ if (currValue > 0.0D )
156
+ {
157
+ currValue = Math .log10 (currValue );
145
158
146
- valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
159
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
160
+ }
161
+ else
162
+ {
163
+ Toast .makeText (this , "The number must not be negative" , Toast .LENGTH_SHORT ).show ();
164
+ }
147
165
148
166
makeStandardVibration ();
149
167
}
@@ -154,9 +172,16 @@ private void sqrtOnClick(View view)
154
172
155
173
Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
156
174
157
- currValue = Math .sqrt (currValue );
175
+ if (currValue >= 0.0D )
176
+ {
177
+ currValue = Math .sqrt (currValue );
158
178
159
- valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
179
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
180
+ }
181
+ else
182
+ {
183
+ Toast .makeText (this , "The number must not be negative" , Toast .LENGTH_SHORT ).show ();
184
+ }
160
185
161
186
makeStandardVibration ();
162
187
}
@@ -167,9 +192,16 @@ private void lnOnClick(View view)
167
192
168
193
Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
169
194
170
- currValue = Math .log (currValue );
195
+ if (currValue > 0.0D )
196
+ {
197
+ currValue = Math .log (currValue );
171
198
172
- valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
199
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
200
+ }
201
+ else
202
+ {
203
+ Toast .makeText (this , "The number must not be negative" , Toast .LENGTH_SHORT ).show ();
204
+ }
173
205
174
206
makeStandardVibration ();
175
207
}
@@ -205,10 +237,25 @@ private void tanOnClick(View view)
205
237
String valueText = valueTextView .getText ().toString ();
206
238
207
239
Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
240
+ double delta = 1E-3 ;
208
241
209
- currValue = Math .tan (currValue );
242
+ // find k
243
+ int k = (int ) Math .round ((currValue - Math .PI / 2 ) / Math .PI );
210
244
211
- valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
245
+ // calculate the difference
246
+ double diff = Math .abs (currValue - (Math .PI / 2 + k * Math .PI ));
247
+
248
+ // check if the input is very close to PI/2 + kPI
249
+ if (diff < delta )
250
+ {
251
+ Toast .makeText (this , "The input is very close or equal to PI/2 + kPI, so the result does not exist" , Toast .LENGTH_LONG ).show ();
252
+ }
253
+ else
254
+ {
255
+ currValue = Math .tan (currValue );
256
+
257
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
258
+ }
212
259
213
260
makeStandardVibration ();
214
261
}
@@ -221,7 +268,12 @@ private void xSquaredOnClick(View view)
221
268
222
269
currValue = Math .pow (currValue , 2.0 );
223
270
224
- valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
271
+ String formattedOutput = DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' );
272
+
273
+ if (!isOutputTooLong (formattedOutput ))
274
+ {
275
+ valueTextView .setText (formattedOutput );
276
+ }
225
277
226
278
makeStandardVibration ();
227
279
}
@@ -279,11 +331,20 @@ public void equalsOnClick(View v)
279
331
280
332
double result = OperationOnClickListener .calculate (prevValue , currValue );
281
333
282
- prevValueTextView .setText ("" );
283
- operationTextView .setText ("" );
284
- valueTextView .setText (DECIMAL_FORMAT .format (result ).replace ('.' , ',' ));
334
+ String formattedOutput = DECIMAL_FORMAT .format (result ).replace ('.' , ',' );
335
+
336
+ if (formattedOutput .equals (INFINITY_SYMBOL ))
337
+ {
338
+ Toast .makeText (this , "Cannot divide by 0" , Toast .LENGTH_SHORT ).show ();
339
+ }
340
+ else if (!isOutputTooLong (formattedOutput ))
341
+ {
342
+ prevValueTextView .setText ("" );
343
+ operationTextView .setText ("" );
344
+ valueTextView .setText (formattedOutput );
285
345
286
- newValueFlag = true ;
346
+ newValueFlag = true ;
347
+ }
287
348
}
288
349
289
350
makeStandardVibration ();
0 commit comments