鱼C论坛

 找回密码
 立即注册
查看: 652|回复: 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

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

typedef struct Node *node;
struct Node
{
    int x;
    int z;
    node next;
};

node read();
void print(node lsit);
node he(node list1,node list2);
node ji(node list1,node list2);

node read()
{
        int i;
        scanf("%d",&i);
        node head = (node)malloc(sizeof(node));
        head->next = NULL;
        node p = head;
        
        while(i--)
        {
                node temp = (node)malloc(sizeof(node));
                scanf("%d %d",&temp->x,&temp->z);
                p->next = temp;
                p = p->next;
        }
        p->next = NULL;
        
        return head;
}

node he(node list1,node list2)
{
        node temp;
        node head = (node)malloc(sizeof(node));
        head->next = NULL;
        node p = head;
        
        if(list2->next == NULL)
                return list1;
        else
        {
                list1 = list1->next;
                list2 = list2->next;

                while(list1 && list2)
                {
                        
                        temp = (node)malloc(sizeof(node));
                        if(list1->z == list2->z)
                        {        
                                temp->x = list1->x + list2->x;
                                temp->z = list1->z;
                                list1 = list1->next;
                                list2 = list2->next;
                        }
                        else if(list1->z > list2->z)
                        {
                                temp->x = list1->x;
                                temp->z = list1->z;
                                list1 = list1->next;
                        }
                        else
                        {
                                temp->x = list2->x;
                                temp->z = list2->z;
                                list2 = list2->next;
                        }
                        p->next = temp;
                        p = p->next;                        
                }
                for(;list1;list1 = list1->next)temp = (node)malloc(sizeof(node)),temp->x = list1->x,temp->z = list1->z,p->next = temp,p = p->next;
                for(;list2;list2 = list2->next)temp = (node)malloc(sizeof(node)),temp->x = list2->x,temp->z = list2->z,p->next = temp,p = p->next;
                p->next = NULL;
        return head; 
        }
}

node ji(node list1,node list2)
{
        
        node t;
        node OK = (node)malloc(sizeof(node));
        OK->next = NULL;
        node L = OK;

        list1 = list1->next;
        list2 = list2->next;
                
        for(list1;list1;list1=list1->next)
        {
                node temp;
                node head = (node)malloc(sizeof(node));
                head->next = NULL;
                node p = head;
                for(t = list2;t;t=t->next)
                {
                        temp = (node)malloc(sizeof(node));
                        temp->x = list1->x * t->x;
                        temp->z = list1->z + t->z;

                        p->next = temp;
                        p = p->next;
                }
                p->next = NULL;
                L = he(head,L); 
        }
        L->next = NULL;
        return OK;
                
}



void print(node list)
{
        if(list->next)
        {
                list = list->next;
                while(list)
                {
                        printf("%d %d",list->x,list->z);
                        list = list->next;
                        if(list)
                        printf(" ");

                }
        }
        else
        printf("0 ");
}



int main()
{
    node list1,list2;
    list1 = read();
    list2 = read();

 //   print(list1);
 //   print(list2);
        print(he(list1,list2));
        print(ji(list1,list2));
  return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

以下是修改后的代码:
#include <stdio.h>
#include <stdlib.h>

typedef struct Node *node;
struct Node
{
    int x;
    int z;
    node next;
};

node read();
void print(node list);
node he(node list1,node list2);
node ji(node list1,node list2);

node read()
{
    int i;
    scanf("%d",&i);
    node head = (node)malloc(sizeof(struct Node));
    head->next = NULL;
    node p = head;
    
    while(i--)
    {
        node temp = (node)malloc(sizeof(struct Node));
        scanf("%d %d",&temp->x,&temp->z);
        temp->next = NULL;
        p->next = temp;
        p = p->next;
    }
    
    return head;
}

node he(node list1,node list2)
{
    node temp;
    node head = (node)malloc(sizeof(struct Node));
    head->next = NULL;
    node p = head;
    
    if(list2->next == NULL)
        return list1;
    else
    {
        list1 = list1->next;
        list2 = list2->next;
        while(list1 && list2)
        {
            temp = (node)malloc(sizeof(struct Node));
            if(list1->z == list2->z)
            {       
                temp->x = list1->x + list2->x;
                temp->z = list1->z;
                list1 = list1->next;
                list2 = list2->next;
            }
            else if(list1->z > list2->z)
            {
                temp->x = list1->x;
                temp->z = list1->z;
                list1 = list1->next;
            }
            else
            {
                temp->x = list2->x;
                temp->z = list2->z;
                list2 = list2->next;
            }
            temp->next = NULL;
            p->next = temp;
            p = p->next;                       
        }
        while(list1)
        {
            temp = (node)malloc(sizeof(struct Node));
            temp->x = list1->x;
            temp->z = list1->z;
            temp->next = NULL;
            p->next = temp;
            p = p->next;
            list1 = list1->next;
        }
        while(list2)
        {
            temp = (node)malloc(sizeof(struct Node));
            temp->x = list2->x;
            temp->z = list2->z;
            temp->next = NULL;
            p->next = temp;
            p = p->next;
            list2 = list2->next;
        }
        p->next = NULL;
        return head;
    }
}

node ji(node list1,node list2)
{
    node t;
    node OK = (node)malloc(sizeof(struct Node));
    OK->next = NULL;
    node L = OK;
    list1 = list1->next;
    list2 = list2->next;
           
    for(; list1; list1=list1->next)
    {
        node temp;
        node head = (node)malloc(sizeof(struct Node));
        head->next = NULL;
        node p = head;
        for(t = list2; t; t=t->next)
        {
            temp = (node)malloc(sizeof(struct Node));
            temp->x = list1->x * t->x;
            temp->z = list1->z + t->z;
            temp->next = NULL;
            p->next = temp;
            p = p->next;
        }
        L = he(head, L);
    }
    return L;
}

void print(node list)
{
    if(list->next)
    {
        list = list->next;
        while(list)
        {
            printf("%d %d",list->x,list->z);
            list = list->next;
            if(list)
                printf(" ");
        }
    }
    else
        printf("0 0");
}

int main()
{
    node list1,list2;
    list1 = read();
    list2 = read();
    print(ji(list1,list2));
    printf("\n");
    print(he(list1,list2));
    return 0;
}

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

使用道具 举报

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

如要释放内存需要在何时释放呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

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

下面是一个释放链表内存的函数示例:
void freeList(node head) {
    node temp;
    while (head != NULL) {
        temp = head;
        head = head->next;
        free(temp);
    }
}

然后在你的  main  函数中调用该函数:
int main() {
    node list1,list2;
    list1 = read();
    list2 = read();
    print(ji(list1,list2));
    printf("\n");
    print(he(list1,list2));

    // 释放链表内存
    freeList(list1);
    freeList(list2);

    return 0;
}

这样可以确保你的程序不会引起内存泄露。但请注意,如果你的程序在其他地方仍然保存有这些节点的指针,并试图在后续的代码中使用它们,那么就会导致未定义的行为。因此,你需要确保在释放内存后不再使用这些指针。
想知道小甲鱼最近在做啥?请访问 -> 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函数里分配的内存空间,创建的链表不用去管他吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-7 13:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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