Thread: Polynomial class

  1. #1
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374

    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. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    43
    Quote 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. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Thirdly, there is no "string" type in Standard C++.
    You're kidding, right?

  4. #4
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote 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. <_<
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    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.
    Last edited by treenef; 11-30-2005 at 04:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM