-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 10
More file actions
188 lines (177 loc) · 3.81 KB
/
Assignment 10
File metadata and controls
188 lines (177 loc) · 3.81 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
Assignment no.:10
Title: Polynomial using a circular linked list using a menu driven program to perform addition,multiplication and evaluation.
#include<stdio.h>
#include<malloc.h>
#include<math.h>
typedef struct node
{
int power;
float coeff;
struct node *next;
}node;
node *insert(node *head,int power1,float coeff1);
node *create();
node *padd(node *p1,node *p2);
node *pmul(node *p1,node *p2);
float eval(node *p1,float x);
void print(node *head);
node *insert(node *head,int power1,float coeff1)
{
node *p,*q;
/*terms are stored in decreasing order of power*/
/*pointer is used to locate the correct place of insertion.
pointer p is used to store the address of the node created for
the current term.If a term with same power is found,coefficients
are added*/
p=(node*) malloc(sizeof(node));
p->power=power1;
p->coeff=coeff1;
p->next=NULL;
if(head==NULL) //Empty linked list
{
head=p;
head->next=head;
return(head);
}
if(power1<head->power)//lowest power,inserted at the end
{
p->next=head->next;
head->next=p;
head=p;
return(head);
}
if(power1==head->power) //add coefficients
{
head->coeff=head->coeff+coeff1;
return(head);
}
q=head;
while(q->next!=head && power1<=q->next->power) //locate the postion for insertion
q=q->next;
if(p->power==q->power)
q->coeff=q->coeff+coeff1;
else
{
p->next=q->next;
q->next=p;
}
return(head);
}
node *create()
{
int n,i,power1;
float coeff1;
node *head=NULL;
printf("\nEnter No. of Terms:");
scanf("%d",&n);
printf("\nenter a term as a tuple of (power,coefficient) : ");
for(i=1;i<=n;i++)
{
scanf("%d%f",&power1,&coeff1);
head=insert(head,power1,coeff1);
}
return(head);
}
node *padd(node *p1,node *p2)
{
node *p;
node *head=NULL;
int power;float coeff;
p=p1->next;
do //insert the first polynomial
{
head=insert(head,p->power,p->coeff);
p=p->next;
} while(p!=p1->next);
p=p2->next;
do //insert the second polynomial
{
head=insert(head,p->power,p->coeff);
p=p->next;
} while(p!=p2->next);
return(head);
}
node *pmul(node *p1,node *p2)
{
node *head1,*head2;
node *head=NULL;
head2=p2->next;
do //for every term of the second polynomial
{
head1=p1->next;
do //multiply with every term of the first polynomial
{
for(p=head1;p!=NULL;p=p->next)
head=insert(head,head1->power+head2->power,head1->coeff * head2->coeff);
head1=head1->next;
}while(head1!=p1->next);
head2=head2->next;
}while(head2!=p2->next);
return(head);
}
float eval(node *head,float x)
{
float value=0.00,t;
int r,i;
node *p;
p=head->next;
do
{r=1;
for(i=1;i<=p->power;i++)
{
t=x;
r=r*t;
}
value=value+p->coeff * r;
p=p->next;
}while(p!=head->next);
return(value);
}
void print( node *head)
{
node *p;
p=head->next;
printf("\n");
do
{
printf("6.2%fx^%d ",p->coeff,p->power);
p=p->next;
}while(p!=head->next);
}
void main()
{
node *p1,*p2,*p3;
int ch;
float value,x;
p1=p2=p3=NULL;
do
{
printf("\n1.create first polynomial\n2.create second polynomial\n3.print first polynomial\n4.print second polynomial\n5.add\n6.multiply\n7.evaluate first polynomial\n8.exit");
printf("\n enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:p1=create();
break;
case 2:p2=create();
break;
case 3:print(p1);
break;
case 4:print(p2);
break;
case 5:p3=padd(p1,p2);
print(p3);
break;
case 6:p3=pmul(p1,p2);
print(p3);
break;
case 7:
printf("\n enter the value of x");
scanf("%f",&x);
value=eval(p1,x);
printf("\n evaluated value=%6.2f",value);
break;
default:printf("\n invalid choice");
}
}while(ch!=8);
}