使用链表将两个多项式相乘的程序

时间:2018-08-20 16:07:43

标签: c gcc linked-list algebra polynomial-math

我正在尝试使用链表乘法多项式。 这是代码,但这给了我错误的结果。

#include<stdio.h>
#include<stdlib.h>

struct node {
  int coefficient, exponent;
  struct node *next;
};

struct node *hPtr1, *hPtr2, *hPtr3;
/*
 * creates new node and fill the given data
 */
struct node * buildNode(int coefficient, int exponent) {
  struct node *ptr = (struct node *) malloc(sizeof (struct node));
  ptr->coefficient = coefficient;
  ptr->exponent = exponent;
  ptr->next = NULL;
  return ptr;
}

/* insert data in decending order - based on exponent value */
void polynomial_insert(struct node ** myNode, int coefficient, int exponent) {
  struct node *lPtr, *pPtr, *qPtr = *myNode;
  lPtr = buildNode(coefficient, exponent);

  /* inserting new node at appropriate position */
  if (*myNode == NULL || (*myNode)->exponent < exponent) {
    *myNode = lPtr;
    (*myNode)->next = qPtr;
    return;
  }

  /* placing new node between two nodes or end of node */
  while (qPtr) {
    pPtr = qPtr;
    qPtr = qPtr->next;
    if (!qPtr) {
      pPtr->next = lPtr;
      break;

    } 
    else if ((exponent < pPtr->exponent) &&       (exponent > qPtr->exponent)){
    lPtr->next = qPtr;
    pPtr->next = lPtr;
    break;
      }
      }
    return;
  }

  /* inserting new node with resultant data into the output list (n1) */
  void polynomial_add(struct node **n1, int coefficient, int exponent) {
    struct node *x = NULL, *temp = *n1;
    if (*n1 == NULL || (*n1)->exponent < exponent) {
      /* adding at the front */
      *n1 = x = buildNode(coefficient, exponent);
      (*n1)->next = temp;
    } else {
      while (temp) {
    if (temp->exponent == exponent) {
      /* updating the co-efficient value  alone */
      temp->coefficient = temp->coefficient + coefficient;
      return;
    }
    if (temp->exponent > exponent && (!temp->next || temp->next->exponent < exponent)) {
      /* inserting in the middle or end */
      x = buildNode(coefficient, exponent);
      x->next = temp->next;
      temp->next = x;
      return;
    }
    temp = temp->next;
      }
      x->next = NULL;
      temp->next = x;
    }
  }

  void polynomial_multiply(struct node **n1, struct node *n2, struct node *n3) {
    struct node * temp;
    int coefficient, exponent;

    temp = n3;

    /* if both input list are absent, then output list is NULL */
    if (!n2 && !n3)
      return;

    /* input list 1(n2) is absent, then output list is input list2 (n3) */
    if (!n2) {
      *n1 = n3;
    } else if (!n3) {

      /*
       * list n3 is absent, then o/p list is n2
       */
      *n1 = n2;
    } else {
      while (n2) {
    while (n3) {
      /* multiply coefficient & add exponents */
      coefficient = n2->coefficient * n3->coefficient;
      exponent = n2->exponent + n3->exponent;
      n3 = n3->next;
      /* insert the above manipulated data to o/p list */
      polynomial_add(n1, coefficient, exponent);
    }
    n3 = temp;
    n2 = n2->next;
      }
    }
    return;
  }

  /* delete the given input list */
  struct node * polynomial_deleteList(struct node *ptr) {
    struct node *temp;
    while (ptr){
      temp = ptr->next;
      free(ptr);
      ptr = temp;
    }
    return NULL;
  }

  void polynomial_view(struct node *ptr) {
    int i = 0;
    int flag=0;
    while (ptr) {
      if(ptr->exponent != 0 || ptr->exponent != 1 ){
    if(ptr->coefficient > 0 && flag==0 ){
      printf("%dx^%d", ptr->coefficient,ptr->exponent);
      flag++;
    }
    else if (ptr->coefficient > 0 && flag==1 )
      printf("+%dx^%d", ptr->coefficient,ptr->exponent);
    else if(ptr->coefficient < 0)
      printf("%dx^%d", ptr->coefficient,ptr->exponent);
      }
      else if (ptr->exponent == 0){
    if(ptr->coefficient > 0 && flag==0 ){
      printf("%d", ptr->coefficient);
      flag++;
    }
    else if (ptr->coefficient > 0 && flag==1 )
      printf("+%d", ptr->coefficient);
    else if(ptr->coefficient < 0)
      printf("%d", ptr->coefficient);
      }
      else if( ptr->exponent == 1 ){
    if(ptr->coefficient > 0 && flag==0 ){
      printf("%dx", ptr->coefficient);
      flag++;
    }
    else if (ptr->coefficient > 0 && flag==1 )
      printf("+%dx", ptr->coefficient);
    else if(ptr->coefficient < 0)
      printf("%dx", ptr->coefficient);
      }
      ptr = ptr->next;
      i++;
    }
    printf("\n");
    return;
  }

  int main (int argc, char *argv[]) {
    int coefficient, exponent, i, n;
    int count;
    printf("-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=\n");
    printf("            Multiplication of Two Polynomials\n");
    printf("-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=\n");
    printf("Enter the number of coefficients in the multiplicand:");
    scanf("%d",&count);
    for(i=0;i<count;i++){
      printf("Enter the coefficient part:");
      scanf("%d", &coefficient);
      printf("Enter the exponent part:");
      scanf("%d",&exponent);
          polynomial_insert(&hPtr1, coefficient, exponent);
    }
    printf("Enter the number of coefficients in the multiplier:");
    scanf("%d",&count);
    for(i=0;i<count;i++){
      printf("Enter the coefficient part:");
      scanf("%d", &coefficient);
      printf("Enter the exponent part:");
      scanf("%d",&exponent);
          polynomial_insert(&hPtr2, coefficient, exponent);
    }
        printf("Polynomial Expression 1: ");
        polynomial_view(hPtr1);
        printf("Polynomial Expression 2: ");
        polynomial_view(hPtr2);

        polynomial_multiply(&hPtr3, hPtr1, hPtr2);

        printf("Output:\n");
        polynomial_view(hPtr3);

    printf("-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
        hPtr1 = polynomial_deleteList(hPtr1);
        hPtr2 = polynomial_deleteList(hPtr2);
        hPtr3 = polynomial_deleteList(hPtr3);

        return 0;
  } 

我期望的输出是

-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-= -=-=-=-= – =
两个多项式的乘法
-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=- =-=-= – =
输入被乘数的系数数:2
输入系数部分:3
输入指数部分:2
输入系数部分:2
输入指数部分:3
在乘数中输入系数数:2
输入系数部分:4
输入指数部分:2
输入系数部分:1
输入指数部分:3
多项式表达式1:2x ^ 3 + 3x ^ 2
多项式表达式2:1x ^ 3 + 4x ^ 2
输出:
2x ^ 6 + 11x ^ 5 + 12x ^ 4
-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=- =-=-=-=-

但是要输出的不是

-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=
            Multiplication of Two Polynomials
-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=
Enter the number of coefficients in the multiplicand:3
Enter the coefficient part:10
Enter the exponent part:x
Enter the coefficient part:Enter the exponent part:Enter the coefficient part:Enter the exponent part:Enter the number of coefficients in the multiplier:Enter the coefficient part:Enter the exponent part:Enter the coefficient part:Enter the exponent part:Enter the coefficient part:Enter the exponent part:Polynomial Expression 1: 10x^0+10x^0+10x^0
Polynomial Expression 2: 10x^0+10x^0+10x^0
Output:
900x^0
-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

上面的代码有什么错误?

1 个答案:

答案 0 :(得分:0)

在示例输入中,您为指数部分指定了x

Enter the exponent part:x

字符x被读入exponent,它是一个整数。阅读这是什么结果?
根据C语言标准(n1256):

  

7.19.6.4 scanf功能
  剧情简介
  1. #include   int scanf(const char *限制格式,...);
  说明
  2. scanf函数等效于fscanf ,其参数stdin插入到scanf的参数之前。

     

7.19.6.2 fscanf函数   ...

     
      
  1. 除了使用%指示符的情况外,输入项(或使用%n指示符的情况下,输入字符的数量)将转换为适合于转换说明符的类型。 如果输入项不是匹配序列,则指令的执行失败:此条件是匹配失败。除非分配抑制用*指示,否则转换结果将放置在对象中由format参数后面的第一个参数指向,该参数尚未收到转换结果。如果该对象没有适当的类型,或者无法在对象中表示转换结果,则该行为是不确定的。
  2.   

这将进一步导致polynomial_insert函数的问题。 在调用polynomial_insert函数之前,可以检查scanf是否成功,以及从std输入到coefficientexponent变量的std读取的值是否在可接受的范围内。 / p>

请参阅@JonathanLeffler的评论,它很好地解释了这一点。