-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompare two fractions.cpp
50 lines (34 loc) Β· 1.14 KB
/
Compare two fractions.cpp
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
class Solution {
public:
string compareFrac(string str) {
string str1="",str2="",str3="",str4="";
bool chk1=false,chk2=false,chk3=false;
for(int i=0;i<str.size();i++){
if(str[i]==' ') continue;
if(str[i]=='/' && chk1 == false) {
chk1 = true;
continue;
}
if(chk1 == false) str1+=str[i];
if(str[i] == ',') {
chk2 = true;
continue;
}
if(chk1 == true && chk2 == false) str2+= str[i];
if(chk2 == true && str[i]== '/'){
chk3 = true;
continue;
}
if(chk2 == true && chk3 == false) str3+= str[i];
if(chk3 == true) str4+=str[i];
}
int a,b,c,d;
a = stoi(str1);
b = stoi(str2);
c= stoi(str3);
d = stoi(str4);
if(a*d > c*b) return str1+"/"+str2;
else if(a*d == c*b) return "equal";
else return str3+"/"+str4;
}
};