# question about programming polynomials

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-27-2011
firehydrant
question about programming polynomials
our homework is actually a long one, about numerical analysis, roots of poylnomials, etc. the problem is i don't know how to do the first step, which is storing/saving a user-input polynomial(eg this form: x^3-4x^2+2). do i use arrays?strings? and how do you save the polynomial as a function so that you can get the value of the polynomial when a value for x is given(say x = 2, then, from the ex above, f(x) = -6).
• 02-27-2011
GReaper
I don't know what option is best for your case, but why don't you experiment with different approaches?
• 02-27-2011
firehydrant
say i use strings to store the polynomial, how can i save it as a function?
• 02-27-2011
GReaper
You can't save it directly as a function but rather pass it to a function that will translate it to the proper operations.
• 02-27-2011
firehydrant
how?
• 02-27-2011
GReaper
I have an idea about how to store the polynomial. My opinion is, store it in a struct that has two elements: an integer "maxPow" that represents the bigger power and an array of size "maxPow" that will store the coefficients of each part of the polynomial.
• 02-27-2011
GReaper
Quote:

Originally Posted by firehydrant
how?

For that you'll need to know how to parse math expressions. There are many tutorials online, compiler expression parsing such get you started! :)
• 02-27-2011
firehydrant
we haven't tackled structs yet in class, so as of the moment, i can't use those.

if i use strings to store the polynomial, is there a way to pick out the numeric parts of it? that way, i can assign it as coefficients or exponents in my function.
• 02-27-2011
GReaper
Quote:

Originally Posted by firehydrant
if i use strings to store the polynomial, is there a way to pick out the numeric parts of it? that way, i can assign it as coefficients or exponents in my function.

Of course there is, but as i said, that can be done by parsing the math expressions inside it.
And since you don't know structs yet, this "translation" has to be done everytime you want to call that polynomial.
• 02-27-2011
firehydrant
how do i do it exactly?
• 02-27-2011
GReaper
Quote:

Originally Posted by firehydrant
how do i do it exactly?

Search for Math Expression Parsing
• 02-27-2011
firehydrant
ok
Thanks!
• 02-27-2011
Bayint Naung
The simplest way is ask user from number of degree(n).
and ask coefficient of each term.

Code:

```Deg of polynomial: 2 coefficient of x^2 : 1 coefficient of x^1 : -4 coeficient of x^0 : 2```
• 02-28-2011
krishnapollu
i have a suggestion. but i think it will make the program a bit lengthy.
anyway let me.

i think u ppl hav heard about infix and postfix expressions.
first of all, consider the input polynomial as an INFIX expression.
convert it to POSTFIX and then evaluate it by giving values to variables.
• 03-02-2011
firehydrant
how do you that math expression parsing?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last