Those of you who are bored and have nothing better to do, here is little simple project to keep anyone who is interested busy for a few hours. By the way this is real project that I have to do for my C++ class. So any one else who is doing a similar project may benifit from this one. OH! any question feel free to email me at firstname.lastname@example.org . thanks...
Oh! Please feel free to post your code. Thank you.
p.s. Truth is I also do need some help doing this project ..
A Polynomial Multiplication Project
You will design and implement a class to represent polynomials and perform polynomial multiplication using the STL list. You will also write a main program that is a client of the class. The main program will read a sequence of polynomial definitions and operations from a file, create polynomials, and perform the specified operations. If you need to brush up on polynomial arithmetic, refer to any elementary algebra textbook.
A polynomial (in one variable) is a function of the form
where n is a non-negative integer and ¹ 0. The degree of p(x) is n. No exponent in a polynomial is allowed to be negative. In this assignment, all coefficients are assumed to be integers. (It is a polynomial in an integer domain.) Note that if the degree of the polynomial is 0, it is a constant. For the remainder of this specification, I write p as a shorthand for p(x).
Given two polynomials p and q, the following binary operation is defined:
p * q the product of p and q
If p(x) = 2x + 3 and q(x) = -x + 1 then
p * q == -2x2 –x + 3
1. The Class Interface
You are to design the interface to a Polynomial class. The class must contain the following member functions.
eval(double x) evaluate the polynomial with argument x and return its value. This should be implemented as an overloaded operator().
operator* given polynomials p and q, return p*q
operator<<given an output stream os and polynomial p, display p in symbolic form on os
In addition, the class needs
· a default constructor that creates a zero polynomial,
· a constructor that takes a coefficient c and an exponent e and constructs a polynomial with the single term cxe, e.g., Polynomial p(c,e);
· a copy constructor that takes a polynomial q and makes a new polynomial that is a copy of q, e.g. Polynomial p(q);
· an operator= for the class that will assign a polynomial to an existing polynomial;
· a destructor, which deletes the polynomial.
Note that this description makes no mention of the private part of the class. That is up to you. You will probably find it necessary to implement other, private members of the Polynomial class.
2. The Class Implementation
A polynomial must be implemented using the STL list template. The list template provides all of the functions that will need. I suggest that your list nodes be terms. Each term is completely defined by its coefficient and exponent. No two terms can have the same exponent; i.e., every node must have a unique exponent. It is a good idea to keep the nodes sorted by exponent value. When two polynomials are added, some terms may cancel. For instance, if p = 2x + 1 and q = -2x + 2, and we let r = p + q, then r = 3. We can end up with a result with fewer (and possibly no) terms. This implies that you need to check when a coefficient of the result is zero, and delete the node. There are other issues that need to be resolved regarding how to perform the arithmetic.
3. Program Input and Output
The main program will read input from a text file. Each line of the file will be in one of the following formats:
p = a1 e1 a2 e2 … an en // definition
q = b1 e1 b2 e2 … bm em // definition
r = p * q // product
eval r(6) // eval
show r // output
· A definition line specifies a particular polynomial. The polynomial should be constructed and stored into the name on the left of the = symbol. An example definition is
p = 6 5 -4 3 -2 1 2 0
which represents the polynomial . There will always be white space between the numbers, and there will always be legal input. Note that 2 0 means the number 2 since x0 = 1. The input line should be copied to the output stream.
· The next line is an instruction to compute the product. The result should be saved in the polynomial named on the left of the = symbol. In each case, the input line should be copied to the output stream.
· The eval line requires that the polynomial r be evaluated with argument 6 and its result displayed in the output stream. The argument can be any floating point number, such as 3.2. The output should be
r(6) = whatever the value is
· The last line is an output instruction. The specified polynomial must be displayed in the output stream in the format below. If it is longer than can fit on a line of a page (60 chars), it may be wrapped onto a second line.
r = a1x^e1 + a2x^e2 + … + anx^en
The main program will not have to store more than 3 polynomials at a time because the input will never require that the program know about more than 3 polynomials at a time. I will ensure that. For example, the input could look like
p = 1 2 2 1 1 0
q = 1 1 1 0
r = p * q
p = r * r
q = p * p
In other words, if main() just keeps a p, q, and r handy, it can keep replacing them as needed. There is no need to create an array of polynomials. You may if you want to. If you do, I will give you 10% extra credit, but you will have to supply input to demonstrate that it works for any number of polynomial names.
4. Input and Output Files
you should design your own input file and test your program using your own input. Your output should be written to a file.
Make sure that you do not tie any objects to cin or cout. Make sure that you use constructors properly. I strongly urge you to follow the principles of good design. Work top down or bottom up, either way. I suggest top-down. this way you have a main program to start with and you can keep testing as you write. Design the main program. Figure out how to parse the input. Create a Polynomial class interface and implement it.