-
Notifications
You must be signed in to change notification settings - Fork 2
/
poly_add.c
153 lines (145 loc) · 3.13 KB
/
poly_add.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct poly
{
int coeff;
int power;
struct poly *next;
};
void create();
void display(struct poly *list);
void addpoly();
struct poly *list1=NULL,*list2=NULL,*add=NULL;
int main()
{
create();
printf("\n\t\tEquation 1\n");
display(list1);
printf("\n\n\t\tEquation 2\n");
display(list2);
addpoly();
printf("\n\n\t\tAnswer \n");
display(add);
}
void create()
{
int power,coeff,num,i;
struct poly *temp,*ptr;
printf("\n\t\tEquation 1");
printf("\nEnter the number of terms:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
temp=(struct poly*)malloc(sizeof(struct poly));
printf("\nEnter the power:");
scanf("%d",&power);
printf("\nEnter the coefficient:");
scanf("%d",&coeff);
temp->coeff=coeff;
temp->power=power;
temp->next=NULL;
if(list1==NULL)
{
list1=temp;
}
else
{
ptr=list1;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
}
}
printf("\n\t\t\t Successfully Created 1\n");
printf("\n\t\tEquation 2");
printf("\nEnter the number of terms:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
temp=(struct poly*)malloc(sizeof(struct poly));
printf("\nEnter the power:");
scanf("%d",&power);
printf("\nEnter the coefficient:");
scanf("%d",&coeff);
temp->coeff=coeff;
temp->power=power;
temp->next=NULL;
if(list2==NULL)
{
list2=temp;
}
else
{
ptr=list2;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
}
}
printf("\n\t\t\t Successfully Created 2\n");
}
void display(struct poly *list)
{
struct poly *ptr;
ptr=list;
while(ptr!=NULL)
{
printf(" %d x^%d ",ptr->coeff,ptr->power);
ptr=ptr->next;
if(ptr!=NULL)
printf("+");
}
}
void addpoly()
{
struct poly *temp1,*temp2,*temp;
temp1=list1;
temp2=list2;
temp=(struct poly*)malloc(sizeof(struct poly));
add=temp;
temp->next=NULL;
while((temp1!=NULL) || (temp2!=NULL))
{
if(temp1==NULL)
{
temp->coeff=temp2->coeff;
temp->power=temp2->power;
temp2=temp2->next;
goto createnode;
}
if(temp2==NULL)
{
temp->coeff=temp1->coeff;
temp->power=temp1->power;
temp1=temp1->next;
goto createnode;
}
if(temp1->power==temp2->power)
{
temp->coeff=temp1->coeff + temp2->coeff ;
temp->power=temp1->power;
temp1=temp1->next;
temp2=temp2->next;
}
else if((temp1->power) > (temp2->power))
{
temp->coeff=temp1->coeff;
temp->power=temp1->power;
temp1=temp1->next;
}
else
{
temp->coeff=temp2->coeff;
temp->power=temp2->power;
temp2=temp2->next;
}
createnode:
if((temp1 != NULL) || (temp2 != NULL))
{
temp->next=(struct poly*)malloc(sizeof(struct poly));
temp=temp->next;
temp->next=NULL;
}
}
}