Ok I have two strings...
My task is to add string A to B i.e
String A = "5x + 4"
String B = "3x + 2 - 5x^2"
and to subtract string A from B
(5x + 4) + (3x + 2 - 5x^2) = 8x + 6 - 5x^2
Note the change in sign shown in red.
(5x + 4) - (3x + 2 - 5x^2) = 2x + 2 + 5x^2
I have these functions already...
1.Split string into terms.
2.Stores terms into vector.
so "5x + 4" would become "5x, +4"
So Vector_string_A is:
and vector_string_B is:
|5x| +4 |
|3x| +2|-5x^2 |
3.Get coefficient(self explanatory)
4.Get exponent (self explanatory)
The question is,given these functions what would be the
simplest way to implement the addition and subtraction
of a polynomial?
I was thinking about using a nested for loop and
looping through the elements in vector_string_A and vector string_B and adding coeff if the exponents are equal. However,
I can't see a good way to do this. My mind's a blank.
Thanks for your time.