1. ## Polynomial class

I plan revise my program to handle polynomial equations with one variable.

i.e.

Code:
``` 5x² - 3x + 7 - x  ...

(x + 7)*(3 - x)```
So I am planning to create a class or function which handles, for the time being, addition, subtraction and multiplication of polynomials.

Any ideas or pointers in the right direction would be useful.

At present I have had the following ideas.

To convert all terms into the form :-

Code:
`ax^b`
Where 'a' and 'b' are just integers.

So x^2 would be:-

Code:
`1x^2`
5x would be:-

Code:
`5x^1`
And just plain ordinary numbers such as '7' would be:-

Code:
`7x^0`
Recall that x^0 = 1

so 7^1 = 7.

Since each term will have three stages. It will be reasonably easy to separate the coefficients from the exponents and do the appropriate math.

For my addition/subtraction class I have this idea.

Code:
```String Addition (string A, string B)
{
A = "3x + 7";     » 3x^1  +  7x^0
B = "4x² - 8x";  » 4x^2  -   8x^1

... Count no. terms in string A and B
Create a nested for loop, looping through the no. terms
Add together coefficients if the exponents are equal

return string;
}```
For my multiplication class I have this idea.

Code:
```String Multiplication (string A, string B)
{
A = "x+2";      »1x^1  +  2x^0
B = "x-3";       »1x^1  +  3x^0

...Count no. terms in string A and B
Create a nested for loop, looping through the no. terms
Multiply coefficients and exponents, of course is a
little more complicated than this but you get the idea.

return string;
}```
Thanks

2. Originally Posted by treenef
I plan revise my program to handle polynomial equations with one variable.

i.e.

Code:
``` 5x² - 3x + 7 - x  ...

(x + 7)*(3 - x)```
So I am planning to create a class or function which handles, for the time being, addition, subtraction and multiplication of polynomials.

Any ideas or pointers in the right direction would be useful.

At present I have had the following ideas.

To convert all terms into the form :-

Code:
`ax^b`
Where 'a' and 'b' are just integers.

So x^2 would be:-

Code:
`1x^2`
5x would be:-

Code:
`5x^1`
And just plain ordinary numbers such as '7' would be:-

Code:
`7x^0`
Recall that x^0 = 1

so 7^1 = 7.

Since each term will have three stages. It will be reasonably easy to separate the coefficients from the exponents and do the appropriate math.

For my addition/subtraction class I have this idea.

Code:
```String Addition (string A, string B)
{
A = "3x + 7";     » 3x^1  +  7x^0
B = "4x² - 8x";  » 4x^2  -   8x^1

... Count no. terms in string A and B
Create a nested for loop, looping through the no. terms
Add together coefficients if the exponents are equal

return string;
}```
For my multiplication class I have this idea.

Code:
```String Multiplication (string A, string B)
{
A = "x+2";      »1x^1  +  2x^0
B = "x-3";       »1x^1  +  3x^0

...Count no. terms in string A and B
Create a nested for loop, looping through the no. terms
Multiply coefficients and exponents, of course is a
little more complicated than this but you get the idea.

return string;
}```
Thanks
First of all, I don't understand why you feel you have to have different classes for the different operations. Surely it would be easier to have just one class, say Polynomial, and Addition, Multiplication etc will be methods.
Secondly, you need to define the methods in a class {} structure, which you don't appear to have done.
Thirdly, there is no "string" type in Standard C++.

3. >Thirdly, there is no "string" type in Standard C++.
You're kidding, right?

4. Originally Posted by swoopy
>Thirdly, there is no "string" type in Standard C++.
You're kidding, right?
I think he means "String", as the OP used a capital S in string for the return types. Which is how Java does it. <_<

5. Well it would be either a polynomial class or just separate functions.

One function for multiplication,addition and subtraction.

And if I was using a class, different methods for addition, multiplication and subtraction.

I was still deciding on whether to use a class structure or just plain ol' functions. In my post above I have illustrated my ideas using functions. Yeah I guess I use the word class and function quite loosely.

Anyway the points mentioned are trivial. Rather I was wondering if anyone had any actual ideas on how to handle polynomials. If so I'm all ears.