-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.cpp
More file actions
218 lines (192 loc) · 3.72 KB
/
Polynomial.cpp
File metadata and controls
218 lines (192 loc) · 3.72 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
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
207
208
209
210
211
212
213
214
215
216
217
218
#include<iostream>
using namespace std;
class node
{
public:
int coef;
int expo;
node *next;
};
class poly
{
public:
node *head;
poly()
{
head = NULL;
}
void sortedCreate(node *temp);
void add(poly p1, poly p2);
void multiply( poly p1, poly p2);
void display();
};
void poly::sortedCreate(node *temp) //Sorting the polynomial
{
node *p;
if(head==NULL)
{
head=temp;
}
else
{
if(temp->expo > head->expo)
{
temp->next=head;
p=head;
while(p->next!=head)
{
p=p->next;
}
p->next=temp;
head=temp;
}
else
{
if(temp->expo==head->expo)
{
head->coef +=temp->coef;
}
else
{
p=head;
int f=1;
while(1)
{
if(p->expo==temp->expo)
{
p->coef=p->coef+temp->coef;
f=2;
break;
}
else if((p->expo>temp->expo && temp->expo>p->next->expo)|| p->next==head)
{
f=1;
break;
}
p=p->next;
}
if(f==1)
{
temp->next=p->next;
p->next=temp;
}
}
}
}
}
void poly::add(poly p1, poly p2) //Adding the polynomial
{
node *t1, *t2, *t3;
t1=p1.head;
t2=p2.head;
do
{
t3=new node;
t3->coef=t1->coef;
t3->expo=t1->expo;
t3->next=t3;
sortedCreate(t3);
t1=t1->next;
}while(t1!=p1.head);
do
{
t3=new node;
t3->coef=t2->coef;
t3->expo=t2->expo;
t3->next=t3;
sortedCreate(t3);
t2=t2->next;
}while(t2!=p2.head);
}
void poly::multiply(poly p1, poly p2) //Multiplication
{
node *t1, *t2, *t4;
t1=p1.head;
do
{
t2=p2.head;
do
{
t4=new node;
t4->coef=t1->coef * t2->coef;
t4->expo=t1->expo + t2->expo;
t4->next=t4;
sortedCreate(t4);
t2=t2->next;
}while(t2!=p2.head);
t1=t1->next;
}while(t1!=p1.head);
}
void poly::display() //Display
{
node *p=head;
do
{
cout<< p->coef <<"x^"<< p->expo <<" + ";
p=p->next;
}while(p->next!=head);
cout<< p->coef <<"x^"<< p->expo;
cout<<" = 0";
}
int main()
{
poly p1, p2, p3,p4;
char ch;
int choice;
node *temp;
cout<<"\nEnter polynomial 1 : \n"; //For poly 1
do{
temp=new node;
cout<<"\nEnter the coefficient -\n";
cin>>temp->coef;
temp->next=temp;
cout<<"Enter the exponent -\n";
cin>>temp->expo;
temp->next=temp;
p1.sortedCreate(temp);
cout<<"\nDo you want to add more nodes - ";
cin>>ch;
}while(ch=='y');
cout<<"\nPolynomial 1 is : ";
p1.display();
cout<<"\n------------------------------------------------------------------------------------";
cout<<"\nEnter polynomial 2 : \n"; //For poly 2
do{
temp=new node;
cout<<"\nEnter the coefficient -\n";
cin>>temp->coef;
cout<<"Enter the exponent -\n";
cin>>temp->expo;
temp->next=temp;
p2.sortedCreate(temp);
cout<<"\nDo you want to add more nodes - ";
cin>>ch;
}while(ch=='y');
cout<<"\nPolynomial 2 is : ";
p2.display();
cout<<"\n------------------------------------------------------------------------------------";
do{
cout<<"\n\n1].Addition \n2].Multiplication ";
cout<<"\n\nEnter your choice of operation - ";
cin>>choice;
switch (choice)
{
case 1:
p3.add(p1,p2);
cout<<"\nAddition is : ";
p3.display();
break;
case 2:
p4.multiply(p1,p2);
cout<<"\nMultiplication is : ";
p4.display();
break;
default:
cout<<"\nInvalid choice of operation !!";
break;
}
cout<<"\n\nDo you wants to continue - "<<endl;
cin>>ch;
}while(ch!='n');
return 0;
}