Solve a given equation and return the value of x
in the form of a string x=#value
. The equation contains only +,-
operation, the variable x
and its coefficient. You should return No solution
if there is no solution for the equation, or Infinite solutions
if there are infinite solutions for the equation.
If there is exactly one solution for the equation, we ensure that the value of x
is an integer.
#include
#include
#include
class Solution {
public:
std::string solveEquation(std::string equation) {
int results[2] = {0, 0}; // first element is the summation of coefficients, second element is the summation of constants
int sign = 1;
int i = 0;
int n = equation.size();
while (i < n) {
if (equation[i] == '=') {
sign = -1;
i++;
continue;
}
int sign_part = sign;
if (equation[i] == '+' || equation[i] == '-') {
sign_part = (equation[i] == '+' ? 1 : -1) * sign;
i++;
}
int val = 0;
bool is_valid = false;
while (i < n && isdigit(equation[i])) {
val = val * 10 + equation[i] - '0';
i++;
is_valid = true;
}
if (i < n && equation[i] == 'x') {
results[0] += is_valid ? sign_part * val : sign_part;
i++;
} else {
results[1] += sign_part * val;
}
}
if (results[0] == 0) {
return results[1] == 0 ? "Infinite solutions" : "No solution";
} else {
return "x=" + std::to_string(-results[1]/results[0]);
}
}
};
int main()
{
Solution s;
std::cout << "The resuts of x+5-3+x=6 is as follows:" << std::endl;
std::cout << s.solveEquation("x+5-3+x=6+x-2") << std::endl;
return 0;
}
class Solution {
public:
string solveEquation(string equation) {
string s = equation;
// split s into two parts by '='
int pos = s.find('=');
string sLeft = s.substr(0, pos);
string sRight = s.substr(pos + 1);
// analyze each part into coefficents and constants by '+' and '-'
vector<int> coefficients;
vector<int> constants;
analyzeString(sLeft, coefficients, constants, 1);
analyzeString(sRight, coefficients, constants, -1);
// add all the coefficents and constants
int sum_coeff = 0, sum_const = 0;
for (int i = 0; i < coefficients.size(); i++) {
sum_coeff += coefficients[i];
}
for (int i = 0; i < constants.size(); i++) {
sum_const += constants[i];
}
// return the result
string result = "";
if (!sum_coeff) {
result = sum_const ? "No solution" : "Infinite solutions";
} else {
result = "x=" + to_string(-sum_const / sum_coeff);
}
return result;
}
void analyzeString(string s, vector<int>& coefficients, vector<int>& constants, int sign) {
int sign_ = sign;
if (s[0] == '+' || s[0] == '-') {
s = s.substr(1);
sign_ = sign * (s[0] == '+' ? 1 : -1);
}
int index = 0;
while (index < s.size()) {
string num = "";
int sign_next = sign;
if (s[index] == '+' || s[index] == '-') {
sign_next = s[index] == '+' ? 1 : -1;
num = s.substr(0, index);
if (!num.empty()) {
if (num.find('x') != string::npos) {
if (num[0] == 'x') {
coefficients.push_back(sign_);
} else {
coefficients.push_back(sign_ * stoi(num));
}
} else {
constants.push_back(sign_ * stoi(num));
}
}
s = s.substr(index + 1);
index = 0;
sign_ = sign * sign_next;
} else {
index++;
}
}
if (!s.empty()) {
if (s.find('x') != string::npos) {
if (s[0] == 'x') {
coefficients.push_back(sign_);
} else {
coefficients.push_back(sign_ * stoi(s));
}
} else {
constants.push_back(sign_ * stoi(s));
}
}
}
};