-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomials.cpp
199 lines (178 loc) · 5.7 KB
/
Polynomials.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
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
/*@Author
* Student Name: Esin Ece Aydın
* Student ID: 150160151
* Date: 23.03.2019
*/
#include "Polynomials.h"
#include<iostream>
#include<fstream>
using namespace std;
//function definitions here
//constructor to create an object corresponding line in the .txt
Polynomials::Polynomials(int linenumber){
ifstream file;
file.open("Polynomial.txt");
if(file){
file >> sizeP; //first element read from .txt is assigned to sizeP
}
string line;
for(int i = 0; i < linenumber; i++){//to skip not used lines
getline(file, line);
}
file >> degree; //first element of line is assigned to degree
coeff = new int[degree+1];//dynamic memory allocation for coeff[]
for(int i = 0; i < degree+1; i++){
file >> coeff[i]; //reading coefficients of polynomials from .txt
}
file.close();
}
//constructor with parameters
Polynomials::Polynomials(int d, int arr[]){
degree = d;
coeff = new int[d + 1];//dynamic memory allocation for coeff array
for(int i = 0; i< d+1; i++ ){
coeff[i] = arr[i];
}
}
//operator Overloading on + sign
void Polynomials::operator+(const Polynomials& p) const{
int d_max = p.degree;
int d_min = degree;
if(degree > p.degree){
d_max = degree;
d_min = p.degree;
}
int difference; //it used used for obtaining differences between these two object's degrees.
int arr[d_max]; //static array Decleration
for(int i = 0; i< d_max+1; i++){
arr[i] = 0; // array initiliazed with zeros
}
if(degree == p.degree){ //if these two objects' degrees are equal
for(int i = 0; i < degree + 1; i++){
arr[i] = coeff[i] + p.coeff[i];
}
}
if(degree > p.degree){ //if these two objects' degrees are not equal
difference = degree - p.degree;
for(int j = 0; j < difference; j++){
arr[j] = coeff[j];
}
for(int i = difference; i < degree + 1; i++){
arr[i] = coeff[i] + p.coeff[i-difference];
}
}
if(degree < p.degree){ //if these two objects' degrees are not equal
difference = p.degree - degree;
for(int j = 0; j < difference; j++){
arr[j] = p.coeff[j];
}
for(int i = difference; i < p.degree + 1; i++){
arr[i] = p.coeff[i] + coeff[i-difference];
}
}
Polynomials temp(d_max, arr); //constructor with parameters is invoked
cout << temp; //thanks to operator Overloading it prints temp object properly
cout << endl;
}
//operator Overloading on *
void Polynomials::operator*(const Polynomials& p) const{
int max_degree; //product of multiplication operation object's degree
max_degree = degree + p.degree;
int arr[max_degree]; //static array Decleration
for(int i = 0; i< max_degree+1; i++){
arr[i] = 0; // firstly array initiliazed with zeros
}
for(int i = 0; i < degree+1; i++){
for(int j = 0; j < p.degree+1; j++){
arr[i + j] += coeff[i] * p.coeff[j];
}
}
Polynomials temp(max_degree, arr); //constructor with parameters is invoked
cout << temp; // temp object is printed.
cout << endl;
}
//operator Overloading on << sign to print object
std::ostream &operator << (std::ostream &out, const Polynomials &p){
int d = p.degree;
int sum = p.degree;
//firstly it looks at coefficients then check their degree to print properly.
while(d != -1){
if(p.coeff[sum-d] == 1){
if(d == p.degree){
out << "x^" << d;
d--;
}
else if(d == 0){
out <<" + "<< p.coeff[sum-d];
d--;
}
else if(d == 1){
out<< " + " << "x" ;
d--;
}
else if(d > 1){
out<< " + " << "x^" << d ;
d--;
}
}//coeff == 1
else if(p.coeff[sum-d] > 1){
if(d == p.degree && d != 1){
out << p.coeff[sum-d] << "x^" << d;
d--;
}
else if(d == p.degree && d == 1){
out << p.coeff[sum-d] << "x";
d--;
}
else if(d == 0){
out<<" + " << p.coeff[sum-d];
d--;
}
else if(d == 1){
out<<" + " << p.coeff[sum-d] << "x" ;
d--;
}
else if(d > 1){
out <<" + " << p.coeff[sum-d] << "x^" << d ;
d--;
}
}//coeff > 1
else if(p.coeff[sum-d] == -1){
p.coeff[sum-d] *= -1;
if(d == 0){
out << " - " << "1";
d--;
}
else if(d == 1){
out << " - " << "x";
d--;
}
else if(d > 1){
out << " - " << "x^"<< d; }
}//coeff == -1
else if(p.coeff[sum-d] < -1){
p.coeff[sum-d] *= -1;
if(d == 0){
out << " - " << p.coeff[sum-d];
d--;
}
else if(d == 1){
out << " - " << p.coeff[sum-d] << "x";
d--;
}
else if(d > 1){
out << " - "<< p.coeff[sum-d] << "x^" << d ;
d--;
}
}//coeff < -1
else if(p.coeff[sum-d] == 0){
d--;
continue;
}//coeff == 0
}//while
return out;
}
//destructor
Polynomials::~Polynomials(){
delete[] coeff;
}