-
Notifications
You must be signed in to change notification settings - Fork 2
/
ques2.h
93 lines (86 loc) · 2.45 KB
/
ques2.h
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
#ifndef __QUES2_H__
#define __QUES2_H__
#include <stdio.h>
#include <stdlib.h>
#define ParamType double //系数的数据类型
struct equationNode {//多项式每一项
int power = 0;//阶数
ParamType param = 0;//系数
equationNode* next = NULL;
};
struct equation {//多项式
int nodeNum = 0;//项数
equationNode* minPow = NULL, * maxPow = NULL;//指向最低阶的节点和最高阶的节点
void InsertNode(ParamType param, int power) {
equationNode* newNode = (equationNode*)malloc(sizeof(equationNode)); newNode->param = param; newNode->power = power; newNode->next = NULL;
if (nodeNum == 0) {//长度为0
maxPow = newNode;
minPow = newNode;
}
else {//长度不为0
if (power < minPow->power) {//比较是否比最小值小
newNode->next = minPow;
minPow = newNode;
}
else if (power > maxPow->power) {//比较是否比最大值大
maxPow->next = newNode;
maxPow = newNode;
}
else {
for (equationNode* pointer = minPow; pointer != NULL; pointer = pointer->next) {
if (pointer->power == power) {
pointer->param += param;
nodeNum--;//抵消后面nodeNum++的影响(因为这里实际上是同幂节点的合并)
break;
}
else if (power > pointer->power && power < pointer->next->power) {//遍历寻找合适的位置
newNode->next = pointer->next;
pointer->next = newNode;
break;
}
}
}
}
nodeNum++;
}
void ShowEquation() {
equationNode* nodePointer = minPow;
printf("当前多项式为:\n");
for (int i = 0; i < nodeNum; i++) {
if (i != 0) printf(nodePointer->param > 0 ? " + " : " - ");
printf("%5.2lf", nodePointer->param > 0 ? nodePointer->param : -(nodePointer->param));
printf(nodePointer->power == 0 ? "" : "x^%d", nodePointer->power);
nodePointer = nodePointer->next;
}
printf("\n");
}
void MergeEquation(equation& Ea, equation& Eb, equation& Ec) {
equationNode* pointer = Ea.minPow;
while (pointer != NULL) {//遍历多项式a,向其中添加
Ec.InsertNode(pointer->param, pointer->power);
pointer = pointer->next;
}
pointer = Eb.minPow;
while (pointer != NULL) {//遍历多项式b,向其中添加
Ec.InsertNode(pointer->param, pointer->power);
pointer = pointer->next;
}
}
};
int ques2_demo() {
equation ea, eb, ec;
ea.InsertNode(2.13, 4);
ea.InsertNode(-14.3, 3);
ea.InsertNode(6.28, 2);
ea.InsertNode(3.14, 0);
ea.ShowEquation();
eb.InsertNode(3.14, 4);
eb.InsertNode(6.28, 3);
eb.InsertNode(-14.3, 2);
eb.InsertNode(2.13, 0);
eb.ShowEquation();
ec.MergeEquation(ea, eb, ec);
ec.ShowEquation();
return 0;
}
#endif