-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aecc76b
commit f6bb93a
Showing
1 changed file
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
#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; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|