-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpoly.cpp
More file actions
144 lines (85 loc) · 2.07 KB
/
poly.cpp
File metadata and controls
144 lines (85 loc) · 2.07 KB
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
#include "MakeCodeAwsome.cpp"
struct pnode{
int coff, expo;
pnode* next;
pnode(int c, int e): coff(c), expo(e) {}
};
void insert(pnode* &head, pnode* &tail, int c, int e){
pnode *temp = new pnode(c, e);
if(head == NULL){
head = tail = temp;
}
else{
tail->next = temp;
tail = temp;
}
}
void print(pnode* head){
pnode* temp = head;
while(temp != NULL){
// deb(head);
// deb(head->coff);
// deb(head->expo);
write(temp->coff,"x^",temp->expo);
temp = temp->next;
}
// write(" now ",head);
}
int main(){
pnode *pol1 = NULL, *pol2 = NULL, *ans = NULL, *t1 = NULL, *t2 = NULL, *t3 = NULL;
char ch;
int a,b = 1;
cout << "Enter first polynomial";
while(b != 0){
read(a,b);
insert(pol1, t1, a, b);
}
cout << "Enter second polynomial";
b = 1;
while(b != 0){
read(a,b);
insert(pol2, t2, a, b);
}
t1 = pol1;
t2 = pol2;
do{
pnode *temp;
if(t1->expo > t2->expo){
temp = new pnode(t1->coff, t1->expo);
t1 = t1->next;
}
else if(t1->expo < t2->expo){
temp = new pnode(t2->coff, t2->expo);
t2 = t2->next;
}
else{
temp = new pnode(t1->coff+t2->coff, t1->expo);
t2 = t2->next;
t1 = t1->next;
}
temp->next = NULL;
if(ans == NULL)
t3 = ans = temp;
else{
t3->next = temp;
t3 = temp;
}
}while(t1 != NULL and t2 != NULL);
print(ans);
}
// struct pnode{
// int coff;
// int expo;
// pnode* next;
// pnode(int c, int ex): coff(c), expo(ex), next(NULL) {}
// void insert(pnode* &head, pnode* &tail, int c, int e){
// pnode *temp = new pnode(c, e);
// if(head == NULL){
// tail = head = temp;
// }
// else{
// tail->next = temp;
// tail = temp;
// }
// }
// };