-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpolynomial.c
More file actions
191 lines (165 loc) · 4.09 KB
/
Copy pathpolynomial.c
File metadata and controls
191 lines (165 loc) · 4.09 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct poly_node
{
float coeff;
int expx, expy, expz;
struct poly_node *link;
} POLY;
POLY *getnode();
void read_poly(int n, POLY *head);
void print_poly(POLY *head);
POLY *add_poly(POLY *h1, POLY *h2);
int COMP(POLY *t1, POLY *t2);
void attach(float cf, POLY *temp, POLY **tres);
POLY *delete_node(POLY *head, POLY *temp);
void evaluate(POLY *head);
int main()
{
int n1, n2;
POLY *head1 = getnode();
POLY *head2 = getnode();
POLY *head3 = getnode();
head1->link = head1;
head2->link = head2;
head3->link = head3;
printf("Enter number of terms in the first and second polynomial:\n");
scanf("%d%d", &n1, &n2);
printf("Enter first polynomial:\n");
read_poly(n1, head1);
printf("Enter second polynomial:\n");
read_poly(n2, head2);
printf("The first polynomial:\n");
print_poly(head1);
printf("The second polynomial:\n");
print_poly(head2);
// Adding two polynomials
head3 = add_poly(head1, head2);
printf("Resultant polynomial:\n");
print_poly(head3);
// Evaluation
printf("Evaluation of the first polynomial:\n");
evaluate(head1);
printf("\n");
}
POLY *getnode()
{
POLY *temp = (POLY *)malloc(sizeof(POLY));
return temp;
}
void read_poly(int n, POLY *head)
{
POLY *newN, *temp;
int i;
temp = head;
for (i = 0; i < n; i++)
{
newN = getnode();
printf("Enter coefficient and exponents (x, y, z):\n");
scanf("%f%d%d%d", &(newN->coeff), &(newN->expx), &(newN->expy), &(newN->expz));
temp->link = newN;
temp = temp->link;
}
temp->link = head;
}
void print_poly(POLY *head)
{
POLY *temp = head->link;
while (temp != head)
{
printf("(%2.2fx^%dy^%dz^%d) + ", temp->coeff, temp->expx, temp->expy, temp->expz);
temp = temp->link;
}
printf("\n");
}
POLY *add_poly(POLY *h1, POLY *h2)
{
POLY *temp1, *temp2;
POLY *result = getnode();
POLY *tempres = result;
double sum;
temp1 = h1->link;
while (temp1 != h1)
{
temp2 = h2->link;
while (temp2 != h2)
{
switch (COMP(temp1, temp2))
{
case 1: // Exponents are equal
sum = temp1->coeff + temp2->coeff;
if (sum)
attach(sum, temp1, &tempres);
h2 = delete_node(h2, temp2);
temp2 = h2->link;
temp1 = temp1->link;
break;
case -1: // Exponents are unequal
temp2 = temp2->link;
break;
}
}
if (temp1 != h1)
{
attach(temp1->coeff, temp1, &tempres);
temp1 = temp1->link;
}
}
temp2 = h2->link;
while (temp2 != h2)
{
attach(temp2->coeff, temp2, &tempres);
temp2 = temp2->link;
}
tempres->link = result;
return result;
}
int COMP(POLY *t1, POLY *t2)
{
if ((t1->expx == t2->expx) && (t1->expy == t2->expy) && (t1->expz == t2->expz))
return 1;
else
return -1;
}
void attach(float cf, POLY *temp, POLY **tres)
{
POLY *newN = getnode();
newN->coeff = cf;
newN->expx = temp->expx;
newN->expy = temp->expy;
newN->expz = temp->expz;
(*tres)->link = newN;
*tres = newN;
}
POLY *delete_node(POLY *head, POLY *temp)
{
POLY *prev, *pres;
prev = head;
pres = head->link;
while (pres != temp)
{
prev = pres;
pres = pres->link;
}
prev->link = pres->link;
free(temp);
return head;
}
void evaluate(POLY *head)
{
POLY *temp = head->link;
double x, y, z, sum = 0;
double tx, ty, tz;
printf("Enter values of x, y, and z:\n");
scanf("%lf%lf%lf", &x, &y, &z);
while (temp != head)
{
tx = (double)temp->expx;
ty = (double)temp->expy;
tz = (double)temp->expz;
sum += ((temp->coeff) * pow(x, tx) * pow(y, ty) * pow(z, tz));
temp = temp->link;
}
printf("Result:\n%lf\n", sum);
}