#include<iostream>
using namespace std;
const int MAX = 100;
struct node {
int exp; int coe;
node* next;
node() {};
node(int exp, int coe) {
this->exp = exp; this->coe = coe;
}
node(int exp, int coe,node*next) {
this->exp = exp; this->coe = coe; this->next = next;
}
};
class linklist {//带头结点的循环链表
protected:
int listsize;
node* head;
public:
linklist();//构造函数
void insert(int index,int exp, int coe);//插入
void output()const;//输出
void binsort(int range);//箱子排序使多项式以指数递减形式排序
linklist add(linklist& list1, linklist& list2);//实现两个多项式的相加
};
linklist::linklist() {
listsize = 0;
head = new node;
head->next = head;
}
void linklist::insert(int index, int exp, int coe) {
node* p = head;
for (int i = 0; i < index; i++)p = p->next;
p->next = new node(exp, coe,p->next);
listsize++;
}
void linklist::output()const {
bool isFirst = true;
for (node* p = head->next; p != head; p = p->next) {
if (p->coe != 0) {//p->coe != 0是为了输入多项式时的输出,在add环节并没有加入coe=0的节点
if (!isFirst) {//调整输出格式
if (p->coe > 0 || p->coe == 1) cout << "+";
else if (p->coe == -1 && p->exp != 0)cout << "-";
if ((p->coe == 1 or p->coe == -1)&&p->exp==0)cout << p->coe;
else if (p->coe != 1 && p->coe != -1)cout << p->coe;
}
else
if (p->exp != 0 && p->coe == -1)cout << "-";
else if (p->coe != 1) cout << p->coe;
if (p->exp != 0) {
cout << "x";
if (p->exp != 1)cout << "^" << p->exp;
}
isFirst = false;
}
}
if (isFirst) {
cout << "0"; //所有项系数都为 0,则输出 0
}
cout << endl;
}
void linklist::binsort(int range) {
node** bottom, ** top;
bottom = new node * [range + 1];
top = new node * [range + 1];
for (int i = 0; i <= range; i++)bottom[i] = NULL;
node* p = head->next;
while(p!=head){
int exp = p->exp;
int coe = p->coe;
if (bottom[exp] == NULL) {
bottom[exp] = top[exp] = p;
}
else {
top[exp]->next = p;
top[exp] = top[exp]->next;
}
p = p -> next;
}
node* y = NULL;
for (int i = range; i>=0; i--) {
if (bottom[i] != NULL) {
if (y == NULL)
head->next = bottom[i];
else y->next = bottom[i];
y = top[i];
}
}
if (y != NULL)y->next = head;
delete[]bottom;
delete[]top;
}
linklist linklist::add(linklist& list1, linklist& list2) {
linklist result;
int i = 0;
node* p1 =list1.head->next; node* p2 = list2.head->next;;
while (p1!=list1.head && p2!=list2.head) {//最高次幂一次比较并插入result
if (p1->exp == p2->exp) {
if (p1->coe + p2->coe)
result.insert(i++, p1->exp, p1->coe + p2->coe);
p1 = p1->next;
p2 = p2->next;
}
else if (p1->exp > p2->exp) {
if (p1->coe)
result.insert(i++, p1->exp, p1->coe);
p1 = p1->next;
}
else {
if (p2->coe)
result.insert(i++, p2->exp, p2->coe);
p2 = p2->next;
}
}
while (p2 != list2.head) {
result.insert(i++, p2->exp, p2->coe);
p2 = p2->next;
}
while (p1 != list1.head) {
result.insert(i++, p1->exp, p1->coe);
p1 = p1->next;
}
return result;//返回相加后得到的链表result
}
int main() {
using namespace std;
int n;
cout << "请输入创建多项式的个数:"; cin >> n;
linklist list[MAX];//默认多项式数目最大值100
int d = 0;
while (d < n) {
cout << "请输入创建第" << ++d << "个多项式的项数:";
int m; cin >> m;
int exp, coe, e = 0;
while (++e <= m) {
cout << "请输入多项式第" << e << "位的幂指数和系数:";
cin >> exp >> coe;
list[d].insert(e - 1, exp, coe);
}
list[d].binsort(100);//默认多项式各项指数在0~100之间
cout << "第" << d << "个多项式为:";
list[d].output();
}
int a1, a2;
cout << "两个多项式相加的实现\n";
cout << "请输入相加的两个多项式的编号";
cin >> a1 >> a2;
linklist result = result.add(list[a1], list[a2]);
result.output();
}
10-26
03-11
573
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-14
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交