Polynomial operators + - * =

Hi everyone
I posted some questions last day and I got very useful helps.
But unfortunately my questions are not finished.
My Prof gave me the .h file and main function, I should write .cpp file.
It is about defining polynomials using struct and classes, and using overload operators to add, subtract, Multiply and setting equal.

The main problems are operators.

As you see I have the size of polynomial's terms in second element of txt file, but I don't know how to benefit from it

See my .cpp for questions in comment
.h is not allowed to be change

.h
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
 #if !defined _DYNAMICARRAY_H_
#define _DYNAMICARRAY_H_
#include <iostream>
using namespace std;

typedef struct Node
{
	double  cof;      // coefficient 
	int     deg;      // degree
} Node;               // the node of polynomial

class CPolynomial
{
private:
	list<Node> m_Polynomial;

public:
	CPolynomial();
	CPolynomial(const string& file);
	CPolynomial(double *cof, double *deg, int n);
	CPolynomial(const vector<double>& cof, const  vector<double> & deg);
	virtual ~CPolynomial();

	// overload
	CPolynomial operator+(const CPolynomial &right)
	CPolynomial operator-(const CPolynomial &right);	//Overload operator -
	CPolynomial operator*(const CPolynomial &right);	//Overload operator *
	Cpolynomial& operator=(const CPolynomial &right);	//Overload operator =

	void Print() const;
private:
	void ReadFromFile(string file);
	void AddOneTerm(Node term);   // add one term into m_Polynomial
};




#endif // _DYNAMICARRAY_H_ 




.cpp

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
CPolynomial::CPolynomial()
{
	Node term;
	term.cof = 0;
	term.deg = 0; 
	AddOneTerm(term);
}
CPolynomial::CPolynomial(const string& file)
{
	fstream MyFile;
	string p;
	int num;
	MyFile.open(file);
	MyFile >> p >> num;   //num= second element in text. terms size
	cout << "p=" << num;
	ReadFromFile(file);
}

CPolynomial::CPolynomial::CPolynomial(double *cof, double *deg, int n)
{
	Node term;

	for (int i = 0; i < n; i++)
	{
		term.cof = *cof;
		term.deg = *deg;

		AddOneTerm(term);
	}
}

CPolynomial::CPolynomial(const vector<double>& cof, const  vector<double> & deg)
{
	Node term;

	if (cof.size() != deg.size())
	{
		cout << "Invalidate input!" << endl;
	}
	else
	{
		for (int i = 0; i < cof.size(); i++)
		{
			term.cof = cof[i];
			term.deg = deg[i];
			AddOneTerm(term);
		}
	}

}

CPolynomial::~CPolynomial()
{

	m_Polynomial.clear();
}

CPolynomial operator+(const CPolynomial &right)   //There are tons of methods for these operands 
{                                                 // but I can't underestand which one is suitable for my case

	for (int i = 0; !EOF; i++)
	{
		if (deg.right = deg.term)
		{
			this->m_Polynomial += right.m_Polynomial;		//error for using this and right.m_Polynomial and deg.right
		}
		else
		{
			this->m_Polynomial
		}
	}
	return *this
}

CPolynomial operator-(const CPolynomial &right)
{

	for (int i = 0; !EOF; i++) 
	{
		if (deg.right = deg.term && deg.right != NULL)
		{
			this->m_Polynomial -= right.m_Polynomial;
		}
		else
		{
			this->m_Polynomial;
		}
	}
	return *this;

}

CPolynomial operator*(const CPolynomial &right)
{
	this->deg += right.deg;
	this->cof *= right.cof;

	free(this->cof);

	return *this;
}


Cpolynomial& operator=(const CPolynomial &right)
{
	this->m_polynominal = right.m_polynominal;
	return *this;
}


void CPolynomial::Print() const
{
	for (int i = 0; !EOF; i++)
	{
		if (term.cof[i] > 0 && term.deg[i] != 0)
		{
			cout << "+" << term.cof[i] << "x^" << term.deg[i];
		}
		if (term.cof[i] > 0 && term.deg[i] == 0)
		{
			cout << "+" << term.cof[i];
		}
		if (term.cof[i] < 0 && term.deg[i] != 0)
		{
			cout << cof[i] << "x^" << term.deg[i];
		}
		if (term.of[i] < 0 && term.deg[i] == 0)
		{
			cout << term.cof[i];
		}
	}
	cout << endl;
}

void CPolynomial::ReadFromFile(string file)
{
	Node term;
	for (int i = 0; !EOF; i++)
	{
		MyFile >> term.deg >> term.cof;
		AddOneTerm(term);
	}
}

void CPolynomial::AddOneTerm(Node term)
{
	m_Polynomial.push_back(term);
}


main

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
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <vector>
#include "CPolynomial.h"
using namespace std;



int main()
{
CPolynomial p1("P3.txt");
CPolynomial p2("P4.txt");
CPolynomial p3;
p1.Print();
p2.Print();

p3=p1+p2;
p3.Print();
p3=p1-p2;
p3.Print();

p3=p1*p2;
p3.Print();

return 0;
}


sample txt file. left column= degrees, right= coefficient, first element of second column is the number of terms
1
2
3
4
5
6
7
8
9
P 8
0 2
5 -3
12 5
2 6
5 7
3 -4
2 9
2 2

Last edited on
What are you supposed to do with the data in the file?
Can you post the complete assignment?
I edited my question, thanks for checking
@Thomas1965
Please don't open a second thread for the same problem.
http://www.cplusplus.com/forum/beginner/194257/

Topic archived. No new replies allowed.