-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoly mult
More file actions
108 lines (100 loc) · 2.33 KB
/
poly mult
File metadata and controls
108 lines (100 loc) · 2.33 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
#include<stdio.h>
#include<stdlib.h>
struct node{
int coeff,expo;
struct node*link;
}
*phead,*qhead,*rhead;
struct node* Readpoly(){
struct node*new,*ptr,*head=NULL;
int n,i;
printf("enter number of terms");
scanf("%d",&n);
printf("enter coeff and expo");
for(i=1;i<=n;i++){
printf("enter (coef%d,expo%d)",i,i);
new=(struct node*)malloc(sizeof(struct node));
scanf("%d",&new->coeff);
scanf("%d",&new->expo);
new->link=NULL;
if(head==NULL){
head=new;
ptr=head;
}
else{
ptr->link=new;
ptr=new;
}
}
return(head);
}
void Displaypoly(struct node*head){
struct node*ptr;
if(head==NULL){
printf("poly is emptu");
}
else{
ptr=head;
while(ptr->link!=NULL){
printf("%dx^%d + ",ptr->coeff,ptr->expo);
ptr=ptr->link;
}
printf("%dx^%d\t ",ptr->coeff,ptr->expo);
}
}
struct node* mulpoly(){
struct node*new,*p,*q,*r,*head=NULL,*prev;
p=phead;
q=qhead;
while(p!=NULL){
while(q!=NULL){
new=(struct node*)malloc(sizeof(struct node));
new->coeff=p->coeff*q->coeff;
new->expo=p->expo+q->expo;
new->link=NULL;
if(head==NULL){
head=new;
r=head;
}
else{
r->link=new;
r=new;
}
q=q->link;
}
p=p->link;
q=qhead;
}
p=head;
while(p!=NULL){
prev=p;
q=p->link;
while(q!=NULL){
if(p->expo==q->expo){
(p->coeff=p->coeff+q->coeff);
prev->link=q->link;
free(q);
q=prev->link;
}
else{
prev=q;
q=q->link;
}
}
p=p->link;
}
return(head);
}
void main(){
printf("enter details of firsr poly");
phead=Readpoly();
printf("enter details of second poly");
qhead=Readpoly();
printf("first polynomail");
Displaypoly(phead);
printf("second polynomail");
Displaypoly(qhead);
rhead=mulpoly();
printf("resultant poly");
Displaypoly(rhead);
}