Algebra project

This is a discussion on Algebra project within the C++ Programming forums, part of the General Programming Boards category; I was thinking how do I evaluate this without using binary trees? Code: =(x+1)(x+2)(x+3) =(x+1)(x+2) ...Firstly, evaluate this =(x^2+3x+2)(x+3)...Then using ...

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

    Algebra project

    I was thinking how do I evaluate this without using binary trees?

    Code:
    =(x+1)(x+2)(x+3)
    
    
    =(x+1)(x+2)  ...Firstly, evaluate this
    
    
    =(x^2+3x+2)(x+3)...Then using the result evaluate this
    
    =x^3+3x^2+3x^2+6x+2x+6
    
    =x^3+6x^2+8x+6

    So I would be using the function recursively? Question is getting the coefficents and multiplying them together whilst at the same time remembering to add the exponents of x.

    Code:
    x^1  *  x^1  =  x^2
    Here's my updated version of add/subtract terms
    Attached Files Attached Files

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Use a stack.

    In C++, fstream objects are streams, not pointers.
    Use functions.
    Rather than
    Code:
            if (string[a]=='9')
            {
               num=9;
            } 
    // You could just do something like the following:
    if(isdigit(s[a]))
       num = s[a] - '0';
    Don't use "string" for an identifier. That's an identifier for a class in the standard library <string>.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Note that the expression =(x+1)(x+2)(x+3) has three polynomial operands and is shorthand for the expression (x + 1) * (x + 2) * (x + 3). There may be a standard polynomial class for you to use to evaluate this, though I'm not sure about that. If not, my first crack at defining a polynomial class would be something like this:

    (NOTE: I have not compiled or tested the code appearing below. It may, and probably does, have numerous bugs in it. It is intended as a presentation of a concept, not as a working example):

    Define a polynomial as a container of terms and a term as a aggregate type of coefficient and base with base as an aggregate type of variable and exponent. Then multiplication of polynomials can be accomplished by taking each term in one polynomial times each term in the other polynomial. Multiplication of terms involves multiplication of coefficients and addition of exponents of bases of the same type.
    Code:
    class Term(
    {
       int coeff;
       char var;
       int exp;
       friend Term & operator*(Term &, Term &);
       friend ostream & operator<<(ostream &, Term &);
    };
     
    Term & operator *(Term & lhs, Term & rhs)
    {
       Term result;
       result.coeff = lhs.coeff * rhs.coeff;
       result.var = lhs.var;
       result.exp = lhs.exp + rhs.exp;
       return result;
    }
     
    ostream & operator<<(ostream & os, Term & t)
    {
       os << t.coeff << t.var << '^' << t.exp;
       return os;
    }
     
    class Polynomial
    {
       vector<Term> terms;
       friend Polynomial & operator*(Polynomial &, Polynomial &);
       friend ostream & operator<<<<(ostream &, Polynomial &);
    };
    
    Polynomial & operator*(Polynomial & lhs, Polynomial & lhs)
    {
      int i, j, k = 0;
      Polynomial result
      for(i = 0; i < lhs.terms.size(); ++i)
      {
    	 for(j = 0; j < rhs.terms.size(); ++j)
    	 {
    		result[k++] = lhs[i] * rhs[j];
    	 }
      }
     
      //collect like terms here;
     
      return result;
    }
     
    ostream & operator<<(ostream & os, Polynomial & p)
    {
       int i;
       for(i = 0; i < p.terms.size() - 1; ++i)
       {
    	  os << p[i] << " + ";
    	  os << p.[i];
       }
       return os;
    }
    This is similar to the approach I've been using to evaluate first order polynomial equations for a single unknown with nested parenthesis. The trick has been to be able to tokenize the original string input such that individual terms/polynomials/operands can be identified with more complex expressions than the type described above.
    You're only born perfect.

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    elad if you could post what you have so far, I would be intrigued to view what you have done.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The attached file is my initial attempt at developing a program to solve first order polynomials with a single unknown. It doesn't use binary trees or iterators. It's overly commented as is my want. It doesn't validate input and could be spruced up a fair amount, but it served my purpose. The code is not rigorously tested, yet it seems to handle equations that don't have nested parenthesis or variable denominators. It's serving as the basis for my current project to handle those examples. Feel free to ask questions, provide constructive criticism, etc.
    Attached Files Attached Files
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 08:01 AM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  4. Algebra project
    By treenef in forum C++ Programming
    Replies: 10
    Last Post: 04-03-2005, 04:58 PM

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