forked from nazirmohd2006/NewRepository
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolynomial.c
More file actions
158 lines (153 loc) · 2.85 KB
/
polynomial.c
File metadata and controls
158 lines (153 loc) · 2.85 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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int coeff;
int expo;
struct node* link;
};
struct node* one=NULL;
struct node* two=NULL;
struct node* sum=NULL;
void append1()
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the next data of first polynomial");
printf("\nEnter the coefficient:");
scanf("%d",&temp->coeff);
fflush(stdin);
printf("\nEnter the expo:");
scanf("%d",&temp->expo);
fflush(stdin);
temp->link=NULL;
if(one==NULL)
one=temp;
else
{
struct node* p=one;
while(p->link!=NULL){
puts("inside loop ");
p=p->link;
}
p->link=temp;
}
}
void append2()
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the next data of second polynomial");
printf("\nEnter the coefficient:");
scanf("%d",&temp->coeff);
printf("\nEnter the expo:");
scanf("%d",&temp->expo);
temp->link=NULL;
if(two==NULL)
two=temp;
else
{
struct node* p=two;
while(p->link!=NULL)
p=p->link;
p->link=temp;
}
}
void appendsum(int a,int b)
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->coeff=a;
temp->expo=b;
temp->link=NULL;
if(sum==NULL){
sum=temp;
puts("inside 3");
}
else
{
struct node* p=sum;
while(p->link!=NULL){
puts("inside 8");
p=p->link;
}
p->link=temp;
}
}
void display()
{
struct node* temp=sum;
while(temp!=NULL)
{
printf("%dx^%d+",temp->coeff,temp->expo);
temp=temp->link;
}
}
void finalsum()
{
struct node* temp1=one;
struct node* temp2=two;
struct node* temp3=sum;
while((temp1!=NULL)&&(temp2!=NULL))
{
puts("inside 1");
if(temp1->expo==temp2->expo)
{
puts("inside 5");
appendsum(temp1->coeff+temp2->coeff,temp1->expo);
temp1=temp1->link;
temp2=temp2->link;
}
else if(temp1->expo>temp2->expo)
{
puts("inside 4");
appendsum(temp1->coeff,temp1->expo);
temp1=temp1->link;
}
else
{
puts("inside 2");
appendsum(temp2->coeff,temp2->expo);
temp2=temp2->link;
}
}
while(temp1!=NULL)
{
puts("inside 6");
temp1=temp1->link;
appendsum(temp1->coeff,temp1->expo);
}
while(temp2!=NULL)
{
puts("inside 7");
temp2=temp2->link;
appendsum(temp2->coeff,temp2->expo);
}
}
void main()
{
int choice;
printf("\nEnter the term of first polynomial");
append1();
while(1)
{
printf("\n1.Enter more");
printf("\n2.Exit");
scanf("\n%d",&choice);
if(choice==1)
append1();
else
break;
}
printf("\nEnter the term of second polynomial");
append2();
while(1)
{
printf("\n1.Enter more");
printf("\n2.Exit");
scanf("%d",&choice);
if(choice==1)
append2();
else
break;
}
finalsum();
display();
}