多项式链接表测试用例,无添加错误,无所事事

时间:2019-12-01 13:52:07

标签: c list polynomials

这部分代码旨在创建两个多项式,然后将它们相加。 Main测试这是否成功完成。当我构建时,没有错误,但是在运行时没有任何反应。我不确定为什么会发生这种情况,我们已经停留了很长时间。注释掉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");
    }
}
` ` `

0 个答案:

没有答案