-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab6_1.c
136 lines (136 loc) · 3.23 KB
/
lab6_1.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
//WAP to perform addition and multiplication of polynomials
#include <stdio.h>
#include <stdlib.h>
struct node
{
int co, ex;
struct node *next;
} * first1, *first2, *first3;
struct node *create(struct node *, int, int);
void display(struct node *);
void add();
void mul();
int main()
{
first1 = NULL;
first2 = NULL;
first3 = NULL;
int n, c, e;
printf("Enter the number of nodes for first polynomial \n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter coeficient and exponent \n");
scanf("%d%d", &c, &e);
first1 = create(first1, c, e);
}
printf("Enter the number of nodes for second polynomial \n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter coeficient and exponent \n");
scanf("%d%d", &c, &e);
first2 = create(first2, c, e);
}
printf("Displaying the first polynomial: \n");
display(first1);
printf("Displaying the second polynomial: \n");
display(first2);
add();
mul();
return 0;
}
struct node *create(struct node *first, int c, int e)
{
struct node *ptr, *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->co = c;
temp->ex = e;
if (first == NULL || e > first->ex)
{
temp->next = first;
first = temp;
}
else
{
ptr = first;
while (ptr->next != NULL && ptr->next->ex > e)
ptr = ptr->next;
temp->next = ptr->next;
ptr->next = temp;
}
return (first);
}
void display(struct node *ptr)
{
while (ptr != NULL)
{
if (ptr->ex != 0)
printf("%dx^%d", ptr->co, ptr->ex);
else
printf("%d", ptr->co);
ptr = ptr->next;
if (ptr != NULL)
{
if (ptr->co > 0)
printf("+");
}
else
printf("\n");
}
}
void add()
{
struct node *ptr1, *ptr2;
ptr1 = first1;
ptr2 = first2;
while (ptr1 != NULL && ptr2 != NULL)
{
if (ptr1->ex > ptr2->ex)
{
first3 = create(first3, ptr1->co, ptr1->ex);
ptr1 = ptr1->next;
}
else if (ptr1->ex < ptr2->ex)
{
first3 = create(first3, ptr2->co, ptr2->ex);
ptr2 = ptr2->next;
}
else
{
first3 = create(first3, ptr1->co + ptr2->co, ptr2->ex);
ptr2 = ptr2->next;
ptr1 = ptr1->next;
}
}
while(ptr1!=NULL)
{
first3 = create(first3, ptr1->co, ptr1->ex);
ptr1 = ptr1->next;
}
while(ptr2!=NULL)
{
first3 = create(first3, ptr2->co, ptr2->ex);
ptr2 = ptr2->next;
}
printf("resultant polynomial after addition: \n");
display(first3);
}
void mul()
{
struct node* ptr1,*ptr2;
first3=NULL;
ptr1 = first1;
while (ptr1 != NULL)
{
ptr2 = first2;
while(ptr2!=NULL)
{
first3=create(first3,ptr1->co*ptr2->co,ptr1->ex+ptr2->ex);
ptr2=ptr2->next;
}
ptr1=ptr1->next;
}
printf("polynomial after multiplication: \n");
display(first3);
}