Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-05-2004, 01:10 PM   #1 (permalink)
verdell32
Registered User
 
verdell32's Avatar
 
Join Date: Oct 2004
Posts: 35
verdell32 is on a distinguished road
Polynomial.cpp

Code:
// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"
#include <iostream>
#include <vector>

#using <mscorlib.dll>

using namespace System;

class Polynomial

	
    {
        enum { MAX_DEGREE = 100 };
        friend int main( );   // So I can do a quick test.

      public:
        Polynomial( );
        void zeroPolynomial( );
        Polynomial operator+( const Polynomial & rhs ) const;
        Polynomial operator*( const Polynomial & rhs ) const;
        void print( ostream & out ) const;
  
      private:
        vector<int> coeffArray;
        int highPower;
    };


        int max( int a, int b )
        {
             return a > b ? a : b;
        }

        Polynomial::Polynomial( ) : coeffArray( MAX_DEGREE + 1 )
        {
           zeroPolynomial( );
        }

        void Polynomial::zeroPolynomial( )
        {
            for( int i = 0; i <= MAX_DEGREE; i++ )
                coeffArray[ i ] = 0;
            highPower = 0;
        }

        Polynomial Polynomial::operator+( const Polynomial & rhs ) const
        {
            Polynomial sum;

            sum.highPower = max( highPower, rhs.highPower );
            for( int i = sum.highPower; i >= 0; i-- )
                sum.coeffArray[ i ] = coeffArray[ i ] + rhs.coeffArray[ i ];
            return sum;
        }

        Polynomial Polynomial::operator*( const Polynomial & rhs ) const
        {
            Polynomial product;

            product.highPower = highPower + rhs.highPower;
            if( product.highPower > MAX_DEGREE )
                cerr << "operator* exceeded MAX_DEGREE" << endl;
            for( int i = 0; i <= highPower; i++ )
                for( int j = 0; j <= rhs.highPower; j++ )
                    product.coeffArray[ i + j ] +=
                            coeffArray[ i ] * rhs.coeffArray[ j ];
            return product;
        }

        void Polynomial::print( ostream & out ) const
        {
            for( int i = highPower; i > 0; i-- )
                out << coeffArray[ i ] << "x^" << i << " + ";
            out << coeffArray[ 0 ] << endl;
        }

        ostream & operator<<( ostream & out, const Polynomial & rhs )
        {
            rhs.print( out );
            return out;
        }

        int main( )
        {
            Polynomial p;
            Polynomial q;

            p.highPower = 1; p.coeffArray[ 0 ] = 1; p.coeffArray[ 1 ] = 1;

            q = p + p;
            p = q * q;
            q = p + p;

            cout << q << endl;
	return 0
}

Last edited by Valmont; 11-06-2004 at 01:03 AM.
verdell32 is offline   Reply With Quote
Old 11-05-2004, 04:50 PM   #2 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
Please see my post from your other posted source code for information regarding this one as well.

Thanks!

-Ted
__________________
while(1) fork();
ender is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 08:36 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting