Polynomial addition

This is a discussion on Polynomial addition within the C++ Programming forums, part of the General Programming Boards category; Ok I have two strings... Code: String A = "5x + 4" String B = "3x + 2 - 5x^2" ...

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

    Polynomial addition

    Ok I have two strings...

    Code:
    String A = "5x + 4"
    String B = "3x + 2 - 5x^2"
    My task is to add string A to B i.e

    Code:
    (5x + 4) + (3x + 2 - 5x^2) = 8x + 6 - 5x^2
    and to subtract string A from B

    Code:
    (5x + 4) - (3x + 2 - 5x^2) = 2x + 2 + 5x^2
    Note the change in sign shown in red.

    I have these functions already...

    1.Split string into terms.

    Code:
      so "5x + 4" would become  "5x, +4"
    2.Stores terms into vector.
    So Vector_string_A is:

    Code:
    +--+----+
    |5x| +4 |
    +--+----+
    and vector_string_B is:

    Code:
    +--+---+------+
    |3x| +2|-5x^2 |
    +--+---+------+

    3.Get coefficient(self explanatory)
    4.Get exponent (self explanatory)

    The question is,given these functions what would be the
    simplest way to implement the addition and subtraction
    of a polynomial?

    I was thinking about using a nested for loop and
    looping through the elements in vector_string_A and vector string_B and adding coeff if the exponents are equal. However,
    I can't see a good way to do this. My mind's a blank.

    Any ideas?

    Thanks for your time.

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    ***Solved

    I found what I was lookin for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Entering in an nth degree polynomial
    By Noah in forum C Programming
    Replies: 1
    Last Post: 03-02-2006, 08:02 PM
  2. Polynomial class
    By treenef in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2005, 03:01 AM
  3. Problem with linked list addition
    By Strait in forum C++ Programming
    Replies: 4
    Last Post: 02-25-2005, 11:36 PM
  4. Polynomial
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2002, 05:00 PM
  5. Polynomial Problem
    By softcoder76 in forum C++ Programming
    Replies: 5
    Last Post: 03-01-2002, 01:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21