professor means by solve is to simplify the expression |
Then he probably means to add/subtract like terms.
If it's only addition and subtraction then it's relatively easy.
Step 1: Split string into substrings.
https://www.geeksforgeeks.org/split-a-sentence-into-words-in-cpp/
Better to use stringstreams. I donno if people ever use strtok() in C++, it's a C function.
Step 2: Parse the substrings.
Iterate over the substrings like so:
--> If value isn't a +/- then append it to an array "Variables"
--> If value is +/- then ditch it and set a bool variable to true.
--> When the bool variable is true, the next to be appended gets a - (unary minus)
Then set back the bool to false after appending.
You will now have an array of terms.
Step 3: Iterate over the array finding like terms.
Little more complex.
--> You've got to find the variable of each term and keep it in memory (an array)
That means you will have to ignore the (-) and the numbers, you can store them too if you want.
--> For every new variable you find, you will compare it with every element of the array.
If you find a like element then you will add the constant parts of the two terms and pop the successive terms.
Once you're done with one iteration, your equation is simplified.
Step 4: Obtain a string for the simplified equation
Simply append all values to a string. If you want you can make it so that when there's a negative value, you first append a '-' followed by a space and then the absolute value.
Exception handling is up to you.