# Thread: Polynomial addition

1. ## Polynomial addition

Ok I have two strings...

Code:
String A = "5x + 4"
String B = "3x + 2 - 5x^2"
My task is to add string A to B i.e

Code:
(5x + 4) + (3x + 2 - 5x^2) = 8x + 6 - 5x^2
and to subtract string A from B

Code:
(5x + 4) - (3x + 2 - 5x^2) = 2x + 2 + 5x^2
Note the change in sign shown in red.

I have these functions already...

1.Split string into terms.

Code:
so "5x + 4" would become  "5x, +4"
2.Stores terms into vector.
So Vector_string_A is:

Code:
+--+----+
|5x| +4 |
+--+----+
and vector_string_B is:

Code:
+--+---+------+
|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.

Any ideas?

Thanks for your time.

2. ***Solved

I found what I was lookin for.

Popular pages Recent additions