polynomial class issues

Hello, I'm currently trying to wrap up a polynomial class project which allocates memory and all of my solutions make the cmd prompt stop working with 0 error messages aside from these

(34): warning C4018: '<' : signed/unsigned mismatch
(51): warning C4244: '=' : conversion from 'double' to 'unsigned int', possible loss of data
(92): warning C4018: '<' : signed/unsigned mismatch

as far as i know this should not prevent my program from running correctly? input much appreciated


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// FILE: poly.h
//     A polynomial has one variable x, real number coefficients, and
//     non-negative integer exponents. Such a polynomial can be viewed
//     as having the form:
//       A[n]*x^n + A[n-1]*x^(n-1) + ... A[2]*x^2 + A[1]*x + A[0]
//     where the A[n] are the real number coefficients and x^i represents
//     the variable x raised to the i power. The coefficient A[0] is
//     called the "constant" or "zeroth" term of the polynomial.
   
//   This version works by storing the coefficients in
//      a dynamic array. The coefficient for the x^k term is stored
//      in location [k] of the dynamic array. When a new term is
//      added beyond the current size of the array, then the
//      dynamic array is replaced by a new, larger array.


// MEMBER CONSTANTS

//
// CONSTRUCTOR for the polynomial class
//     POSTCONDITION: This polynomial has been create with all zero
//     coefficients, the size is set to the DEFAULT_CAPACITY, and the 
//     current degree is set to 0.

// MODIFICATION MEMBER FUNCTIONS for the polynomial class
//   void assign_coef(double coefficient, unsigned int exponent)
//     POSTCONDITION: Sets the coefficient for the specified exponent.
//
//   void reserve(unsigned int number)
//     POSTCONDITION: The size of the array for coefficients has been changed 
//     to the requested number (but not less that the size needed to store the
//     current non-zero coefficients). In effect, this guarantees that member
//     functions will not need to allocate new memory for exponents through

// CONSTANT MEMBER FUNCTIONS for the polynomial class
//   double coefficient(unsigned int exponent) const
//     POSTCONDITION: Returns coefficient at specified exponent of this
//     polynomial.
//
//   double getsize( ) const
//     POSTCONDITION: Returns size of the coef array
//
//   unsigned int degree( ) const
//     POSTCONDITION: The function returns the value of the largest exponent
//     with a non-zero coefficient.
//     If all coefficients are zero, then the function returns zero.
//
//   double eval(double x) const;
//		POSTCONDITION:  Evaluates a polynomial with a given value of x. 
//      For example, given the polynomial 3x^2-7x and a value of x to be 2,
//      the result would be 3 * (2^2) - 7 * (2^1) = 3 * 4 - 7 * 2 = 12 - 14 = -2

// NON-MEMBER BINARY OPERATORS for the polynomial Class
//   polynomial operator -(const polynomial& p1, const polynomial& p2)
//     POSTCONDITION: return-value is a polynomial with each coefficient
//     equal to the difference of the coefficients of p1 & p2 for any given
//     exponent.
//
//   polynomial operator +(const polynomial& p1, const polynomial& p2)
//     POSTCONDITION: return-value is a polynomial with each coefficient
//     equal to the sum of the coefficients of p1 & p2 for any given
//     exponent.
//
// NON-MEMBER OUTPUT FUNCTIONS for the polynomial Class
//   ostream& operator << (ostream& out, const polynomial& p)
//     POSTCONDITION: The polynomial has been printed to ostream out, which,
//     in turn, has been returned to the calling function.
//
// DYNAMIC MEMORY 
//   Since this class uses dynamic memory, the copy constructor and assignment
//   operator are written, and there is a destructor implemented. 

#ifndef POLY_H
#define POLY_H
#include <iostream>
#include <cstdlib>

using namespace std;


const unsigned int DEFAULT_CAPACITY = 10;
typedef size_t size_type;
typedef double value_type;

class polynomial
{
    public:
		// CONSTRUCTORS and DESTRUCTOR
		polynomial();
		polynomial(const polynomial& source);
		~polynomial( );

		// MODIFICATION MEMBER FUNCTIONS
		void operator =(const polynomial& source);
		void assign_coef(double coefficient, unsigned int exponent);
        void reserve(unsigned int number);
		void setcurrent_degree(int init_degree);
		void setsize(int init_size);

		// CONSTANT MEMBER FUNCTIONS
		double coefficient(unsigned int exponent) const;
		unsigned int degree( ) const;
    	double eval(double x) const;
		unsigned int getsize ( ) const;
	private:
		double *coef;                 // Pointer to the dynamic array
		unsigned int size;            // Current size of the dynamic array
		unsigned int current_degree;  // Current degree of the polynomial
};
    
     	// NON-MEMBER FUNCTIONS
		ostream& operator << (ostream& cout, const polynomial& r);
  		polynomial operator +(const polynomial& l, const polynomial& r);
    	polynomial operator -(const polynomial& l, const polynomial& r);


#endif




implementation file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
 
// FILE: poly.cpp
// CLASS IMPLEMENTED: polynomial (see poly.h for documentation)
// INVARIANT for the polynomial class:
//   1. coef points to a dynamic array with size elements.
//   2. For each k < size, the coefficient of the x^k term is 
//      stored in coef[k].
//   3. The degree of the polynomial is stored in current_degree
//      (using zero for the case of all zero coefficients).

#include <cassert>   // Provides assert 
#include <cmath>     // Provides pow
#include <iostream>  
#include <cstdlib>
#include "poly.h"

using namespace std;
//contructors n destructors
polynomial::polynomial()
{
	 current_degree = 0;
   for ( int i = 0; i <= DEFAULT_CAPACITY; i++ )
      coef[i] = 0.0;

	
}


    polynomial::polynomial(const polynomial& source)
	{
	    coef = new double[source.size];
	    size = source.size;
	    current_degree = source.current_degree;
	    for (int i = 0; i < size; i++)
	    	coef[i] = source.coef[i];
	}

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

	//mod member fns
	void polynomial::assign_coef(double coefficient, unsigned int exponent)
	{
		 coef[exponent] = coefficient;
   
 
   if ( coefficient != 0.0 && exponent > current_degree ) 
      current_degree = coefficient;

   int i = current_degree;
   while ( i >= 0 && coef[i] == 0.0  )
   {
      current_degree--;
      i--;
   }
	}
    
     void polynomial::operator =(const polynomial& source)
    {
	    double *new_coef;
	
	    if (this == &source)
	        return;
	    if (size != source.size)
	    {
			new_coef = new double[source.size];
			delete [ ] coef;
			coef = new_coef;
			size = source.size;
	    }
	    current_degree = source.current_degree;
	    for(size_t i = 0; i < size; i++)
	    	coef[i] = source.coef[i];
	    	
    }

    
    
    void polynomial::reserve(unsigned int number)
	{
		double *larger_array; 
		if (number == size) 
			return;
		if (number < current_degree)
			number = current_degree; 

		larger_array = new double[number](); 
	
		for (int i = 0; i < number; i++)
		{
			larger_array[i] = coef[i];
		}
		delete [ ] coef; 
		coef = larger_array; 
		size = number; 
	}
	
void polynomial::setcurrent_degree(int init_degree)
	{
		init_degree = current_degree; 
	}

	void polynomial::setsize(int init_size)
	{
		init_size = size;
	}
	
	// CONSTANT MEMBER FUNCTIONS

	double polynomial::coefficient(unsigned int exponent) const
	{
		return coef[exponent];
	}
		
	unsigned int polynomial::degree( ) const
	{
		{
      int d = 0;
      for ( int i = 0; i < 10; i++ )
         if ( coef[i] != 0 ) d = i;
      return d;
   }
	}
    	
	double polynomial::eval(double x) const
	{
		 double power = 1,
          result = 0;

   for (unsigned int i = 0; i <= current_degree; i++)
   {
      result += coef[i] * power;
      power *= current_degree;
   }
   return result;

	}


	unsigned int polynomial::getsize ( ) const
	{
	return size;
	}

	//nonmember fns

		
		
		ostream & operator<<(ostream & out, const polynomial & r)
		{
			 if ( r.degree( ) == -1 )
   {
      cout << 0;
      return out;
   }      
   out << r.coefficient( r.degree( ) ) << "x^" << r.degree( );
   for (int i = r.degree( )-1; i >= 0; i--)
   {
      out << " + ";
      if ( r.coefficient( i ) < 0.0 )
         out << '(' << r.coefficient( i ) << ')';
      else
         out << r.coefficient( i ); 
      out << "x^" << i;
   }

   return out;
		}
		
    	
polynomial operator+(const polynomial & l, const polynomial & r )
{
   int leftDegree = l.degree( ), 
       rightDegree = r.degree( ),
       degree;
   double sum;
       
   if ( leftDegree > rightDegree )
      degree = leftDegree;
   else
      degree = rightDegree;
      
   polynomial resPoly;
   
   for ( int i=degree; i >= 0; i-- )
   {
      sum = l.coefficient( i ) + r.coefficient( i );
       
   }

   return resPoly;
}
		
	/*	
polynomial operator-(const polynomial & l, const polynomial & r )

{

}
*/
driver program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include "poly.h"

using namespace std;

void cctest(polynomial);

int main()
{
	polynomial P1, P2, P3;
	int howmany;
	double c, x, result;
	unsigned int e;
	
	cout << "Enter how many coeffiecients the polynomial : ";
	cin >> howmany;
	while (howmany > 0)
	{
		cout << "Enter coefficient : ";
		cin >> c;
		cout << "Enter exponent : ";
		cin >> e;
		P1.assign_coef(c,e);
		howmany--;
	}
	
	cout << "Enter how many coeffiecients the polynomial : ";
	cin >> howmany;
	while (howmany > 0)
	{
		cout << "Enter coefficient : ";
		cin >> c;
		cout << "Enter exponent : ";
		cin >> e;
		P2.assign_coef(c,e);
		howmany--;
	}
	
	cout << "The first polynomial is " << P1 << endl
		 << "The second polynomial is " << P2 << endl << endl;
		 
	P3 = P2;
	cout << "Testing = operator ... " << endl
		 << "The second polynomial is " << P3 << endl << endl;

	cout << "Results of Addition : " << P1 + P2 << endl;
	// cout << "Results of Subtraction : " << P1 - P2 << endl << endl;
	
	cout << "Testing Evaluate ..." << endl;
	cout << "  Enter value of x : ";
	cin >> x;
	result = P1.eval(x);
	cout << "  The result of P1 when x is " << x << " is " << result << endl << endl;
	
	cout << "Testing copy constructor..." << endl;
	cctest(P1);
		
	return 0;
}

void cctest(polynomial P)
{
	cout << "  The first polynomial is " << P << endl << endl;
}
Topic archived. No new replies allowed.