The task is to write an equation solver, which takes a string expression containing a mathematical formula, then calculating its roots.
For simplicitys sake, it doesn't have to be able to solve equations larger than grade 2 (that's extra bonus points if it does ).
The letter X (capital x) will be the unknown variable.
The string expression can contain extra spaces, so your function must handle this properly.
Bonus points are awarded if the function can handle parantheses and division, but this is not required.
Examples of string input:
Code:
"X = 5"
"2*X = 12 - 4"
"X+8 = 78-X + 3*X"
"X^2 = 4*2 + 8"
"5*X^2 + 12*X + 8 = -X + 3*X^2 - 3 - X +8*X + 15"
"X = -8"
" X =8 "
The syntax of this function will look like this:
void SolveEquation(const char* Equation, char* ResultBuffer);
Where Equation is the string containing the equation, and ResultBuffer is the buffer where you store the answer after it has been calculated. This buffer is allocated outside the function, so your function can assume that there is enough space to store your answer.
You decide the syntax of this answer yourself. It could look something like this:
Code:
"r1 = ???, r2 = ???"
"Answer: ???, ???"
"??? ???"
Whatever, you decide!
Alternative solution:
If you find this too easy, you can make a function with the syntax:
void SolveEquation(const char* Equation, char Variable, char* ResultBuffer);
Equation and ResultBuffer is the same as before. Variable is a character representing the unknown variable, and all other variables encountered in the Equation expression will be treated as unknown constants.
Examples (X is the unknown for everyone here):
Code:
"X^2 + a*X + b = 0"
"3*a*X^2 + 4*b*X + 3*a + c = b"