-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path一元多项式相加.sln
154 lines (147 loc) · 3.26 KB
/
一元多项式相加.sln
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
struct Node { //a是系数,b是次数
int a;
int b;
struct Node*link;
};
struct Node* head1=new Node();
struct Node* head2=new Node();
struct Node* head3=new Node();
int max3; //所输入的总最大次数
void insert(Node*HEAD,int a, int b) //插入一个多项式
{
Node* temp = new Node();
temp->a = a;
temp->b = b;
temp->link = NULL;
if(HEAD==NULL)
HEAD = temp;
else
{
Node*temp1 = HEAD;
while (temp1->link != NULL)
temp1 = temp1->link;
temp1->link = temp;
}
}
void print(Node*HEAD) //打印一个多项式
{
Node* temp = HEAD;
cout << "The result of two equation add is = ";
while (temp->link!= NULL)
{
temp = temp->link;
if (temp->b != 0 && temp->a != 0)
{
if (temp->b != max3)
cout << " + ";
cout << temp->a << "x^" << temp->b;
}
else if(temp->b==0&&temp->a!=0)
cout << " + "<<temp->a << endl;
}
}
void add() //使两个多项式相加
{
Node* temp = new Node();
Node* temp1 = new Node();
temp = head3;
while (temp->link != NULL)
{
temp = temp->link;
temp1 = head1; //使temp1它回到第一个链表的头结点
while (temp1->link != NULL)
{
temp1 = temp1->link;
if (temp1->b == temp->b)
temp->a = temp->a + temp1->a;
}
}
temp = head3; //使temp回到第三个链表的头结点
while (temp->link != NULL)
{
temp = temp->link;
temp1 = head2;
while (temp1->link != NULL)
{
temp1 = temp1->link;
if (temp1->b == temp->b)
temp->a = temp->a + temp1->a;
}
}
}
void FindMax() //找所输入的次数的最大值
{
int max1, max2;
Node* first1 = head1->link;
Node* first2 = head2->link;
max1 = first1->b;
max2 = first2->b;
Node* temp = head1;
while (first1->link != NULL)
{
first1 = first1->link;
temp = temp->link;
if (first1->b > temp->b)
max1 = first1->b;
}
temp = head2;
while (first2->link != NULL)
{
first2 = first2->link;
temp = temp->link;
if (first2->b > temp->b)
max2 = first2->b;
}
//cout <<"max1 is " <<max1 << " max2 is " << max2<<endl;
if (max1 >= max2)
max3 = max1;
else
max3 = max2;
}
void main()
{
int num, p, a,b,c,d;
head1->link = NULL;
head2->link = NULL;
head3->link = NULL;
cout << "Please enter the first item number of equation : "; //创建第一个链表
cin >> num;
for (p = 0; p < num; p++)
{
cout << "Enter ( " << p + 1 << " ) coefficient = ";
cin >> a;
cout << "Enter ( " << p + 1 << " ) time = ";
cin >> b;
insert(head1,a, b);
}
//print(head1);
cout << endl<<"Please enter the second item number of equation :"; //创建第二个链表
cin >> num;
for (p = 0; p < num; p++)
{
cout << "Enter ( " << p + 1 << " ) coefficient = ";
cin >> a;
cout << "Enter ( " << p + 1 << " ) time = ";
cin >> b;
insert(head2, a, b);
}
//print(head2);
FindMax();
//cout << "max3= " << max3<<endl;
int w = max3;
for (p = 0; p <=max3; p++) //创建第三个链表
{
insert(head3, 0, w);
w = w - 1;
}
add();
cout << endl;
print(head3);
free(head1);
free(head2);
free(head3);
}