-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_Poly_mul.c
135 lines (102 loc) · 2.43 KB
/
4_Poly_mul.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
133
134
135
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int count;
struct node
{
int co, po;
struct node *addr;
};
typedef struct node *NODE;
NODE insertend(NODE start, int co, int po)
{
NODE temp, cur;
temp = (NODE)malloc(sizeof(struct node));
temp->co = co;
temp->po = po;
temp->addr = NULL;
if (start == NULL)
return temp;
cur = start;
while (cur->addr != NULL)
cur = cur->addr;
cur->addr = temp;
return start;
}
void display(NODE start)
{
NODE temp;
if (start == NULL)
printf("\n Polynomial Empty");
else
{
temp = start;
while (temp->addr != NULL)
{
printf("%dx^%d+", temp->co, temp->po);
temp = temp->addr;
}
printf("%dx^%d\n", temp->co, temp->po);
}
}
NODE addterm(NODE res, int co, int po)
{
NODE temp, cur;
temp = (NODE)malloc(sizeof(struct node));
temp->co = co;
temp->po = po;
temp->addr = NULL;
if (res == NULL)
return temp;
cur = res;
while (cur != NULL)
{
if (cur->po == po)
{
cur->co = cur->co + co;
return res;
}
cur = cur->addr;
}
if (cur == NULL)
res = insertend(res, co, po);
return res;
}
NODE multiply(NODE poly1, NODE poly2)
{
NODE p1, p2, res = NULL;
for (p1 = poly1; p1 != NULL; p1 = p1->addr)
for (p2 = poly2; p2 != NULL; p2 = p2->addr)
res = addterm(res, p1->co * p2->co, p1->po + p2->po);
return res;
}
int main()
{
NODE poly1 = NULL, poly2 = NULL, poly;
int co, po;
int i, n, m;
printf("\nRead no of terms of first polynomial:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("\n Read CO and PO of %d term : ", i);
scanf("%d%d", &co, &po);
poly1 = insertend(poly1, co, po);
}
printf("\n First polynomial is\n");
display(poly1);
printf("\nRead no of terms of second polynomial:");
scanf("%d", &m);
for (i = 1; i <= m; i++)
{
printf("\n Read CO and PO of %d term : ", i);
scanf("%d%d", &co, &po);
poly2 = insertend(poly2, co, po);
}
printf("\n Second polynomial is\n");
display(poly2);
poly = multiply(poly1, poly2);
printf("\n Resultant polynomial is\n");
display(poly);
return 0;
}