-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFracAddSub.java
More file actions
54 lines (46 loc) · 1.63 KB
/
Copy pathFracAddSub.java
File metadata and controls
54 lines (46 loc) · 1.63 KB
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
public class FracAddSub {
public String FracAddSub(String expr) {
int nume = 0;
int deno = 0;
int i = 0;
int n= expr.length();
while(i < n){
int currNume = 0; // Current fraction numerator
int currDeno = 0; // Current fraction denomenator
boolean isNeg = (expr.charAt(i) == '-');
// skip + or -
if(expr.charAt(i) == '+' || expr.charAt(i) == '-'){
i++;
}
// Build the current numerator
while(i < n && Character.isDigit(expr.charAt(i))){
int val = expr.charAt(i) - '0';
currNume = (currNume*10) + val;
i++;
}
i++; // skip the division character
if(isNeg){
currNume *= -1; // apply negative sign when it is needed
}
// Build the current denominator
while(i < n && Character.isDigit(expr.charAt(i))){
int val = expr.charAt(i) - '0';
currDeno = (currDeno*10) + val;
i++;
}
/* Update the accumulated numerator & denominator */
nume = (nume * currDeno) + (currNume * deno);
deno = deno * currDeno;
}
// Now we use the term Greatest common divisor
int gcd = gcd(Math.abs(nume), deno);
// return the result in terms of numerator & denominator
return nume+"/"+deno;
}
private int gcd(int a, int b){
if(b == 0){
return a;
}
return gcd(b, a%b);
}
}