GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Oct-2006, 19:45
JKSung5295 JKSung5295 is offline
New Member
 
Join Date: Sep 2006
Posts: 13
JKSung5295 is on a distinguished road

polynomial class can't return a full dynamic array


I'm trying to create a polynomial class that represent the polynomial.
When i overload the + operator and try to return a WHOLE polynomial, it doesn't work. It only returns the first term. I tried everything to try to fix it but i dont know what to do. could someone please help me.


if i can figure out how to overload just one of the operator i should be able to do the rest of them myself. I need the function to return the whole class not just the first object in the class

this is the header file
CPP / C++ / C Code:
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <cstdlib>
#include <iostream>

namespace csci301128_polynomial
{
    class polynomial
    {

      public:
        // MEMBER CONSTANT
        static const size_t MAX_CAPACITY = 10;
        typedef double Data;

        // CONSTRUCTORS
        polynomial (size_t cap = MAX_CAPACITY);
        polynomial (const polynomial& original);

        // DESTRUCTOR
        ~polynomial();

        // MODFICATION MEMBER FUNCTIONS
        void operator = (const polynomial& original);
        void empty () { degree = 0; };
        void insert (const Data& entry, const size_t& entry_degree);

        // CONSTANT MEMBER FUNCTIONS
        std::size_t degree_poly() const { return degree; };
        polynomial operator + (const polynomial& p1);
        polynomial operator * (const Data& real_const);
        polynomial derivative ();

        // FRIEND FUNCTIONS
        friend polynomial operator * ( const double& real_const, const polynomial &or$
        friend std::istream& operator >> (std::istream& in_s, polynomial &original);
        friend std::ostream& operator << (std::ostream& out_s, const polynomial &orig$

      private:

        double *coeff;
        size_t degree;
        size_t capacity;

        // PRIVATE FUNCTIONS:
        void resize ( size_t new_cap );
    };

}
#endif



this is the implementation file

CPP / C++ / C Code:
#include <cstdlib>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <fstream>
#include "polynomial.h"
using namespace std;

namespace csci301128_polynomial
{

  polynomial::polynomial (size_t in_capacity)
  {
    coeff = new Data[in_capacity];
    capacity = in_capacity;
    degree = 0;
  }

  polynomial::polynomial (const polynomial& original)
  {
    capacity = original.capacity;
    coeff = new Data[capacity];
    degree = original.degree;
  }

  polynomial::~polynomial ( )
  {
  {
    delete [] coeff;
  }

  void polynomial::operator = (const polynomial& original)
  {
    Data *new_data;

    if ( this == &original )
      return;

    if (capacity != original.capacity)
    {
      new_data = new Data[original.capacity];
      delete [] coeff;
      coeff = new_data;
      capacity = original.capacity;
    }

    degree = original.degree;
    for (size_t i=0; i < degree+1; ++i)
    for (size_t i=0; i < degree+1; ++i)
      coeff[i] = original.coeff[i];
  }

// void empty() is an inline function

  void polynomial::insert (const Data& entry, const size_t& entry_degree)
  {
    if (entry_degree == capacity)
      resize (degree+1);
    coeff[entry_degree] = entry;
    if (entry_degree > degree)
      degree = entry_degree;
  }

// size_t degree_poly () const is an inline function

  polynomial polynomial::operator + (const polynomial& p1)
  {
  polynomial p2;
    for (size_t i = 0; i < degree+1; ++i)
      p2.coeff[i] =  p1.coeff[i] + coeff[i];

  return p2;
  }

  polynomial polynomial::operator * (const Data& real_const)
  {
    polynomial new_coeff;
    size_t i;
    for ( i = 0; i < degree+1; ++i)
      new_coeff.coeff[i] = coeff[i] * real_const;

    return new_coeff;
  }

  polynomial polynomial::derivative ()
  {
    polynomial new_coeff;
    size_t i;
    size_t temp_deg = degree;
    for ( i = degree; i = 0; --i)
    {
      new_coeff.coeff[i-1] = coeff[i] * temp_deg;
      --temp_deg;
    }
  }

  polynomial operator * (const double& real_const, const polynomial &original)
  {
  polynomial new_coeff;
  size_t i;
  for ( i=0; i < original.degree+1; ++i)
    new_coeff.coeff[i] = original.coeff[i] * real_const;

  return new_coeff;
  }

  istream& operator >> ( istream& in_s, polynomial &original )
  {
  size_t exponent;
  double coefficient;

  original.empty();
  do
  {
    cin >> coefficient >> exponent;
    original.insert(coefficient, exponent);
  } while (cin.peek() != '\n');

  return in_s;
  }

  ostream& operator << ( ostream& out_s, const polynomial& original)
  {
  size_t i;
  for (i=0; i < original.degree+1; i++)
  cout << original.coeff[i] << " " << i << " ";
  cout << endl;
  return out_s;
  }

  void polynomial::resize (size_t new_cap)
  {
    Data *new_coeff;

    if (new_cap == capacity)
      return;

    if (new_cap < degree)
      new_cap = degree;

    new_coeff = new Data[new_cap];
    for (size_t i=0; i < degree+1; ++i)
      new_coeff = coeff;
    delete [] coeff;
    coeff = new_coeff;
    capacity = new_cap;
  }
}


this is the program that uses the class
CPP / C++ / C Code:
#include <cstdlib>
#include <iomanip>
#include <cctype>
#include <iostream>
#include "polynomial.h"

using namespace std;
using namespace csci301128_polynomial;

void menu();
char get_choice();

int main()
{

polynomial p1, p2, p3, p4;
char choice;


cin >> p1;
cout << p1 << endl;;

p2 = p1;
cout << p2 << endl;
p3 = p1+p2;
cout << p3 << endl;
p4 = p1.derivative();
cout << p4;

cout << p1.degree_poly() << endl;
p1.~polynomial();
return EXIT_SUCCESS;
}


this is what i get when i run the program

csci>use-polynomial
3.5 5 2.4 9 1.3 0
1.3 0 0 1 0 2 0 3 0 4 3.5 5 0 6 0 7 0 8 2.4 9

1.3 0 0 1 0 2 0 3 0 4 3.5 5 0 6 0 7 0 8 2.4 9

2.6 0

1.6976e-314 0
9


i will appreciate any kind of help...
  #2  
Old 13-Oct-2006, 00:04
Gamer_2k4's Avatar
Gamer_2k4 Gamer_2k4 is offline
Member
 
Join Date: Apr 2005
Location: Wisconsin
Posts: 117
Gamer_2k4 will become famous soon enough

Re: polynomial class can't return a full dynamic array


Try printing out debugging messages, like "cout << p3.degree_poly();" immediately before and after the math operations. This can help you pinpoint where the problem is. Also, since you have a copy constructor, you could try using it in the + operator overload. Maybe "polynomial p2 = polynomial(this)" ("polynomial(&this)"? not sure...), then perform the addition to it. I'm not sure how much of a difference this would make, but it never hurts to try.

Gamer_2k4
  #3  
Old 13-Oct-2006, 00:19
JKSung5295 JKSung5295 is offline
New Member
 
Join Date: Sep 2006
Posts: 13
JKSung5295 is on a distinguished road

Re: polynomial class can't return a full dynamic array


i have tried what you told me to do. i did get it to somewhat work. but i didn't have much luck. i did find where the error was so, i decide to change that and then something else went wrong. If i ever did manage to get the thing to work it never return the FULL dynamic array so i really don't know what to do.
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with a console menu system BullBuchanan C++ Forum 6 20-Aug-2006 14:46
determinant algorithms Blake C++ Forum 11 13-Mar-2006 18:36
Problem with one variable bretter C++ Forum 1 16-May-2005 07:20
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
C++ file I/O CronoX C++ Forum 36 09-Mar-2004 17:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:29.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.