forked from yashrajmani/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculatorWithoutOperators.java
351 lines (171 loc) · 7.29 KB
/
CalculatorWithoutOperators.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺
*************************************
ENTER FIRST NUMBER ⤵️ hit enter
ENTER OPERATOR (+,-,*,/) ⤵️ hit enter
ENTER SECOND NUMBER ✔️ press submit
🔸🔸NOTE🔸🔸:
🔹If your choise is +,- or /
the numbers need to be INTEGERS
🔹Only if you have chosen *
the numbers can be and DECIMAL
🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺
*************************************
*/
import java.util.Scanner;
public class CalculatorWithotOperators{
static int reminder=0;
static String res="";
public static void main(String[] args) {
try{
Scanner in=new Scanner(System.in);
double ad=in.nextDouble();
String operator=in.next();
double bd=in.nextDouble();
System.out.printf("First number: %s%nOperator: %s%nSecond number: %s%n", String.valueOf(ad),operator,String.valueOf(bd));
checkInput(ad,bd,operator.trim());
in.close();
}catch(Exception e){
System.out.println("Your inputs are incorrect.Try again.");
}
}
static void checkInput(double ad, double bd, String operator) {
// check if the input numbers are integer numbers for operation +, -, / and conversion form double type to int (example 5.0 = 5) for further work
if(operator.equals("+")||operator.equals("-")||operator.equals("/")) {
int a=(int)ad;
int b=(int)bd;
//if the input numbers are not integers, code ends with execution
if(ad!=a || bd!=b) {
System.out.println("Your input is incorrect. Please enter a integers.");
System.exit(0);
}
// find out which operation is being performed (depending on the input operator) and displaying the final result
switch(operator) {
case "+":
System.out.println("Result: ".concat(String.valueOf(add(a,b))));
break;
case "-":
System.out.println("Result: ".concat(String.valueOf(subtract(a,b))));
break;
case "/":
System.out.println("Result: ".concat(String.valueOf(divide(a,b))));
break;
}
}
else if(operator.equals("*")) System.out.println(multiple(ad,bd));
// if the input operator does not respond to any of the +, -, *, / code ends with execution
else System.out.println("Your input is incorrect. Operator can be: -, +, * or /");
}
//sum of two integer can be obtained by XOR bitwise operator and carry can be obtained but AND bitwise operator. We also need to use signed left shift Java operator to add carry into sum.
static int add(int a,int b) {
int sum,carry;
do{
//sum of two bits is A XOR B
sum=a^b;
//carry is AND of two bits
carry=a&b;
//we are calculating carry and keeping it in a separate variable, than we are storing sum of two numbers into variable a, and shifts carry to 1 bit by using signed left shift operator, in order to add into sum.
a=sum;
//shifts carry to 1 bit to calculate sum
b=carry<<1;
}while(b!=0);
return sum;
}
static int subtract(int a, int b){
// instead subtract we find sum for a+(-b) => a-b
// remeber that -b = (~b)+1
return add(a,add(~b,1));
}
static int multiply(int a,int b){
// 0 multiplied with anything gives 0
if(b==0) return 0;
// add "a" one by one
// for example: 3*5=3+3+3+3+3
else if(b>0) return (add(a,multiply(a, subtract(b,1))));
//the case where "b" is negative
else return -multiply(a,-b);
}
static int division(int a,int b) {
//we start by shifting the divisor left until it's greater than the dividend. We then repeatedly shift it right and for each right shift check whether that value is less than the intermediate we got after the last subtraction. If it's less, we subtract again and fill in a for that digit in our result. If it's greater, we "subtract 0" (don't do anything).When we've filled in all the digits, that's our result, and any amount left that we haven't subtracted yet is our remainder.
int temp = 1;
int quot = 0;
while (b <= a) {
b <<= 1;
temp <<= 1;
}
while (temp > 1) {
b >>= 1;
temp >>= 1;
if (a >= b) {
a =subtract(a,b);
quot=add(quot,temp);
}
}
reminder=a;
return quot;
}
static String multiple(double a, double b){
//we find the decimal point position for "double a" and the number of digits behind the decimal point
//after that, we convert the decimal number to the integer ignoring the decimal comma (5.24 => 524)
int countA=0;
String s=String.valueOf(a);
int dot = s.indexOf('.');
countA = subtract(subtract(s. length(),dot),1);
int ai=Integer.parseInt(s.replace (".",""));
//we find the decimal point position for "double b" and the number of digits behind the decimal point
//after that, we convert the decimal number to the integer ignoring the decimal comma (5.24 => 524)
int countB=0;
s=String.valueOf(b);
dot=s.indexOf('.');
countB = subtract(subtract(s. length(),dot),1);
int bi=Integer.parseInt(s.replace (".",""));
//multiplay numbers without decimal comma (integers)
res=String.valueOf(multiply (ai,bi));
//calculate total decimal places
int pos=add(countA,countB);
// if the inputs are integers return result
if(pos==0) return res;
// if the inputs are decimal numbers, need to arrange the result by placing a decimal point at the appropriate location
return res.substring(0,subtract(res.length(),pos)).concat(".".concat(res.substring(subtract(res.length(),pos))));
}
static String divide(int a, int b) {
boolean dot=false; //we assume that the result will be an integer(without dot)
//we check if the number "b" is negative. If is negative, we convert it to positive(-1*b)
int signB=1;
if(b<0) {
signB=-1;
b=multiply(b,signB);
}
//we check if the number "a" is negative. If is negative, we convert it to positive (-1*a)
int signA=1;
if(a<0){
signA=-1;
a=multiply(a,signA);
}
//if the divisor greater than dividend, result will be decimal number
if(a<b){ a=find(a,b); dot=true;}
String sign="";
// if some of inputs are negative number then result will be negative, except if both numbers are negative
if(signA==-1||signB==-1) sign="-";
if(signA==-1&&signB==-1) sign=""; res=sign.concat(res.concat(String.valueOf(division(a,b))));
// if we have remainder, result will be decimal number
if(reminder!=0&&dot==false) res=res.concat(".");
//multiplay while has reminder, but result can't be over 10 digits(because of time limit exceed)
while(reminder!=0) {
res=res.concat(String.valueOf(division(multiply(reminder,10),b)));
if(res.length()>10) return res;
}
return res;
}
static int find(int a, int b) {
//this method find how much zero goes behind the decimal point (until divisor multiplied by 10 is greater than dividend)
res=res.concat("0.");
int count=0;
while(a<b){
count=add(count,1);
if(count>1) res=res.concat("0");
a=multiply(a,10);
}
return a;
}
}