K
K
kykyryky2014-10-07 23:41:22
Programming
kykyryky, 2014-10-07 23:41:22

How to multiply brackets and get a polynomial?

For example
Given: (x+1)(x+2)(x+3)
It should be: x^3 + 6x^2 + 11x + 6
Moreover, there can be either two or ten such brackets.
It seems not difficult in theory, but I don’t even know how to approach it. How to do it right?
First, let's make an array of length 4, i.e. n + 1, where n is the highest degree (number of brackets?), and we will store the coefficients at x in it. Once these ratios are calculated..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tsarevfs, 2014-10-07
@kykyryky

To parse an expression, the easiest way is to use translation into Polish notation (you can use the recursive descent method , it is a little more complicated, but more universal).
Next, describe the Poly class for the polynomial. Define addition and multiplication operations for it (by overloading the appropriate operators or simply by defining the add and mul methods). The polynomial itself is conveniently represented as a vector (array), the i-th element of which is the coefficient of x^i.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question