-
Notifications
You must be signed in to change notification settings - Fork 0
/
q01.c
108 lines (95 loc) · 1.9 KB
/
q01.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
/*
/* Using circular representation for a polynomial, design, develop, and
/* execute a program in C to accept two polynomials, add them, and
/* then print the resulting polynomial.
/*
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct term {
int coeff, deg;
struct term *next;
} term;
term *polyAttach(term *p, int c, int d) {
term *n = malloc(sizeof(term));
n->coeff = c;
n->deg = d;
n->next = p->next;
p->next = n;
return n;
}
void polyRead(term *p) {
int c, d;
p->next = p;
while(1) {
printf(" Enter term: ");
scanf(" %d %d", &c, &d);
p = polyAttach(p, c, d);
printf(" Again? ");
scanf("%d", &d);
if(!d) break;
}
}
void polyAdd(term *a, term *b, term *sum) {
term *ta = a, *tb = b;
a = a->next; b= b->next; sum->next = sum;
while(a != ta && b != tb) {
if(a->deg == b->deg) {
sum = polyAttach(sum, (a->coeff + b->coeff), b->deg);
a = a->next;
b = b->next;
}
else if(a->deg < b->deg) {
sum = polyAttach(sum, b->coeff, b->deg);
b = b->next;
}
else if(a->deg > b->deg) {
sum = polyAttach(sum, a->coeff, a->deg);
a = a->next;
}
}
while(a != ta) {
sum = polyAttach(sum, a->coeff, a->deg);
a = a->next;
}
while(b != tb) {
sum = polyAttach(sum, b->coeff, b->deg);
b = b->next;
}
}
void polyDisplay(term *p) {
term *tp = p; p = p->next;
while(tp != p) {
printf(" %d x^ %d", p->coeff, p->deg);
p = p->next;
if(tp != p)
printf(" +");
}
}
void polyFree(term *p) {
term *tp = p;
p = p->next;
while(p != tp) {
term *next = p->next;
free(p);
p = next;
}
}
void main() {
term a, b, sum;
printf(" Enter 1st polynomial in form <coeff deg>: \n");
polyRead(&a);
printf("\n Enter 2nd polynomial in form <coeff deg>: \n");
polyRead(&b);
polyAdd(&a, &b, &sum);
printf("\n");
polyDisplay(&a);
printf(" + ");
polyDisplay(&b);
printf("\n = ");
polyDisplay(&sum);
printf("\n\n");
polyFree(&a);
polyFree(&b);
polyFree(&sum);
}