-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment_2.c
133 lines (114 loc) · 3.51 KB
/
assignment_2.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
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
typedef struct Term {
float coefficient;
int exponent;
struct Term* next;
} Term;
typedef struct Polynomial {
Term* header;
} Polynomial;
Term* createTerm(float coefficient, int exponent) {
Term* term = (Term*)malloc(sizeof(Term));
term->coefficient = coefficient;
term->exponent = exponent;
term->next = NULL;
return term;
}
void initPolynomial(Polynomial* polynomial) {
polynomial->header = createTerm(0.0, -1);
polynomial->header->next = polynomial->header;
}
void addTerm(Polynomial* polynomial, float coefficient, int exponent) {
Term* newTerm = createTerm(coefficient, exponent);
Term* current = polynomial->header;
while (current->next != polynomial->header && current->next->exponent > exponent) {
current = current->next;
}
newTerm->next = current->next;
current->next = newTerm;
}
void Pwrite(Polynomial* polynomial) {
Term* current = polynomial->header->next;
int firstTerm = 1;
while (current != polynomial->header) {
if (current->coefficient != 0) {
if (!firstTerm) {
if (current->coefficient > 0) {
printf(" + ");
} else {
printf(" - ");
}
}
if (fabs(current->coefficient) != 1 || current->exponent == 0) {
printf("%.2f", fabs(current->coefficient));
}
if (current->exponent > 0) {
printf("x");
if (current->exponent > 1) {
printf("^%d", current->exponent);
}
}
firstTerm = 0;
}
current = current->next;
}
if (firstTerm) {
printf("0");
}
printf("\n");
}
void Padd(Polynomial* polynomial, float coefficient, int exponent) {
Term* current = polynomial->header->next;
Term* prev = polynomial->header;
while (current != polynomial->header && current->exponent > exponent) {
prev = current;
current = current->next;
}
if (current != polynomial->header && current->exponent == exponent) {
current->coefficient += coefficient;
if (current->coefficient == 0) {
prev->next = current->next;
free(current);
}
} else {
Term* newTerm = createTerm(coefficient, exponent);
newTerm->next = current;
prev->next = newTerm;
}
}
int main() {
Polynomial result;
initPolynomial(&result);
printf("Enter polynomial A:\n");
Polynomial a;
initPolynomial(&a);
int numTermsA;
printf("Enter the number of terms in polynomial A: ");
scanf("%d", &numTermsA);
for (int i = 0; i < numTermsA; i++) {
float coefficient;
int exponent;
printf("Enter coefficient and exponent for term %d: ", i + 1);
scanf("%f %d", &coefficient, &exponent);
addTerm(&a, coefficient, exponent);
}
printf("Enter polynomial B:\n");
int numTermsB;
printf("Enter the number of terms in polynomial B: ");
scanf("%d", &numTermsB);
for (int i = 0; i < numTermsB; i++) {
float coefficient;
int exponent;
printf("Enter coefficient for term %d: ", i + 1);
scanf("%f", &coefficient);
exponent = i; // Assigning increasing exponents for the second polynomial
Padd(&result, coefficient, exponent);
}
printf("\nPolynomial A: ");
Pwrite(&a);
printf("Polynomial B: ");
Pwrite(&result);
return 0;
}