c - 多项式链表测试用例,不添加任何错误,不做任何事情
问题描述
我们的这部分代码旨在创建两个要添加的多项式。主要测试这是否成功完成。当我构建时没有错误,但运行时没有任何反应。我不确定为什么会发生这种情况,我们已经被困了很长时间。当 createPoly 函数被注释掉时,它似乎运行并打印空白多项式,所以我们不确定出了什么问题。
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link{
int coeff;
int pow;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL, *polyAddC=NULL;
void createPoly1(struct link *node)
{
node->coeff = 3;
node->pow = 0;
node=node->next;
node->next=NULL;
node->coeff = 4;
node->pow = 1;
node=node->next;
node->next=NULL;
node->coeff = 6;
node->pow = 2;
node=node->next;
node->next=NULL;
node->coeff = 7;
node->pow = 3;
}
void createPoly2(struct link *node)
{
node->coeff = 4;
node->pow = 0;
node=node->next;
node->next=NULL;
node->coeff = 5;
node->pow = 1;
node=node->next;
node->next=NULL;
node->coeff = 1;
node->pow = 2;
node=node->next;
node->next=NULL;
node->coeff = 3;
node->pow = 3;
}
void createPolyAddCorrect(struct link *node)
{
node->coeff = 7;
node->pow = 0;
node=node->next;
node->next=NULL;
node->coeff = 9;
node->pow = 1;
node=node->next;
node->next=NULL;
node->coeff = 7;
node->pow = 2;
node=node->next;
node->next=NULL;
node->coeff = 10;
node->pow = 3;
}
void displayPoly(struct link *node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
void addPoly(struct link *poly1,struct link *poly2,struct link *poly)
{
while(poly1->next && poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
int main()
{
poly1=(struct link *)malloc(sizeof(struct link));
poly2=(struct link *)malloc(sizeof(struct link));
poly=(struct link *)malloc(sizeof(struct link));
polyAddC=(struct link *)malloc(sizeof(struct link));
createPoly1(poly1);
printf("\nFirst Polynomial: ");
displayPoly(poly1);
createPoly2(poly2);
printf("\nSecond Polynomial: ");
displayPoly(poly2);
addPoly(poly1, poly2, poly);
printf("\nAdded Polynomials: ");
displayPoly(poly);
createPolyAddCorrect(polyAddC);
printf("\nCorrect Answer: ");
displayPoly(polyAddC);
if(poly==polyAddC)
{
printf("Test Passed");
}
else
{
printf("Test Failed");
}
}
` ` `
解决方案
推荐阅读
- python - 如何制作一个提供请求的csv文件的python网络服务器
- python - 我试图让一个对象在我的游戏中随机生成一段时间,当我的角色走过该对象时它会消失
- asp.net - 嵌套用户控件中的访问方法
- sql - 如何按三分之二或四分之二对数据进行分类 - Oracle SQL
- itext - 带有签名文件的 iTextsharp
- java - 从测试套件重复运行仅在 Eclipse JUnit 视图中显示最终运行
- azure - 在 Azure DevOps Pipelines 中跨阶段共享变量
- c# - 如何在 C# WPF 中有一个多列下拉框?
- kubernetes - Kubernetes 上的 minIO:备份它的最佳方式
- sql - 如何转义要执行的 sql 字符串中使用的变量中的潜在引号?