-
Notifications
You must be signed in to change notification settings - Fork 0
/
1015.cpp
62 lines (50 loc) · 1.47 KB
/
1015.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
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class SubExpr {
public:
int term_count;
string terms[20];
SubExpr(string s) : term_count(0) {
int pos;
while ((pos = s.find('-', 1)) != -1 || (pos = s.find('+', 1)) != -1) {
terms[term_count++] = s.substr(0, pos);
s = s.substr(pos);
}
terms[term_count++] = s;
}
};
int main() {
string expr;
cin >> expr;
int pos = expr.find('=');
string l_expr = expr.substr(0, pos);
string r_expr = expr.substr(pos + 1);
SubExpr left(l_expr);
SubExpr right(r_expr);
int l_x;
int r_x;
int l_c = 0;
int r_c = 0;
char x;
for (int i = 0; i < left.term_count; i++) {
if (isalpha(left.terms[i][left.terms[i].length() - 1])) {
x = left.terms[i][left.terms[i].length() - 1];
l_x = atoi((left.terms[i].substr(0, left.terms[i].length() - 1)).c_str());
} else {
l_c += atoi(left.terms[i].c_str());
}
}
for (int i = 0; i < right.term_count; i++) {
if (isalpha(right.terms[i][right.terms[i].length() - 1])) {
r_x = atoi((right.terms[i].substr(0, right.terms[i].length() - 1)).c_str());
} else {
r_c += atoi(right.terms[i].c_str());
}
}
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.precision(3);
cout << x << '=' << (r_c - l_c) / (double)(l_x - r_x) << endl;
return 0;
}