1 solutions
-
0
C++ :
#include<bits/stdc++.h> using namespace std; struct EvaluationSlot { public: int UnknownSlot; int ConstantSlot; EvaluationSlot() { UnknownSlot = 0; ConstantSlot = 0; } }; int main() { cin.sync_with_stdio(false); cout.sync_with_stdio(false); //boolalpha(cout); std::string input; getline(cin, input); size_t whereEqual = input.find('='); std::string left = input.substr(0, whereEqual + 1); std::string right = input.substr(whereEqual + 1); right = right + "="; EvaluationSlot leftEq{}; EvaluationSlot rightEq{}; char unknownSymbol = ' '; bool currentItemUnknown = false; int nextItemSign = 1; std::string cur; std::stringstream stringBuilder{}; for (size_t i = 0, len = left.length(); i != len; ++i) { const char currentChar = left[i]; switch (currentChar) { case '+': case '=': cur = stringBuilder.str(); stringBuilder.str(""); if (currentItemUnknown) { cur[cur.length() - 1] = '\0'; int c = atoi(cur.c_str()); if (cur.length() == 1) { c = 1; } leftEq.UnknownSlot += (c * nextItemSign); } else { int c = atoi(cur.c_str()); leftEq.ConstantSlot += (c * nextItemSign); } nextItemSign = 1; currentItemUnknown = false; break; case '-': cur = stringBuilder.str(); stringBuilder.str(""); if (currentItemUnknown) { cur[cur.length() - 1] = '\0'; int c = atoi(cur.c_str()); if (cur.length() == 1) { c = 1; } leftEq.UnknownSlot += (c * nextItemSign); } else { int c = atoi(cur.c_str()); leftEq.ConstantSlot += (c * nextItemSign); } nextItemSign = -1; currentItemUnknown = false; break; default: stringBuilder << currentChar; if (isalpha(currentChar)) { unknownSymbol = currentChar; currentItemUnknown = true; } break; } } stringBuilder.str(""); currentItemUnknown = false; nextItemSign = 1; for (size_t i = 0, len = right.length(); i != len; ++i) { const char currentChar = right[i]; switch (currentChar) { case '+': case '=': cur = stringBuilder.str(); stringBuilder.str(""); if (currentItemUnknown) { cur[cur.length() - 1] = '\0'; int c = atoi(cur.c_str()); if (cur.length() == 1) { c = 1; } rightEq.UnknownSlot += (c * nextItemSign); } else { int c = atoi(cur.c_str()); rightEq.ConstantSlot += (c * nextItemSign); } currentItemUnknown = false; nextItemSign = 1; break; case '-': cur = stringBuilder.str(); stringBuilder.str(""); if (currentItemUnknown) { cur[cur.length() - 1] = '\0'; int c = atoi(cur.c_str()); if (cur.length() == 1) { c = 1; } rightEq.UnknownSlot += (c * nextItemSign); } else { int c = atoi(cur.c_str()); rightEq.ConstantSlot += (c * nextItemSign); } currentItemUnknown = false; nextItemSign = -1; break; default: stringBuilder << currentChar; if (isalpha(currentChar)) { unknownSymbol = currentChar; currentItemUnknown = true; } break; } } leftEq.UnknownSlot -= rightEq.UnknownSlot; rightEq.ConstantSlot -= leftEq.ConstantSlot; double result = rightEq.ConstantSlot / (double)leftEq.UnknownSlot; if (result == -0.0) { result = 0; } cout << unknownSymbol << '=' << fixed << setprecision(3) << result << endl; return 0; }
- 1
Information
- ID
- 9184
- Time
- 1000ms
- Memory
- 64MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By