-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment_2problem_1.cpp
212 lines (155 loc) · 4.59 KB
/
Assignment_2problem_1.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
200
201
202
203
204
205
206
#include<bits\stdc++.h>
using namespace std;
class Polynomial {
public:
int *degCoeff;
int capacity;
Polynomial(){
this->degCoeff=new int[6];
this->capacity=5;
}
Polynomial (int capacity){
this->degCoeff=new int[capacity+1];
this->capacity=capacity;
}
Polynomial (Polynomial const &p){
int *newdeg=new int[p.capacity+1];
for(int i=0;i<=p.capacity;i++)
newdeg[i]=p.degCoeff[i];
this->degCoeff=newdeg;
this->capacity=p.capacity;
}
void setCoefficient(int deg,int coef){
if(deg>capacity){
int newcapacity=deg;
int *newdeg=new int[newcapacity+1];
for(int i=0;i<=capacity;i++)
newdeg[i]=degCoeff[i];
this->degCoeff=newdeg;
this->capacity=newcapacity;
degCoeff[deg]=coef;
}
else{
degCoeff[deg]=coef;
}
}
Polynomial operator+(Polynomial const &P2){
int newcap=max(this->capacity,P2.capacity);
Polynomial P3(newcap);
for(int i=0;i<=newcap;i++){
if(i<=capacity && i<=P2.capacity)
P3.degCoeff[i]=this->degCoeff[i]+P2.degCoeff[i];
else if(i<=capacity)
P3.degCoeff[i]=this->degCoeff[i];
else
P3.degCoeff[i]=P2.degCoeff[i];
}
return P3;
}
Polynomial operator-(Polynomial const &P2){
int newcap=max(this->capacity,P2.capacity);
Polynomial P3(newcap);
for(int i=0;i<=newcap;i++){
if(i<=capacity && i<=P2.capacity)
P3.degCoeff[i]=this->degCoeff[i]-P2.degCoeff[i];
else if(i<=capacity)
P3.degCoeff[i]=this->degCoeff[i];
else
P3.degCoeff[i]=-P2.degCoeff[i];
}
return P3;
}
Polynomial operator*(Polynomial const &P2){
int newcap=this->capacity+P2.capacity;
Polynomial P3(newcap);
for(int i=0;i<=this->capacity;i++){
for(int j=0;j<=P2.capacity;j++){
P3.degCoeff[i+j]+=this->degCoeff[i]*P2.degCoeff[j];
}
}
return P3;
}
void operator=(Polynomial const &p){
int *newdeg=new int[p.capacity+1];
for(int i=0;i<p.capacity;i++)
newdeg[i]=p.degCoeff[i];
this->degCoeff=newdeg;
this->capacity=p.capacity;
}
void print(){
for(int i=0;i<=this->capacity;i++){
if(degCoeff[i]!=0)
cout<<degCoeff[i]<<"x"<<i<<" ";
}
cout<<endl;
}
};
int main()
{
int count1,count2,choice;
cin >> count1;
int *degree1 = new int[count1];
int *coeff1 = new int[count1];
for(int i=0;i < count1; i++) {
cin >> degree1[i];
}
for(int i=0;i < count1; i++) {
cin >> coeff1[i];
}
Polynomial first;
for(int i = 0; i < count1; i++){
first.setCoefficient(degree1[i],coeff1[i]);
}
cin >> count2;
int *degree2 = new int[count2];
int *coeff2 = new int[count2];
for(int i=0;i < count2; i++) {
cin >> degree2[i];
}
for(int i=0;i < count2; i++) {
cin >> coeff2[i];
}
Polynomial second;
for(int i = 0; i < count2; i++){
second.setCoefficient(degree2[i],coeff2[i]);
}
cin >> choice;
Polynomial result;
switch(choice){
case 1:
result = first + second;
result.print();
break;
case 2 :
result = first - second;
result.print();
break;
case 3 :
result = first * second;
result.print();
break;
case 4 :
{
Polynomial third(first);
if(third.degCoeff == first.degCoeff) {
cout << "false" << endl;
}
else {
cout << "true" << endl;
}
break;
}
case 5 :
{
Polynomial fourth(first);
if(fourth.degCoeff == first.degCoeff) {
cout << "false" << endl;
}
else {
cout << "true" << endl;
}
break;
}
}
return 0;
}