首页 > 解决方案 > 多项式链表测试用例,不添加任何错误,不做任何事情

问题描述

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

标签: clistpolynomials

解决方案


推荐阅读