Two polynomials are multiplied together by multiplying each term in the first polynomial with each term in the second polynomial, and adding all of these individual products together. For example:
(4*x^4 + 1) * (3*x^5 + 7*x)
= (4*x^4 * 3*x^5) + (4*x^4 * 7*x) + (1 * 3*x^5) + (1 * 7*x)
= 12*x^9 + 28*x^5 + 3*x^5 + 7*x
This can be simplified by combining the two x^5 terms:
= 12*x^9 + 31*x^5 + 7*x
Write a program which multiplies two polynomials, and simplifies the product by combining terms with the same exponent. Each polynomial will be a function of x, and will consist of one or more terms separated by " + " (a space, '+', and another space). Each term will be in one of the following forms:
1
#
x
x^#
#*x
#*x^#
(Each '#' character represents a single digit between '2' and '9', inclusive.)
Your program should return the product as a sum of terms, each in one of the formats above, except that the coefficients and exponents in your return value may be larger than a single digit. (Note that coefficients and exponents of 1 are omitted.) In between each term, insert the String " + " (a space, '+', and another space). You should output the terms in order from largest exponent of x to smallest exponent of x. If two or more terms have the same exponent, add their coefficients together to combine them into a single term.
|