-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds10_a.cpp
113 lines (101 loc) · 2.3 KB
/
ds10_a.cpp
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
#include <stdio.h>
#include <stdlib.h>
int compare(int A, int B) {
if (A > B)
return 1;
else if (A < B)
return -1;
else
return 0;
}
typedef struct term {
float C;
int E;
struct term* nextterm;
}term;
typedef struct headnode {
term* head;
}headnode;
headnode* createpolynomiallist() {
headnode* firstnode;
firstnode = (headnode*)malloc(sizeof(headnode));
firstnode->head = NULL;
return firstnode;
}
void insertnode(headnode* head, float c, int e) {
term* newnode; term* first;
newnode = (term*)malloc(sizeof(term));
newnode->C = c;
newnode->E = e;
newnode->nextterm = NULL;
if (head->head == NULL) {
head->head = newnode;
return;
}
else {
first = head->head;
while (first->nextterm != NULL) {
first = first->nextterm;
}
first->nextterm = newnode;
}
}
headnode* ADDpolynomiallist(headnode* A, headnode* B, headnode* C) {
term* a = A->head; term* b = B->head;
float sum;
while (a && b) {
switch (compare(a->E, b->E)) {
case 1:
insertnode(C, a->C, a->E);
a = a->nextterm;
break;
case 0:
sum = a->C + b->C;
if (sum)
insertnode(C, sum, a->E);
a = a->nextterm; b = b->nextterm;
break;
case -1:
insertnode(C, b->C, b->E);
b = b->nextterm;
}
}
for (; a != NULL; a = a->nextterm)
insertnode(C, a->C, a->E);
for (; b != NULL; b = b->nextterm)
insertnode(C, b->C, b->E);
return C;
}
void printlist(headnode* N) {
term* node = N->head;
while (node != NULL) {
printf("%.0f %d ", node->C, node->E);
node = node->nextterm;
}
}
int main() {
FILE* polynomialA; FILE* polynomialB;
headnode* A = createpolynomiallist();
headnode* B = createpolynomiallist();
headnode* C = createpolynomiallist();
fopen_s(&polynomialA, "a.txt", "r");
fopen_s(&polynomialB, "b.txt", "r");
int anumber, bnumber;
fscanf_s(polynomialA, "%d", &anumber);
fscanf_s(polynomialB, "%d", &bnumber);
float coefficient; int exponent;
for (int a = 1; a <= anumber; a++) {
fscanf_s(polynomialA, "%f", &coefficient);
fscanf_s(polynomialA, "%d", &exponent);
insertnode(A, coefficient, exponent);
}
for (int b = 1; b <= bnumber; b++) {
fscanf_s(polynomialB, "%f", &coefficient);
fscanf_s(polynomialB, "%d", &exponent);
insertnode(B, coefficient, exponent);
}
C = ADDpolynomiallist(A, B, C);
printlist(C);
fclose(polynomialA); fclose(polynomialB);
return 0;
}