鱼C论坛

 找回密码
 立即注册
查看: 1066|回复: 4

C语言实现一元多项式的乘法与加法运算(求告知错误原因)

[复制链接]
发表于 2023-6-25 23:03:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
求告知求乘积出错原因 谢谢!

设计函数分别求两个一元多项式的乘积与和。

输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。

输入样例:
4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1
输出样例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

我的代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct Node *node;
  4. struct Node
  5. {
  6.     int x;
  7.     int z;
  8.     node next;
  9. };

  10. node read();
  11. void print(node lsit);
  12. node he(node list1,node list2);
  13. node ji(node list1,node list2);

  14. node read()
  15. {
  16.         int i;
  17.         scanf("%d",&i);
  18.         node head = (node)malloc(sizeof(node));
  19.         head->next = NULL;
  20.         node p = head;
  21.        
  22.         while(i--)
  23.         {
  24.                 node temp = (node)malloc(sizeof(node));
  25.                 scanf("%d %d",&temp->x,&temp->z);
  26.                 p->next = temp;
  27.                 p = p->next;
  28.         }
  29.         p->next = NULL;
  30.        
  31.         return head;
  32. }

  33. node he(node list1,node list2)
  34. {
  35.         node temp;
  36.         node head = (node)malloc(sizeof(node));
  37.         head->next = NULL;
  38.         node p = head;
  39.        
  40.         if(list2->next == NULL)
  41.                 return list1;
  42.         else
  43.         {
  44.                 list1 = list1->next;
  45.                 list2 = list2->next;

  46.                 while(list1 && list2)
  47.                 {
  48.                        
  49.                         temp = (node)malloc(sizeof(node));
  50.                         if(list1->z == list2->z)
  51.                         {       
  52.                                 temp->x = list1->x + list2->x;
  53.                                 temp->z = list1->z;
  54.                                 list1 = list1->next;
  55.                                 list2 = list2->next;
  56.                         }
  57.                         else if(list1->z > list2->z)
  58.                         {
  59.                                 temp->x = list1->x;
  60.                                 temp->z = list1->z;
  61.                                 list1 = list1->next;
  62.                         }
  63.                         else
  64.                         {
  65.                                 temp->x = list2->x;
  66.                                 temp->z = list2->z;
  67.                                 list2 = list2->next;
  68.                         }
  69.                         p->next = temp;
  70.                         p = p->next;                       
  71.                 }
  72.                 for(;list1;list1 = list1->next)temp = (node)malloc(sizeof(node)),temp->x = list1->x,temp->z = list1->z,p->next = temp,p = p->next;
  73.                 for(;list2;list2 = list2->next)temp = (node)malloc(sizeof(node)),temp->x = list2->x,temp->z = list2->z,p->next = temp,p = p->next;
  74.                 p->next = NULL;
  75.         return head;
  76.         }
  77. }

  78. node ji(node list1,node list2)
  79. {
  80.        
  81.         node t;
  82.         node OK = (node)malloc(sizeof(node));
  83.         OK->next = NULL;
  84.         node L = OK;

  85.         list1 = list1->next;
  86.         list2 = list2->next;
  87.                
  88.         for(list1;list1;list1=list1->next)
  89.         {
  90.                 node temp;
  91.                 node head = (node)malloc(sizeof(node));
  92.                 head->next = NULL;
  93.                 node p = head;
  94.                 for(t = list2;t;t=t->next)
  95.                 {
  96.                         temp = (node)malloc(sizeof(node));
  97.                         temp->x = list1->x * t->x;
  98.                         temp->z = list1->z + t->z;

  99.                         p->next = temp;
  100.                         p = p->next;
  101.                 }
  102.                 p->next = NULL;
  103.                 L = he(head,L);
  104.         }
  105.         L->next = NULL;
  106.         return OK;
  107.                
  108. }



  109. void print(node list)
  110. {
  111.         if(list->next)
  112.         {
  113.                 list = list->next;
  114.                 while(list)
  115.                 {
  116.                         printf("%d %d",list->x,list->z);
  117.                         list = list->next;
  118.                         if(list)
  119.                         printf(" ");

  120.                 }
  121.         }
  122.         else
  123.         printf("0 ");
  124. }



  125. int main()
  126. {
  127.     node list1,list2;
  128.     list1 = read();
  129.     list2 = read();

  130. //   print(list1);
  131. //   print(list2);
  132.         print(he(list1,list2));
  133.         print(ji(list1,list2));
  134.   return 0;
  135. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-6-25 23:29:38 | 显示全部楼层
您的代码存在一些错误。首先,您在申请新的节点时大小应该为  sizeof(struct Node)  而不是  sizeof(node) 。其次,您在乘法函数  ji  中初始化  L  节点的时候, OK  节点的  next  未置为  NULL ,这会导致在乘法结果累加时出错。

以下是修改后的代码:

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

  3. typedef struct Node *node;
  4. struct Node
  5. {
  6.     int x;
  7.     int z;
  8.     node next;
  9. };

  10. node read();
  11. void print(node list);
  12. node he(node list1,node list2);
  13. node ji(node list1,node list2);

  14. node read()
  15. {
  16.     int i;
  17.     scanf("%d",&i);
  18.     node head = (node)malloc(sizeof(struct Node));
  19.     head->next = NULL;
  20.     node p = head;
  21.    
  22.     while(i--)
  23.     {
  24.         node temp = (node)malloc(sizeof(struct Node));
  25.         scanf("%d %d",&temp->x,&temp->z);
  26.         temp->next = NULL;
  27.         p->next = temp;
  28.         p = p->next;
  29.     }
  30.    
  31.     return head;
  32. }

  33. node he(node list1,node list2)
  34. {
  35.     node temp;
  36.     node head = (node)malloc(sizeof(struct Node));
  37.     head->next = NULL;
  38.     node p = head;
  39.    
  40.     if(list2->next == NULL)
  41.         return list1;
  42.     else
  43.     {
  44.         list1 = list1->next;
  45.         list2 = list2->next;
  46.         while(list1 && list2)
  47.         {
  48.             temp = (node)malloc(sizeof(struct Node));
  49.             if(list1->z == list2->z)
  50.             {      
  51.                 temp->x = list1->x + list2->x;
  52.                 temp->z = list1->z;
  53.                 list1 = list1->next;
  54.                 list2 = list2->next;
  55.             }
  56.             else if(list1->z > list2->z)
  57.             {
  58.                 temp->x = list1->x;
  59.                 temp->z = list1->z;
  60.                 list1 = list1->next;
  61.             }
  62.             else
  63.             {
  64.                 temp->x = list2->x;
  65.                 temp->z = list2->z;
  66.                 list2 = list2->next;
  67.             }
  68.             temp->next = NULL;
  69.             p->next = temp;
  70.             p = p->next;                       
  71.         }
  72.         while(list1)
  73.         {
  74.             temp = (node)malloc(sizeof(struct Node));
  75.             temp->x = list1->x;
  76.             temp->z = list1->z;
  77.             temp->next = NULL;
  78.             p->next = temp;
  79.             p = p->next;
  80.             list1 = list1->next;
  81.         }
  82.         while(list2)
  83.         {
  84.             temp = (node)malloc(sizeof(struct Node));
  85.             temp->x = list2->x;
  86.             temp->z = list2->z;
  87.             temp->next = NULL;
  88.             p->next = temp;
  89.             p = p->next;
  90.             list2 = list2->next;
  91.         }
  92.         p->next = NULL;
  93.         return head;
  94.     }
  95. }

  96. node ji(node list1,node list2)
  97. {
  98.     node t;
  99.     node OK = (node)malloc(sizeof(struct Node));
  100.     OK->next = NULL;
  101.     node L = OK;
  102.     list1 = list1->next;
  103.     list2 = list2->next;
  104.            
  105.     for(; list1; list1=list1->next)
  106.     {
  107.         node temp;
  108.         node head = (node)malloc(sizeof(struct Node));
  109.         head->next = NULL;
  110.         node p = head;
  111.         for(t = list2; t; t=t->next)
  112.         {
  113.             temp = (node)malloc(sizeof(struct Node));
  114.             temp->x = list1->x * t->x;
  115.             temp->z = list1->z + t->z;
  116.             temp->next = NULL;
  117.             p->next = temp;
  118.             p = p->next;
  119.         }
  120.         L = he(head, L);
  121.     }
  122.     return L;
  123. }

  124. void print(node list)
  125. {
  126.     if(list->next)
  127.     {
  128.         list = list->next;
  129.         while(list)
  130.         {
  131.             printf("%d %d",list->x,list->z);
  132.             list = list->next;
  133.             if(list)
  134.                 printf(" ");
  135.         }
  136.     }
  137.     else
  138.         printf("0 0");
  139. }

  140. int main()
  141. {
  142.     node list1,list2;
  143.     list1 = read();
  144.     list2 = read();
  145.     print(ji(list1,list2));
  146.     printf("\n");
  147.     print(he(list1,list2));
  148.     return 0;
  149. }
复制代码


注意:
· 你需要在新的  node  分配后立即将  next  指针设置为  NULL 。这样可以防止你在后续代码中意外地访问到未初始化的内存。
· 你需要先打印乘法的结果,再打印加法的结果,以符合题目的输出顺序。
· 空的多项式应输出  0 0 ,而非  0 。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-26 09:37:21 From FishC Mobile | 显示全部楼层
isdkz 发表于 2023-6-25 23:29
您的代码存在一些错误。首先,您在申请新的节点时大小应该为  sizeof(struct Node)  而不是  sizeof(node)  ...

如要释放内存需要在何时释放呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-26 09:39:47 | 显示全部楼层
a905448839 发表于 2023-6-26 09:37
如要释放内存需要在何时释放呢?

在C语言中,你需要在不再需要动态分配的内存时释放它,以防止内存泄露。在你的程序中,可能在  main  函数的末尾释放所有动态分配的内存。

你可以通过迭代链表,并对每个节点使用  free  函数来释放内存。然而,你需要注意,一旦一个节点被释放,你就不能再访问它的  next  指针。所以,你需要在释放节点前先保存下一个节点的指针。

下面是一个释放链表内存的函数示例:

  1. void freeList(node head) {
  2.     node temp;
  3.     while (head != NULL) {
  4.         temp = head;
  5.         head = head->next;
  6.         free(temp);
  7.     }
  8. }
复制代码


然后在你的  main  函数中调用该函数:

  1. int main() {
  2.     node list1,list2;
  3.     list1 = read();
  4.     list2 = read();
  5.     print(ji(list1,list2));
  6.     printf("\n");
  7.     print(he(list1,list2));

  8.     // 释放链表内存
  9.     freeList(list1);
  10.     freeList(list2);

  11.     return 0;
  12. }
复制代码


这样可以确保你的程序不会引起内存泄露。但请注意,如果你的程序在其他地方仍然保存有这些节点的指针,并试图在后续的代码中使用它们,那么就会导致未定义的行为。因此,你需要确保在释放内存后不再使用这些指针。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-26 09:55:37 From FishC Mobile | 显示全部楼层
isdkz 发表于 2023-6-26 09:39
在C语言中,你需要在不再需要动态分配的内存时释放它,以防止内存泄露。在你的程序中,可能在  main  函 ...


我在ji函数里面分配的内存空间,创建的链表传入了he函数。而he函数又是新创建的链表,也分配了内存空间。,所以在最后只用free释放掉list1和list2吗?ji函数和he函数里分配的内存空间,创建的链表不用去管他吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 09:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表