-
Notifications
You must be signed in to change notification settings - Fork 0
/
polycalc.cpp
644 lines (526 loc) · 17.4 KB
/
polycalc.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#include<iostream>
#include<string>
#include<windows.h>
#include<conio.h>
#include<bits/stdc++.h>
using namespace std;
//function to clear screen
void clrscr()
{
system("cls");
}
//project introductory page
void start(){
int i;
cout<<"\n\t\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb"<<endl<<endl;
cout<<"\t\t\t\t POLYNOMIAL CALCULATOR \t\t\t"<<endl<<endl<<endl;
cout<<"\t\t\t\t\t BY"<<endl<<endl<<endl;
cout<<"\t\t\t\t 20PW03 - AKSHAYA J"<<endl<<endl;
cout<<"\t\t\t\t 20PW36 - SWETHA S"<<endl<<endl<<endl;
cout<<"\t\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb"<<endl<<endl<<endl;
}
//class Polynomial is declared
class Polynomial{
private:
//a user defined data type named node is created which has coefficient and exponent
struct node {
float coeff;
int expo;
node* link;
}*start,*ptr,*ptr1,*ptr2;
public:
//member function declarations
Polynomial();
void insert_polynomial(float , int );
void display_polynomial();
void add_polynomial(Polynomial& l1, Polynomial& l2);
void sub_polynomial(Polynomial& l1, Polynomial& l2);
void scalarmul_polynomial(Polynomial& l1, float );
void multiply_polynomial(Polynomial& l1, Polynomial& l2);
void scalardiv_polynomial(Polynomial& l1, float );
void find_degree(Polynomial& l1 );
};
//default constructor
Polynomial::Polynomial()
{
start=NULL;
}
//to insert coeff and expo into a node of the polynomial
void Polynomial::insert_polynomial(float co,int ex){
struct node *N=start;
//creation of the node if no other node exists
if(N == NULL) {
N = new node;
start = N;
}
//creating a node at the end of the linked list
else {
while (N->link != NULL)
{
N = N->link;
}
N->link = new node;
N = N->link;
}
//inserting coefficient and exponent values into the newly created node
N->coeff = co;
N->expo = ex;
N->link = NULL;
}
//to display the polynomial
void Polynomial::display_polynomial()
{
ptr=start;
int x= 0;
//traversing through each node
while (ptr!=NULL) {
if (x!=0) {
if (ptr->coeff > 0)
cout<<"+";
}
if (ptr->expo != 0){
cout<<ptr->coeff<< "x^"<<ptr->expo;
}
else
cout<<ptr->coeff;
ptr=ptr->link;
x=1;
}
}
//to add the two polynomials
void Polynomial::add_polynomial(Polynomial& l1, Polynomial& l2)
{
//assigning the start of the two polynomials to ptr1 and ptr2
ptr1=l1.start;
ptr2=l2.start;
//traversing both polynomials
//ptr is the pointer of the resultant polynomial
while (ptr1!=NULL && ptr2!=NULL) {
if(start==NULL) {
start=new node;
ptr=start;
}
else {
ptr->link=new node;
ptr=ptr->link;
}
if(ptr1->expo<ptr2->expo)
{
ptr->coeff=ptr2->coeff;
ptr->expo=ptr2->expo;
ptr2=ptr2->link;
}
else if(ptr1->expo>ptr2->expo)
{
ptr->coeff=ptr1->coeff;
ptr->expo=ptr1->expo;
ptr1=ptr1->link;
}
else{
ptr->coeff=ptr1->coeff + ptr2->coeff;
ptr->expo=ptr1->expo;
ptr1=ptr1->link;
ptr2=ptr2->link;
}
}
//while there are remaining terms in the two polynomials
while (ptr1!=NULL || ptr2!=NULL)
{
if (start==NULL) {
start=new node;
ptr=start;
}
else {
ptr->link=new node;
ptr=ptr->link;
}
if(ptr1!=NULL){
ptr->coeff=ptr1->coeff;
ptr->expo=ptr1->expo;
ptr1=ptr1->link;
}
if(ptr2!=NULL)
{
ptr->coeff=ptr2->coeff;
ptr->expo=ptr2->expo;
ptr2=ptr2->link;
}
}
ptr->link=NULL;
}
//to subtract the two polynomials
void Polynomial::sub_polynomial(Polynomial& l1, Polynomial& l2)
{
//assigning the start of the two polynomials to ptr1 and ptr2
ptr1=l1.start;
ptr2=l2.start;
//traversing both polynomials
//ptr is the pointer of the resultant polynomial
while (ptr1!=NULL && ptr2!=NULL) {
if(start==NULL) {
start=new node;
ptr=start;
}
else {
ptr->link=new node;
ptr=ptr->link;
}
if(ptr1->expo<ptr2->expo)
{
ptr->coeff=-1*(ptr2->coeff);
ptr->expo=ptr2->expo;
ptr2=ptr2->link;
}
else if(ptr1->expo>ptr2->expo)
{
ptr->coeff=ptr1->coeff;
ptr->expo=ptr1->expo;
ptr1=ptr1->link;
}
else{
if(ptr1->coeff-ptr2->coeff!=0){
ptr->expo=ptr1->expo;
ptr->coeff=ptr1->coeff-ptr2->coeff;
ptr1=ptr1->link;
ptr2=ptr2->link;
}
}
}
//while there are remaining terms in the two polynomials
while (ptr1!=NULL || ptr2!=NULL)
{
if (start==NULL) {
start=new node;
ptr=start;
}
else {
ptr->link=new node;
ptr=ptr->link;
}
if(ptr1!=NULL){
ptr->coeff=ptr1->coeff;
ptr->expo=ptr1->expo;
ptr1=ptr1->link;
}
if(ptr2!=NULL)
{
ptr->coeff=-1*(ptr2->coeff);
ptr->expo=ptr2->expo;
ptr2=ptr2->link;
}
}
ptr->link=NULL;
}
//to multiply a polynomial by a scalar
void Polynomial::scalarmul_polynomial(Polynomial& l1, float k)
{
//assigning the start of the polynomial to ptr1
ptr1=l1.start;
while (ptr1!=NULL)
{
if (start==NULL) {
start=new node;
ptr=start;
}
else {
ptr->link=new node;
ptr=ptr->link;
}
if(ptr1!=NULL){
ptr->coeff=ptr1->coeff*k;
ptr->expo=ptr1->expo;
ptr1 = ptr1->link;
}
}
ptr->link=NULL;
}
//to multiply the two polynommials
void Polynomial::multiply_polynomial(Polynomial& l1, Polynomial& l2)
{
//assigning the start of the two polynomials to ptr1 and ptr2
ptr1=l1.start;
ptr2=l2.start;
//traversing both polynomials
//ptr is the pointer of the resultant polynomial
if (ptr1==NULL && ptr2==NULL)
{
cout<<"\nMultiplication of polynomial is not possible!\n"<<endl;
}
while (ptr1!=NULL && ptr2!=NULL)
{
while(ptr1!=NULL)
{
//multiplying each node of ptr1 with each and every node of ptr2
ptr2=l2.start;
while(ptr2!=NULL)
{
if(start==NULL) {
start=new node;
ptr=start;
}
else {
ptr->link=new node;
ptr=ptr->link;
}
ptr->coeff=ptr1->coeff*ptr2->coeff;
ptr->expo=ptr1->expo+ptr2->expo;
ptr2=ptr2->link;
}
ptr1=ptr1->link;
}
}
ptr->link=NULL;
}
//to divide a polynomial by a scalar
void Polynomial::scalardiv_polynomial(Polynomial& l1, float k)
{
ptr1=l1.start;
//assigning the start of the polynomial to ptr1
while (ptr1!=NULL)
{
if (start==NULL) {
start=new node;
ptr=start;
}
else {
ptr->link=new node;
ptr=ptr->link;
}
if(ptr1!=NULL){
ptr->coeff=ptr1->coeff/k;
ptr->expo=ptr1->expo;
ptr1 = ptr1->link;
}
}
ptr->link=NULL;
}
//to find the degree of the polynomial
void Polynomial::find_degree(Polynomial& l1)
{
ptr1=l1.start;
int maximum=0,c;
while (ptr1!=NULL)
{
if(ptr1->expo>maximum)
{
//to find maximum exponent by traversing and storing its coefficient
maximum=ptr1->expo;
c=ptr1->coeff;
}
ptr1=ptr1->link;
}
cout<<"The degree of the polynomial is:"<<maximum<<endl<<endl;
cout<<"The term with the highest degree is:"<<endl;
cout<<c<<"x^"<<maximum<<endl<<endl;
}
int main(){
start();
//creating instances for the class
Polynomial p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11;
string consent;
float c;
int e;
int i,choice;
for(i=0;i<2;i++){
if(i==0){
int flag=1;
cout<<"\n\nENTER FIRST POLYNOMIAL:"<<endl;
//to enter any no of the terms the user wants to input into the polyomial
cout<<"\nDo you want to insert into the polynomial?"<<endl;
cin>>consent;
while(flag!=0){
if(consent=="yes" || consent=="Yes"){
cout<<"Enter coefficient:";
cin>>c;
cout<<"Enter exponent:";
cin>>e;
p1.insert_polynomial(c,e);
cout<<"\nDo you want to insert into the polynomial?"<<endl;
cin>>consent;
if(consent=="yes" || consent=="Yes"){
flag=1;
}
else{
flag=0;
}
}
}
}
else{
clrscr();
int flag=1;
cout<<"ENTER SECOND POLYNOMIAL:"<<endl;
//to enter any no of the terms the user wants to input into the polyomial
cout<<"\nDo you want to insert into the polynomial?"<<endl;
cin>>consent;
while(flag!=0){
if(consent=="yes" || consent=="Yes"){
cout<<"Enter coefficient:";
cin>>c;
cout<<"Enter exponent:";
cin>>e;
p2.insert_polynomial(c,e);
cout<<"\nDo you want to insert into the polynomial?"<<endl;
cin>>consent;
if(consent=="yes" || consent=="Yes"){
flag=1;
}
else{
flag=0;
}
}
}
}
}
//to display the two polynomials the user has input
cout<<"\nTHE FIRST POLYNOMIAL:"<<endl;
p1.display_polynomial();
cout<<endl<<endl;
cout<<"\nTHE SECOND POLYNOMIAL:"<<endl;
p2.display_polynomial();
cout<<endl<<endl;
//menu driven program to perform the desired operations on the polynomial/polynomials
cout<<" OPERATIONS:"<<endl<<endl;
cout<<"\t1.To add the two polynomials\n"<<endl;
cout<<"\t2.To subtract the two polynomials\n"<<endl;
cout<<"\t3.To multiply a polynomial by a scalar\n"<<endl;
cout<<"\t4.To multiply the two polynomials\n"<<endl;
cout<<"\t5.To divide a polynomial by a scalar\n"<<endl;
cout<<"\t6.To find degree of a polynomial\n"<<endl;
cout<<endl;
cout<<"Enter your choice/Enter 0 to exit:"<<endl;
cin>>choice;
system("cls");
while(choice!=0){
switch(choice){
//to perform the operations
case 1:
{
//addition
cout<<"\nTHE FIRST POLYNOMIAL:"<<endl;
p1.display_polynomial();
cout<<endl<<endl;
cout<<"\nTHE SECOND POLYNOMIAL:"<<endl;
p2.display_polynomial();
cout<<endl<<endl;
cout<<"THE ADDITION OF THE TWO POLYNOMIALS = "<<endl<<endl;
p3.add_polynomial(p1,p2);
cout<<"Resultant Polynomial:"<<endl<<endl;
p3.display_polynomial();
break;
}
case 2:
{
//subtraction
cout<<"THE SUBTRACTION OF THE TWO POLYNOMIALS = "<<endl<<endl;
p4.sub_polynomial(p1,p2);
cout<<"Subtracting p1 from p2"<<endl;
cout<<"Resultant Polynomial:"<<endl<<endl;
p4.display_polynomial();
break;
}
case 3:
{
//multiplication by scalar
int k=0;
float scalar;
cout<<"Enter the scalar value:"<<endl;
cin>>scalar;
cout<<"THE MULTIPLICATION OF THE POLYNOMIAL BY THE SCALAR = "<<endl<<endl;
//asks the choice of polynomial to perform the operation on
while(k!=1 || k!=2){
cout<<"Do you want to multiply the scalar with the first polynomial or the second polynomial? Enter 1 for 1st and 2 for 2nd:";
cin>>k;
if(k==1){
p5.scalarmul_polynomial(p1,scalar);
cout<<"\nResultant Polynomial:"<<endl<<endl;
p5.display_polynomial();
break;
}
else if(k==2){
p6.scalarmul_polynomial(p2,scalar);
cout<<"\nResultant Polynomial:"<<endl<<endl;
p6.display_polynomial();
break;
}
else{
cout<<"Invalid input"<<endl;
}
}
break;
}
case 4:
{
//multiplication
cout<<"THE MULTIPLICATION OF THE TWO POLYNOMIALS = "<<endl<<endl;
p7.multiply_polynomial(p1,p2);
cout<<"Resultant Polynomial:"<<endl<<endl;
p7.display_polynomial();
break;
}
case 5:
{
//division by scalar
int k=0;
float scalar;
cout<<"Enter the scalar value:"<<endl;
cin>>scalar;
cout<<"THE DIVISION OF THE POLYNOMIAL BY THE SCALAR = "<<endl<<endl;
//asks the choice of polynomial to perform the operation on
while(k!=1 || k!=2){
cout<<"Do you want to divide the first or the second polynomial by the scalar? Enter 1 for 1st and 2 for 2nd:";
cin>>k;
if(k==1){
p8.scalardiv_polynomial(p1,scalar);
cout<<"\nResultant Polynomial:"<<endl<<endl;
p8.display_polynomial();
break;
}
else if(k==2){
p9.scalardiv_polynomial(p2,scalar);
cout<<"\nResultant Polynomial:"<<endl<<endl;
p9.display_polynomial();
break;
}
else{
cout<<"Invalid input"<<endl;
}
}
break;
}
case 6:
{
//finding degree
int k=0;
//asks the choice of polynomial to perform the operation on
while(k!=1 || k!=2){
cout<<"Do you want to find degree of the first polynomial or the second polynomial? Enter 1 for 1st and 2 for 2nd:";
cin>>k;
if(k==1){
p10.find_degree(p1);
break;
}
else if(k==2){
p11.find_degree(p2);
break;
}
else{
cout<<"Invalid input"<<endl;
}
}
break;
}
}
//repeats the menu driven program unless user exits
cout<<"\n\n OPERATIONS:"<<endl<<endl;
cout<<"\t1.To add two polynomials\n"<<endl;
cout<<"\t2.To subtract the two polynomials\n"<<endl;
cout<<"\t3.To multiply a polynomial by a scalar\n"<<endl;
cout<<"\t4.To multiply the two polynomials\n"<<endl;
cout<<"\t5.To divide a polynomial by a scalar\n"<<endl;
cout<<"\t6.To find degree of a polynomial\n"<<endl;
cout<<endl;
cout<<"\nEnter your choice/Enter 0 to exit :";
cin>>choice;
}
}