-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomialmultiplication.c
132 lines (132 loc) · 3.23 KB
/
polynomialmultiplication.c
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
//Polynomial multiplication
#include <stdio.h>
#include <stdlib.h>
struct node{
int coeff,expo;
struct node *link;
}*start1,*start2,*temp1,*temp2,*start3,*temp3,*head,*newNode,*t1,*t2,*X;
void create1();
void create2();
void multiply();
void create1(){
char ch;
start1=NULL;
int co,ex;
do{
head=(struct node *)malloc(sizeof(struct node));
printf("Enter coeff and expo of the term: ");
scanf("%d %d",&co,&ex);
head->coeff=co;
head->expo=ex;
head->link=NULL;
if (start1==NULL){
start1=head;
temp1=head;
}
else{
temp1->link=head;
temp1=head;
}
printf("Want to add more?(y/n): ");
scanf("%c",&ch);
scanf("%c",&ch);
}while (ch=='y');
}
void create2(){
char ch;
start2=NULL;
int co,ex;
do{
head=(struct node *)malloc(sizeof(struct node));
printf("Enter coeff and expo of the term: ");
scanf("%d %d",&co,&ex);
head->coeff=co;
head->expo=ex;
head->link=NULL;
if (start2==NULL){
start2=head;
temp2=head;
}
else{
temp2->link=head;
temp2=head;
}
printf("Want to add more?(y/n): ");
scanf("%c",&ch);
scanf("%c",&ch);
}while (ch=='y');
}
void multiply(){
temp1=start1;
temp2=start2;
start3=NULL;
int x,y;
while (temp1!=NULL){
while (temp2!=NULL){
x=temp1->coeff*temp2->coeff;
y=temp1->expo+temp2->expo;
newNode=(struct node*)malloc(sizeof(struct node));
newNode->coeff=x;
newNode->expo=y;
newNode->link=NULL;
if (start3==NULL){
start3=newNode;
temp3=newNode;
}
else{
temp3->link=newNode;
temp3=newNode;
}
temp2=temp2->link;
}
temp1=temp1->link;
temp2=start2;
}
temp3=start3;
while (temp3!=NULL){
t1=temp3;
t2=temp3->link;
while (t2!=NULL){
if (temp3->expo==t2->expo){
temp3->coeff=temp3->coeff+t2->coeff;
t1->link=t2->link;
X=t2;
t2=t2->link;
free(X);
}
else{
t1=t2;
t2=t2->link;
}
}
temp3=temp3->link;
}
printf("First Polynomial\n");
temp1=start1;
while (temp1->link!=NULL){
printf("%dx^%d+",temp1->coeff,temp1->expo);
temp1=temp1->link;
}
printf("%dx^%d\n",temp1->coeff,temp1->expo);
printf("Second Polynomial\n");
temp2=start2;
while (temp2->link!=NULL){
printf("%dx^%d+",temp2->coeff,temp2->expo);
temp2=temp2->link;
}
printf("%dx^%d\n",temp2->coeff,temp2->expo);
printf("Product polynomial\n");
temp3=start3;
while (temp3->link!=NULL){
printf("%dx%d+",temp3->coeff,temp3->expo);
temp3=temp3->link;
}
printf("%dx%d",temp3->coeff,temp3->expo);
}
void main(){
printf("Enter first polynomial \n");
create1();
printf("Enter second polynomial \n");
create2();
multiply();
}