-
Notifications
You must be signed in to change notification settings - Fork 56
/
poly.cpp
84 lines (84 loc) · 1.61 KB
/
poly.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
#include<iostream>
using namespace std;
struct Node{
int coeff;
int exp;
Node* next;
};
class Poly{
Node* head;
public:
Poly(){
head=NULL;
}
void insert(int,int);
Poly operator+(Poly &p);
void display();
};
void Poly::insert(int c,int e){
Node* curr=new Node;
curr->coeff=c;
curr->exp=e;
if(head==NULL){
head=curr;
return;
}
Node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=curr;
}
Poly Poly::operator+(Poly &p){
Node* h1=head;
Poly result;
Node* h2=p.head;
while(h1!=NULL&&h2!=NULL){
if(h1->exp>h2->exp){
result.insert(h1->coeff,h1->exp);
h1=h1->next;
}
else if(h1->exp<h2->exp){
result.insert(h2->coeff,h2->exp);
}
else{
result.insert(h2->coeff+h2->exp,h1->exp);
h1=h1->next;
h2=h2->next;
}
}
while(h1!=NULL){
result.insert(h1->coeff,h1->exp);
h2=h2->next;
return result;
}
}
void Poly::display(){
Node* temp=head;
if(head==NULL){
cout<<"0"<<endl;
return;
}
while(temp->next!=NULL){
cout<<temp->coeff<<"x"<<temp->exp<<"+";
temp=temp->next;
}
cout<<temp->coeff<<"x^"<<temp->exp<<endl;
}
int main(void){
Poly p1,p2,p3;
p1.insert(2,3);
p1.insert(3,2);
p1.insert(4,1);
p1.display();
cout<<endl;
p2.insert(3,4);
p2.insert(4,5);
p2.insert(5,6);
p2.display();
cout<<endl;
p3=p1+p2;
p3.display();
cout<<endl;
return 0;
}