How to add two polynomials

Hey there, What I'm trying to do is make a function that will add two polynomials, and I don't know how to exactly fix it. My first question would be if the Polynomial operator+ function is right, or whether it needs some fixing. Any hints or advice would be appreciated. This is what I have so far:

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
/*-- Polynomial.h ---------------------------------------------------------
 Defines the monomial and polynomial classes. Polynomial is implemented
 basically as a list of monomials.
---------------------------------------------------------------------------*/

#include <iostream>

#ifndef PNOM
#define PNOM

const int CAPACITY = 1024;
//typedef int ElementType;

class Monomial {
private:
	float coef;
	int		exp;
public:
	Monomial(){};
	/*----------------------------------------------------------------------
	 Construct a Monomial object.
   -----------------------------------------------------------------------*/
	Monomial(float c,int p) { coef = c; exp = p;};
	/*----------------------------------------------------------------------
	 Construct a Monomial object with specified coeffient and exponent.
   -----------------------------------------------------------------------*/
	friend Monomial operator+ (Monomial &, Monomial &);
	/*----------------------------------------------------------------------
	 Overloading the operator+ so we can sum two monomials.
   -----------------------------------------------------------------------*/
	friend ostream & operator<< (ostream & out, const Monomial & mono);	
	/*----------------------------------------------------------------------
	 Overloading the OUTPUT operator for Monomials. 
   -----------------------------------------------------------------------*/
};

typedef Monomial ElementType;

class Polynomial
{
 public:

   Polynomial();
   /*----------------------------------------------------------------------
     Construct a List object.
   -----------------------------------------------------------------------*/

   /***** empty operation *****/
   bool empty() const;
   /*----------------------------------------------------------------------
     Check if a list is empty.
   -----------------------------------------------------------------------*/

   /***** insert and erase *****/
   void insert(ElementType item, int pos);
   /*----------------------------------------------------------------------
     Insert a value into the list at a given position.
   -----------------------------------------------------------------------*/

   void erase(int pos);
   /*----------------------------------------------------------------------
     Remove a value from the list at a given position.
   ----------------------------------------------------------------------*/

   /***** output *****/
   void display(ostream & out) const;
   /*----------------------------------------------------------------------
     Display a list.

     Precondition:  The ostream out is open. 
     Postcondition: The list represented by this List object has been
         inserted into out. 
   -----------------------------------------------------------------------*/

friend Polynomial operator+(Polynomial &, Polynomial &);
Polynomial SubstractPoly(Polynomial &);
Polynomial MultiplyPoly(Polynomial &);


 private:
   int mySize;                     // current size of list stored in myArray
   ElementType myArray[CAPACITY];  // array to store the Monomials

}; //--- end of List class

//------ Prototype of output operator
ostream & operator<< (ostream & out, const Polynomial & p);

#endif 


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
/*-- Polynomial.cpp------------------------------------------------------------
 
   This file implements List member functions.

-------------------------------------------------------------------------*/

#include <cassert>
using namespace std;

#include "Polynomial.h"


/*----------------------------------------------------------------------
 Member and friend functions for Monomial.
 -----------------------------------------------------------------------*/

Monomial operator+ (Monomial& a, Monomial& b) {
	Monomial res;
	res.coef	= a.coef + b.coef;
	res.exp	= a.exp;
	return res;
}


ostream & operator<< (ostream & out, const Monomial & mono)
{
	out << mono.coef ;
	if (mono.exp > 0) out << "x^" << mono.exp; 
	return out;
}


/*----------------------------------------------------------------------
 Member and friend functions for Polinomial.
 -----------------------------------------------------------------------*/

Polynomial::Polynomial()                 
: mySize(0)
{}


bool Polynomial::empty() const
{
   return mySize == 0;
}


void Polynomial::display(ostream & out) const
{
	for (int i = mySize-1; i >= 0 ; i--) {
		if (i != mySize-1) out << " + ";
		out << myArray[i];
	}

}


ostream & operator<< (ostream & out, const Polynomial & aList)
{
   aList.display(out);
   return out;
}


void Polynomial::insert(ElementType item, int pos)
{
   if (mySize == CAPACITY)
   {
      cerr << "*** No space for list element -- terminating "
              "execution ***\n";
      exit(1);
   }
   if (pos < 0 || pos > mySize)
   {
      cerr << "*** Illegal location to insert -- " << pos 
           << ".  List unchanged. ***\n";
      return;
   }

   // First shift array elements right to make room for item

   for(int i = mySize; i > pos; i--)
      myArray[i] = myArray[i - 1];

   // Now insert item at position pos and increase list size  
   myArray[pos] = item;
   mySize++;
}


void Polynomial::erase(int pos)
{
   if (mySize == 0)
   {
      cerr << "*** List is empty ***\n";
      return;
   }
   if (pos < 0 || pos >= mySize)
   {
      cerr << "Illegal location to delete -- " << pos
           << ".  List unchanged. ***\n";
      return;
   }

   // Shift array elements left to close the gap
   for(int i = pos; i < mySize; i++)
       myArray[i] = myArray[i + 1];

   // Decrease list size
    mySize--;
}


Polynomial operator+(Polynomial &p1, Polynomial &p2){
	Polynomial res;
	Monomial resP;
	int i = 0;
	
	if(p1.exp == p2.exp)
		resP.coef = p1.coef + p2.coef;

	for(i = 0; i < p1.mySize; i++){
		resP.myArray[i] = p1.myArray[i] + p2.myArray;
	}
	res.mySize = p1.mySize + p2.mySize;

	return res;

}


/*
Polynomial Polynomial::SubstractPolynomials(Polynomial &p){
	Polynomial res;

	for(int i = 0; i < mySize; i++){
		res.myArray[i] = myArray[i];
	}
	for(int j = 0; j < p.mySize; j++){
		res.myArray[j+i] = p.myArray[j];
	}
	res.p3 = mySize - p.mySize;

	return res;
}

Polynomial Polynomial::MultiplyPolynomials(Polynomial &p){
	Polynomial res;

	for(int i = 0; i < mySize; i++){
		res.myArray[i] = myArray[i];
	}
	for(int j = 0; j < p.mySize; j++){
		res.myArray[j+i] = p.myArray[j];
	}
	res.p3 = mySize * p.mySize;

	return res;
}*/


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
//--- Program to test Polynomial class.

#include <iostream>
using namespace std;

#include "Polynomial.h"

int main()
{
	float coefficient;
	int exp;
	Polynomial myPoly01;
	Polynomial myPoly02;
	Polynomial p03;
	
	//cout << "Please enter coefficient <ENTER> power <ENTER>" << endl;
	do  {
		cout << "Enter coefficient of monomial (or 0 to end):";
		cin >> coefficient;
		if (coefficient !=0) {
			cout << "Enter exponent of monomial:";
			cin >> exp;
			myPoly01.insert(Monomial(coefficient,exp),0); }
	} while(coefficient != 0);
	cout << myPoly01 << endl;


	cout << "\nNow create another polynomial: " << endl;
	do  {
		cout << "Enter coefficient of monomial (or 0 to end):";
		cin >> coefficient;
		if (coefficient !=0) {
			cout << "Enter exponent of monomial:";
			cin >> exp;
			myPoly02.insert(Monomial(coefficient,exp),0); }
	} while(coefficient != 0);
	cout << myPoly02 << endl;

	cout << "Adding the two polynomials together: " << endl;
	//p03 = myPoly01.AddPoly(myPoly02);
	p03 = myPoly01 + myPoly02;
    cout << p03 << endl;

	cout << "Substracting the two polynomials: " << endl;
	cout << "Multiplying the two polynomials: " << endl;
	
	system ("pause");
	return 1;
	
}
To save "some" cost of creating and constructing un-necessary Monomial objects on the stack space, you can use below style. This is documented in Scott Meyers Effective C++ and in some cases it create "less" objects.

1
2
3
Monomial operator+ (Monomial& a, Monomial& b) {
	return Monomial(a.coef + b.coef, a.exp);
}


Edit: I don't know how Monomial arithmetic works so I am just commenting on the C++ language features aspect instead.
Last edited on
Oh okay, so you don't know of any reason on why my addition function (Polynomial operator+) doesn't work?
Topic archived. No new replies allowed.