Thread: Polynomial Problem

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    Post Polynomial Problem

    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 [email protected] . 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.
    Polynomial Arithmetic
    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
    Example
    If p(x) = 2x + 3 and q(x) = -x + 1 then
    p * q == -2x2 –x + 3
    Project Details
    1. The Class Interface
    You are to design the interface to a Polynomial class. The class must contain the following member functions.
    Name Description
    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
    show r
    p = r * r
    q = p * p
    show q


    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.

    Reminders
    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.


    Even the greatest of whale is helpless, in the middle of the dessert.
    So help someone who needs help today then some one else will help U tommorrow when U need it.

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > Truth is I also do need some help doing this project

    What part do you need help with? Post your code and people will help you...

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    my apology

    Hello to all,
    First I would like to extend my apology to all for posting my project. My intention is not for any one to do my project for me, cause I am almost done anyway. All I wanted was to get ideas how others will approch this problem that is all. The only problem I am having with this project is the eval(x) function and the reason I did not post it because am still trying to solve it my self. Maybe my wording was not right which gave ppl the wrong impression and I apologized for it. I am new in posting stuff in boards so please excuse me and everyone has the option to not post anything. Thank you.


    Even the greatest of whale is helpless, in the middle of the dessert.
    So help someone who needs help today then some one else will help U tommorrow when U need it.

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Do something like

    [code]
    struct Term {
    int exp;
    Coff c;
    };

    class Poly {
    public:


    private:
    list<Term> p;
    int degree;
    };

    Make sure you keep the list p with the property
    that the highest degree terms are in the front of the list and then when you add polynomials all that's required is loop and when one degree is the same as another add the term's c values. You should be able to do this in O(n).

    For multiplication, probably the easiest way would be to write a term * polynomial function and then times each term of p1 by p2.

    For eval it's possible to use Horner's algorithm.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    Dont apologize man...just because the idiots here feel like flaming you. You clearly stated why you posted before. People here like too assume your a dumb.......... before all else. They get joy like nothing else when they get to waste their time writing...THIS IS FOR A TROJAN HORSE!! NOONE HELP HIM!! DIE HACKER...or great thrills in DONT CROST POST!! IT HURTS MY EYES...I HAVE TO CLICK EVERY POST AND YOUVE WASTED MY VALUBLE SECOND...or even...THATS HOMEWORK HELLRAISER..DIE FOOL DIE!!...

    The worst part is that it;s the ones that you assumed were damn intelligent and are obviously good coders that revel in doing this the most. Not much you can do..its there stomping grounds..if they want there kicks theyll get them..but don't apolagize..thats more than they deserve.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    i almost forget the dubiously clever
    Code:
    int main (void)
    {
       cout << " I am a clever god you retard , so dont try this again
     return 0
    }
    Nothing gets these guys off more than getting to code soemthing clever like that...theres more of those code snippets on the board than anything practical...

    Ok, ok...its not that bad...but you do get the feeling that the people are are just plain ass** a good 40% of the time. I suppose we have no right expect better of you...
    Last edited by TrojanGekko; 03-01-2002 at 02:10 PM.
    ______________________
    The Gekko

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with for loop calling external function
    By lordbubonicus in forum C Programming
    Replies: 2
    Last Post: 10-13-2007, 10:54 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM