问题遇到的现象和发生背景
完成实验时,求一元多项式的乘积,遇到了一个bug
这是相关的代码
//求多项式之间的乘积
Polynomial MulPolyn(Polynomial Pa, Polynomial Pb)
{
int sum1 = 0;
int sum2 = 0;
//再建立一个链表
Polynomial Pc = new PNode;
Polynomial Pd = new PNode;
Polynomial p1 = Pa->next;
Polynomial p2 = Pb->next;
Polynomial p3 = new PNode;
Polynomial p4 = new PNode;
//进行连接
Pc->next = p3;
Pd->next = p4;
//p3只需要一个节点来存放乘积
//p4是最后的结果
while (p1)
{
while (p2)
{
sum1 = p1->coef * p2->coef;
sum2 = p1->expn + p2->expn;
//将新生成的存放到链表C中
p3->coef = sum1;
p3->expn = sum2;
//相当于add只加一个值
AddPolyn(Pd,Pc);
p2 = p2->next;
Polynomial s = new PNode;
p4->next = s;
p4 = s;
}
p1 = p1->next;
}
return Pd;
}
其中的加法函数
void AddPolyn(Polynomial& Pa, Polynomial& Pb)
{
//p1,p2分别指向Pa.Pb的第一个节点
Polynomial p1 = Pa->next;
Polynomial p2 = Pb->next;
//将p3指向p1
Polynomial p3 = Pa;
while (p1 && p2)
{
//总共存在三种情况,等于,大于和小于
if (p1->expn == p2->expn)
{
double sum = p1->coef + p2->coef;
if (sum == 0)
{
//分别删除p1,p2的第一个节点,并且移动向下一个节点
Polynomial r1 = p1;
p1 = p1->next;
delete r1;
Polynomial r2 = p2;
p2 = p2->next;
delete r2;
}
else
{
//将p1第一个节点中的指数修改为sum,然后删除p2中的当前节点,并且向下移动一位
p1->coef = sum;
//将当前节点指在p3后面
p3->next = p1;
p3 = p1;
p1 = p1->next;
//删除p2当前节点
Polynomial r = p2;
p2 = p2->next;
delete r;
}
}
else if (p1->expn < p2->expn)
{
p3->next = p1;
p3 = p1;
p1 = p1->next;
}
else
{
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
}
p3->next = p1 ? p1 : p2;
delete Pb;
}
运行结果及报错内容
我的思路是:比如说(y1+y2)(y3+y4),那么我在第一个括号中,每次循环取第一个,与第二个括号中进行相乘,依次得到各项结果.就结束了,我把pd用来存放改变,pc用来存放每一次的乘积,所以pc后面只需要跟一个链节,而pd在每一次的循环中增加一个链节,就相等于两个多项式相加了。
在我运行一遍后,会出现报错,访问权限发生冲突,会发生在加法的第二次循环中,按照我的想法,此时p1已将指空了,可是奇怪,在判定的时候又进去了,进去后,又认为不对了。
这里是全部的代码,希望看到的朋友可以帮忙检查一下,困扰好几个小时了。
#include<iostream>
using namespace std;
typedef int Status;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
//链表的结构
typedef struct PNode {
int coef;//系数
int expn;//指数
struct PNode* next;
}PNode,*Polynomial;
//多项式的创建
void CreatePolyn(Polynomial& P, int n)
{
//有序链表P,多项式总共有n项
//创建一个带有头节点的链表
P = new PNode;
P->next = NULL;
for (int i = 1; i <= n; i++)
{
//创建新的节点
Polynomial s = new PNode;
//一会在主程序中提醒用户
cin >> s->coef >> s->expn;
//用来保存前驱
Polynomial pre = P;
//将指针q指向首元节点
Polynomial q = P->next;
while (q && q->expn < s->expn)
{
pre = q;
q = q->next;
}
//后面连接
s->next = q;
//前面连接
pre->next = s;
}
}
void PrintPolyn(Polynomial P)
{
Polynomial p1 = P->next;
while (p1)
{
printf("%d(x)^%d ", p1->coef, p1->expn);
if (p1->next)
printf("+ ");
p1 = p1->next;
}
printf("\n");
}
void AddPolyn(Polynomial& Pa, Polynomial& Pb)
{
//p1,p2分别指向Pa.Pb的第一个节点
Polynomial p1 = Pa->next;
Polynomial p2 = Pb->next;
//将p3指向p1
Polynomial p3 = Pa;
while (p1 && p2)
{
//总共存在三种情况,等于,大于和小于
if (p1->expn == p2->expn)
{
double sum = p1->coef + p2->coef;
if (sum == 0)
{
//分别删除p1,p2的第一个节点,并且移动向下一个节点
Polynomial r1 = p1;
p1 = p1->next;
delete r1;
Polynomial r2 = p2;
p2 = p2->next;
delete r2;
}
else
{
//将p1第一个节点中的指数修改为sum,然后删除p2中的当前节点,并且向下移动一位
p1->coef = sum;
//将当前节点指在p3后面
p3->next = p1;
p3 = p1;
p1 = p1->next;
//删除p2当前节点
Polynomial r = p2;
p2 = p2->next;
delete r;
}
}
else if (p1->expn < p2->expn)
{
p3->next = p1;
p3 = p1;
p1 = p1->next;
}
else
{
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
}
p3->next = p1 ? p1 : p2;
delete Pb;
}
//调用减法
void MinusPolyn(Polynomial& Pa, Polynomial& Pb)
{
//加一个负的就好
Polynomial p = Pb->next;
while (p)
{
p->coef = -(p->coef);
p = p->next;
}
AddPolyn(Pa, Pb);
}
//计算多项式在X=X0处的值
double ResultPolyn(Polynomial Pa)
{
int sum = 0;
int x0;
int result;
cin >> x0;
Polynomial p = Pa->next;
while (p)
{
result = (p->coef) * pow( x0, p->expn);
sum += result;
p = p->next;
}
return sum;
}
//求多项式的导数
Status DerPolyn(Polynomial Pa)
{
Polynomial p = Pa->next;
while (p)
{
if (p->coef < 0 && p->expn == 0.5)
{
return ERROR;
}
p->coef = (p->coef) * (p->expn);
p->expn = (p->expn) - 1;
p = p->next;
}
PrintPolyn(Pa);
return OK;
}
//求多项式之间的乘积
Polynomial MulPolyn(Polynomial Pa, Polynomial Pb)
{
int sum1 = 0;
int sum2 = 0;
//再建立一个链表
Polynomial Pc = new PNode;
Polynomial Pd = new PNode;
Polynomial p1 = Pa->next;
Polynomial p2 = Pb->next;
Polynomial p3 = new PNode;
Polynomial p4 = new PNode;
//进行连接
Pc->next = p3;
Pd->next = p4;
//p3只需要一个节点来存放乘积
//p4是最后的结果
while (p1)
{
while (p2)
{
sum1 = p1->coef * p2->coef;
sum2 = p1->expn + p2->expn;
//将新生成的存放到链表C中
p3->coef = sum1;
p3->expn = sum2;
//相当于add只加一个值
AddPolyn(Pd,Pc);
p2 = p2->next;
Polynomial s = new PNode;
p4->next = s;
p4 = s;
}
p1 = p1->next;
}
return Pd;
}
int main()
{
void test1();
void test2();
test1();
test2();
return 0;
}
void test1()
{
menu();
Polynomial s1;
Polynomial s2;
CreatePolyn(s1, 2);
PrintPolyn(s1);
CreatePolyn(s2, 2);
PrintPolyn(s2);
//MinusPolyn(s1, s2);
//PrintPolyn(s1);
/*AddPolyn(s1, s2);*/
//PrintPolyn(s1);
int result = ResultPolyn(s1);
cout << "结果是:" << result<<endl;
DerPolyn(s1);
Polynomial s3 = MulPolyn(s1, s2);
PrintPolyn(s3);
}