#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
//多项式的项
typedef struct polynomial //定义struct类型数据
{
int n; //统计链表中元素个数
int coef, index; //每一项系数及指数
struct polynomial *next; //指向下一项的指针
}POLY;
POLY *Create_poly(void);
POLY *Sort_poly(POLY *head); //对多项式按升幂顺序排序
POLY *Tidy_poly(POLY *head); //对多项式合并同类项
POLY *Add_poly(POLY *, POLY *); //多项式加法
POLY *Dec_poly(POLY *, POLY *); //多项式减法
POLY *Mul_poly(POLY *, POLY *); //多项式乘法
void Print_poly(POLY *);
int main(void)
{
POLY *heada = NULL, *headb = NULL, *headsum = NULL, *headmul = NULL;
printf(“创建第一个多项式:\n”);
heada = Create_poly();
printf(“创建第二个多项式:\n”);
headb = Create_poly();
printf(“第一个多项式是:\n”);
Print_poly(heada);
printf(“第二个多项式是:\n”);
Print_poly(headb);
headsum = Add_poly(heada, headb);
printf(“两多项式相加结果是:”);
Print_poly(headsum);
headsum = Dec_poly(heada, headb);
printf(“两多项式相减结果是:”);
Print_poly(headsum);
headmul = Mul_poly(heada, headb);
printf(“两多项式相乘结果是:”);
Print_poly(headmul);
return 0;
}
//创建
POLY *Create_poly()
{
int count = 0; //数据节点个数计数
int coef = 0, index = 0;
POLY *head = NULL, *s = NULL, *r = NULL;
head = (POLY *)malloc(sizeof(POLY)); //申请新节点空间,头节点
r = head;
printf(“请输入多项式每一项的系数及其对应的指数(以0 0结束):\n”);
scanf_s(“%d%d”, &coef, &index);
while (coef != 0)
{
s = (POLY *)malloc(sizeof(POLY));
s->coef = coef, s->index = index;
r->next = s;
r = s; //移动的节点,起针的作用
count++;
scanf_s(“%d%d”, &coef, &index);
}
r->next = NULL;
head->n = count;
head = Tidy_poly(head);
return head;
}
//合并同类项
POLY *Tidy_poly(POLY *head)
{
int p2start = 0;
POLY *p1 = NULL, *p2 = NULL, *p2p = NULL;
if (head == NULL){ return NULL; } //如果传进的是空表,则返回空
if (head->next == NULL){ return head; } //如果传进的是只有一个节点的表,则直接返回此表
p1 = p2 = p2p = head;
while (p1->next != NULL)
{
while (p2->next != NULL)
{
if (p1->index == p2->index)
{
if (p2start == 0)
{
p2start = 1;
p2 = p2->next; //首次分离p2p与p2
}
else
{
p1->coef = p1->coef + p2->coef;
//然后进行删除后面相同指数的节点操作
if (p2->next == NULL)
{
p2p->next = NULL;
head->n–;
break;
}
p2 = p2->next;
p2p->next = p2;
head->n–;
}
}
else //指数不等,移动p2
{
if (p2->next == NULL){ break; }
p2p = p2;
p2 = p2->next;
}
}
if (p1->index == p2->index&&p2start == 1) //回收最后一个元素
{
p1->coef = p1->coef + p2->coef;
p2p->next = NULL;
head->n–;
}
if (p1->next == NULL)
{
head = Sort_poly(head); //排序
return head;
}
p1 = p2 = p2p = p1->next;
p2start = 0;
}
head = Sort_poly(head); //排序
return head;
}
//升幂排序
POLY *Sort_poly(POLY *head)
{
int pflag1 = 0,flag = 1;
POLY *p1 = NULL, *pMin = NULL, *p2 = NULL, *ptemp = NULL;
if (head == NULL){ return NULL; } //如果传进的是空表,则返回空
if (head->next == NULL){ return head; } //如果传进的是只有一个节点的表,则直接返回此表
p1 = pMin = ptemp = p2 = head;
while (p1->next != NULL)
{
if (pflag1 == 0)
{
pflag1 = 1;
}
else
{
ptemp = p1;
pMin = p2 = p1->next;
}
while (p2->next != NULL) //找出最小指数
{
p2 = p2->next;
if (pMin->index > p2->index) //分离pMin、p2
{
pMin = p2;
}
}
if (pMin != head) //注意起始的时候别访问越界
{
while (ptemp->next != pMin) //定位ptemp在pMin之前
{
ptemp = ptemp->next;
}
}
if (flag==1) //第一次插入的是head后
{
if (head == pMin) //是跟在head后的开头元素吗
{
p1 = head;
flag = 0;
continue;
}
else
{
//把元素剪出来,插入head后面
if (pMin->next == NULL)
{
ptemp->next = NULL;
}
else
{
ptemp->next = pMin->next;
}
pMin->next = head->next;
head->next = pMin;
p1 = head;
flag = 0;
continue;
}
}
else
{
//这里是插入在p1后
if (pMin->next == NULL)
{
ptemp->next = NULL;
}
else
{
ptemp->next = pMin->next;
}
pMin->next = p1->next;
p1->next = pMin;
p1 = pMin;
}
}
return head;
}
//相加
POLY *Add_poly(POLY *opera1, POLY *opera2)
{
POLY *head, *tmp, *s = NULL;
int value;
opera1 = opera1->next;
opera2 = opera2->next;
head = tmp = (POLY*)malloc(sizeof(POLY));
head->next = NULL;
while (opera1 &&opera2)
{
if (opera1->index == opera2->index)
{
value = opera1->coef + opera2->coef;
if (value != 0)
{
s = (POLY *)malloc(sizeof(POLY));
s->coef = value;
s->index = opera1->index;
s->next = NULL;
}
opera1 = opera1->next;
opera2 = opera2->next;
}
else
if (opera1->index <opera2->index)
{
s = (POLY *)malloc(sizeof(POLY));
s->coef = opera1->coef;
s->index = opera1->index;
s->next = NULL;
opera1 = opera1->next;
}
else
{
s = (POLY *)malloc(sizeof(POLY));
s->coef = opera2->coef;
s->index = opera2->index;
s->next = NULL;
opera2 = opera2->next;
}
if (head->next == NULL)
{
head->next = s;
tmp = s;
}
else
{
tmp->next = s;
tmp = s;
}
}
tmp->next = opera1 ? opera1 : opera2;
return head;
}
//相减,不能改变原链,多项式还有其他用
POLY *Dec_poly(POLY *head1, POLY *head2)
{
//定义为head1-head2,漏斗法
int value;
int flag = 0,flag1=1,flag2=1;
int pflag=1;
POLY *head = NULL, *pt1 = NULL, *pt2 = NULL, *phead = NULL, *s = NULL;
pt1 = head1;
pt2 = head2;
while (pt1->next != NULL&&pt2->next != NULL) //两个多项式分别多于两项,其中任一置空的都已回收
{
if (flag == 0)
{
flag = 1;
}
else
{
if (pflag == 1) //移动pt1
{
pt1 = pt1->next;
}
else if (pflag==2) //移动pt2
{
pt2 = pt2->next;
}
else if (pflag == 3) //同时移动pt1、pt2
{
pt1 = pt1->next;
pt2 = pt2->next;
}
}
if (pt1->index == pt2->index)
{
value = pt1->coef – pt2->coef;
if (value != 0)
{
s = (POLY *)malloc(sizeof(POLY));
s->coef = value; //系数
s->index = pt1->index; //指数
s->next = NULL;
if (flag1 == 1) //首次将head、phead接上第一个节点空间
{
flag1 = 0; //状态置换
head = phead = s;
pflag = 3;
}
else
{
phead->next = s;
phead = phead->next;
pflag = 3;
}
}
}
else if (pt1->index <pt2->index)
{
s = (POLY *)malloc(sizeof(POLY));
s->coef = pt1->coef;
s->index = pt1->index;
s->next = NULL;
if (flag1 == 1) //首次将head、phead接上第一个节点空间
{
flag1 = 0;
head = phead = s;
pflag = 1;
}
else
{
phead->next = s;
phead = phead->next;
pflag = 1;
}
}
else
{
value = 0 – pt2->coef;
s = (POLY *)malloc(sizeof(POLY));
s->coef = value;
s->index = pt2->index;
s->next = NULL;
if (flag1 == 1) //首次将head、phead接上第一个节点空间
{
flag1 = 0;
head = phead = s;
pflag = 2;
}
else
{
phead->next = s;
phead = phead->next;
pflag = 2;
}
}
}
//什么置空导致while退出,还是同时空(合并或者不合并,或者不作处理),回收元素
if (pt1->next == NULL&&pt2->next != NULL) //pt1置空
{
while (pt2->next != NULL)
{
if (flag2 == 1)
{
flag2 = 0;
}
else
{
pt2 = pt2->next;
}
if (flag == 0) //要回收pt1,即head==phead==NULL
{
flag = 1; //状态置换
s = (POLY *)malloc(sizeof(POLY));
s->coef = pt1->coef;
s->index = pt1->index;
s->next = NULL;
head = phead = s;
value = 0 – pt2->coef;
s = (POLY *)malloc(sizeof(POLY));
s->coef = value;
s->index = pt2->index;
s->next = NULL;
phead->next= s;
phead = phead->next;
}
else
{
if (pflag == 3) //此时的pt2不回收
{
pflag = 4; //置新状态
continue;
}
value = 0 – pt2->coef;
s = (POLY *)malloc(sizeof(POLY));
s->coef = value;
s->index = pt2->index;
s->next = NULL;
phead->next = s;
phead = phead->next;
}
}
}
else if (pt2->next== NULL&&pt1->next != NULL) //pt2置空
{
while (pt1->next != NULL)
{
if (flag2 == 1)
{
flag2 = 0;
}
else
{
pt1 = pt1->next;
}
if (flag == 0) //要回收pt2,即head==phead==NULL
{
flag = 1; //状态置换
value = 0 – pt2->coef;
s = (POLY *)malloc(sizeof(POLY));
s->coef = value;
s->index = pt2->index;
s->next = NULL;
head = phead = s;
s = (POLY *)malloc(sizeof(POLY));
s->coef = pt1->coef;
s->index = pt1->index;
s->next = NULL;
phead->next = s;
phead = phead->next;
}
else
{
if (pflag == 3) //此时的pt1不回收
{
pflag = 4; //置新状态
continue;
}
s = (POLY *)malloc(sizeof(POLY));
s->coef = pt1->coef;
s->index = pt1->index;
s->next = NULL;
phead->next = s;
phead = phead->next;
}
}
}
else //同时置空
{
if (flag == 0) //即一开始两数就仅有一项
{
if (pt1->index < pt2->index)
{
s = (POLY *)malloc(sizeof(POLY));
s->coef = pt1->coef;
s->index = pt1->index;
s->next = NULL;
head = s;
value = 0 – pt2->coef;
s = (POLY *)malloc(sizeof(POLY));
s->coef =value;
s->index = pt2->index;
s->next = NULL;
head->next = s;
}
else if (pt1->index > pt2->index)
{
value = 0 – pt2->coef;
s = (POLY *)malloc(sizeof(POLY));
s->coef = value;
s->index = pt2->index;
s->next = NULL;
head = s;
s = (POLY *)malloc(sizeof(POLY));
s->coef = pt1->coef;
s->index = pt1->index;
s->next = NULL;
head->next = s;
}
else
{
value = pt1->coef – pt2->coef; //合并
if (value != 0)
{
s = (POLY *)malloc(sizeof(POLY));
s->coef = value;
s->index = pt1->index;
s->next = NULL;
head = s;
}
}
}
//不是开始都为空,状态为运算到结尾同时为空,运算结束
}
return head;
}
//相乘
POLY *Mul_poly(POLY *op1, POLY *op2)
{
POLY *head;
POLY *t, *q, *s, *r;
head = (POLY *)malloc(sizeof(POLY));
head->next = NULL;
r = (POLY *)malloc(sizeof(POLY));
r->next = NULL;
for (t = op1->next; t; t = t->next) //双重循环,即第一项多项式每一项乘上第二项的每一项
{
for (q = op2->next; q; q = q->next)
{
s = (POLY *)malloc(sizeof(POLY));
r->next = s;
s->coef = q->coef * t->coef;
s->index = q->index + t->index;
s->next = NULL;
head = Add_poly(r, head);
}
}
return head;
}
//输出
void Print_poly(POLY *head)
{
int flag = 1;
POLY *p = NULL;
p = head->next;
if (p == NULL)
printf(“多项式为空!\n”);
else
{
do
{
if (p->coef >= 0)
{
if (flag == 1)
{
printf(“%dx^%d”, p->coef, p->index);
flag = 0;
}
else
{
printf(“+%dx^%d”, p->coef, p->index);
}
}
else
printf(“%dx^%d”, p->coef, p->index);
p = p->next;
} while (p != NULL);
printf(“\n”);
}
}