-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolveEquation.cpp
More file actions
45 lines (45 loc) · 1.24 KB
/
solveEquation.cpp
File metadata and controls
45 lines (45 loc) · 1.24 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
class Solution {
public:
int coef = 0, consant = 0;
string cur = "";
bool if_plus = true, if_left = true;
inline int getVal(bool if_x) {
if (cur == "") {
if (if_x)
return if_plus ? 1 : -1;
else return 0;
}
int temp = stoi(cur);
cur = "";
if (!if_plus) return -temp;
return temp;
}
string solveEquation(string equation) {
for (char c: equation) {
if (c == '+' || c == '-') {
int temp = getVal(false);
if (temp)
consant += if_left ? -temp : temp;
if_plus = (c == '+');
}
else if (c == '=') {
consant += -1 * getVal(false);
if_left = false;
if_plus = true;
}
else if (c == 'x') {
int temp = getVal(true);
coef += if_left ? temp : -temp;
}
else {
cur += c;
}
}
consant += getVal(false);
if (!coef && !consant)
return "Infinite solutions";
if (!coef && consant)
return "No solution";
return "x=" + to_string(consant / coef);
}
};