多项式相加结果追加读入文件,程序无逻辑问题但是结果失败

以下这段程序运行的时候并未报错,但是就是无法在文件中追加打印出多项式相加的结果,请问是哪里出了问题?


  #include
#include
typedef struct PolyArray
{
    double coef;
    int exp;
}PolyArray;
typedef struct PolyNode
{
    PolyArray data;
    struct PolyNode* next;
}PolyNode;
void CreatePoly(PolyNode* head, int m, PolyArray* array)
{
    PolyNode* prev;
    head = (PolyNode*)malloc(sizeof(PolyNode));
    prev = (PolyNode*)malloc(sizeof(PolyNode));
    if (prev)
    {
        head = prev;
    }
        for (int i = 0; i < m; i++)
        {
            PolyNode* p = (PolyNode*)malloc(sizeof(PolyNode));
            if (p&&prev)
            {
                p->data.coef = array[i].coef;
                p->data.exp = array[i].exp;
                p->next = NULL;
                prev->next = p;
                p = prev;
            }
        }
}
int ComparePoly(PolyNode* a, PolyNode* b)
{
    if (a->data.exp < b->data.exp)
        return -1;
    else if (a->data.exp < b->data.exp)
        return 0;
    else return 1;
}
PolyNode* AddPoly(PolyNode* ha, PolyNode* hb, PolyNode* hc)
{
    PolyNode* p, * q, * newp;
    p = (PolyNode*)malloc(sizeof(PolyNode));
    q = (PolyNode*)malloc(sizeof(PolyNode));
    p = ha->next;
    p = hb->next;
    double sum;
    PolyNode* newh = (PolyNode*)malloc(sizeof(PolyNode));
    newh = hc;
    while (p && q)
    {
        switch (ComparePoly(p, q))
        {
        case -1:
            newp = (PolyNode*)malloc(sizeof(PolyNode));
            if (newp)
            {
                newp->data.coef = p->data.coef;
                newp->data.exp = p->data.exp;
                newh->next = newp;
                p = p->next;
            }
            break;
        case 0:
            newp = (PolyNode*)malloc(sizeof(PolyNode));
            if (newp)
            {
                newp->data.coef = q->data.coef;
                newp->data.exp = q->data.exp;
                newh->next = newp;
                q = q->next;
                break;
            }
        case 1:
            sum = p->data.coef + q->data.coef;
            if (sum != 0.0)
            {
                newp = (PolyNode*)malloc(sizeof(PolyNode));
                if (newp)
                {
                    newp->data.coef = sum;
                    newp->data.exp = q->data.exp;
                    newh->next = newp;
                    newp = newh;
                }
            }
            p = p->next;
            q = q->next;
            break;
        }
    }
    newh->next = p ? p : q;
    return hc;
}
void PrintPoly(FILE* fp, PolyNode* head)
{
    PolyNode* s = (PolyNode*)malloc(sizeof(PolyNode));
    if (head&&s)
        s = head->next;
    while (s)
    {
        fprintf(fp,"\n(%lf,%d)", s->data.coef, s->data.exp);
        s = s->next;
    }
}
int main()
{
    FILE* fp;
    char str[] = "D:\\visualstudio\\数据结构作业2\\input.txt";
    fopen_s(&fp, str, "at");
    if (!fp)
    {
        printf("open file error");
        return 0;
    }
    PolyNode* ha, * hb, * hc;
    ha = (PolyNode*)malloc(sizeof(PolyNode));
    hb = (PolyNode*)malloc(sizeof(PolyNode));
    hc = (PolyNode*)malloc(sizeof(PolyNode));
    PolyArray* array_a, * array_b;
    array_a = (PolyArray*)malloc(sizeof(PolyArray) * 3);
    array_b = (PolyArray*)malloc(sizeof(PolyArray) * 3);
    if (array_a && array_b)
        {
            for (int i = 0; i < 3; i++)
            {
                fscanf_s(fp, "(%lf,%d)", &array_a[i].coef, &array_a[i].exp);
                fscanf_s(fp, "(%lf,%d)", &array_b[i].coef, &array_b[i].exp);
            }
        }
    CreatePoly(ha, 3, array_a);
    CreatePoly(hb, 3, array_b);
    AddPoly(ha, hb, hc);
    PrintPoly(fp, hc);
    free(ha);
    free(hb);
    free(hc);
    free(array_a);
    free(array_b);
    fclose(fp);
}

参考GPT和自己的思路:

根据你提供的代码,可能出现问题的地方有:

  1. CreatePoly函数中对head的改变不会影响到主函数中的ha和hb,因此可以考虑将head改为指向指针的指针,这样函数中对head的改变会影响到主函数中的ha和hb。

  2. 在ComparePoly函数中,当a->data.exp < b->data.exp时,应该返回1而不是-1。

  3. 在AddPoly函数中,变量p和q均指向ha和hb中的第二个节点,因此应该将p和q的初始化语句改为p=ha和q=hb。

  4. 在AddPoly函数中,newp是指向新创建节点的指针,在switch语句中需要将newp的next指向NULL。

  5. 在AddPoly函数中,newp = newh会将newp指向newh而不是将newh的next指向newp,因此应该将newh->next = newp;

  6. 在AddPoly函数中,newp = prev应该改为prev = p,将prev指向p。

  7. 在PrintPoly函数中,应该将s初始化为head而不是新创建的节点。

在纠正上述错误之后,代码如下所示:

#include<stdio.h>
#include<malloc.h>

typedef struct PolyArray
{
    double coef;
    int exp;
}PolyArray;

typedef struct PolyNode
{
    PolyArray data;
    struct PolyNode* next;
}PolyNode;

void CreatePoly(PolyNode** head, int m, PolyArray* array)
{
    PolyNode* prev = NULL;
    *head = (PolyNode*)malloc(sizeof(PolyNode));
    for (int i = 0; i < m; i++)
    {
        PolyNode* p = (PolyNode*)malloc(sizeof(PolyNode));
        if (p)
        {
            p->data.coef = array[i].coef;
            p->data.exp = array[i].exp;
            p->next = NULL;
            if (!prev)
            {
                prev = *head;
            }
            prev->next = p;
            prev = p;
        }
    }
}

int ComparePoly(PolyNode* a, PolyNode* b)
{
    if (a->data.exp < b->data.exp)
        return 1;
    else if (a->data.exp > b->data.exp)
        return -1;
    else return 0;
}

PolyNode* AddPoly(PolyNode* ha, PolyNode* hb, PolyNode* hc)
{
    PolyNode* p, * q, * newp, *newh;
    p = ha->next;
    q = hb->next;
    newh = hc;
    while (p && q)
    {
        switch (ComparePoly(p, q))
        {
        case 1:
            newp = (PolyNode*)malloc(sizeof(PolyNode));
            if (newp)
            {
                newp->data.coef = p->data.coef;
                newp->data.exp = p->data.exp;
                newp->next = NULL;
                newh->next = newp;
                newh = newp;
                p = p->next;
            }
            break;
        case -1:
            newp = (PolyNode*)malloc(sizeof(PolyNode));
            if (newp)
            {
                newp->data.coef = q->data.coef;
                newp->data.exp = q->data.exp;
                newp->next = NULL;
                newh->next = newp;
                newh = newp;
                q = q->next;
                break;
            }
        case 0:
            double sum = p->data.coef + q->data.coef;
            if (sum != 0.0)
            {
                newp = (PolyNode*)malloc(sizeof(PolyNode));
                if (newp)
                {
                    newp->data.coef = sum;
                    newp->data.exp = q->data.exp;
                    newp->next = NULL;
                    newh->next = newp;
                    newh = newp;
                }
            }
            p = p->next;
            q = q->next;
            break;
        }
    }
    newh->next = p ? p : q;
    return hc;
}

void PrintPoly(FILE* fp, PolyNode* head)
{
    PolyNode* s = head->next;
    while (s)
    {
        fprintf(fp, "(%lf,%d)", s->data.coef, s->data.exp);
        s = s->next;
    }
}

int main()
{
    FILE* fp;
    char str[] = "D:\\visualstudio\\数据结构作业2\\input.txt";
    fopen_s(&fp, str, "at");
    if (!fp)
    {
        printf("open file error");
        return 0;
    }
    PolyNode* ha, * hb, * hc;
    PolyArray* array_a = (PolyArray*)malloc(sizeof(PolyArray) * 3);
    PolyArray* array_b = (PolyArray*)malloc(sizeof(PolyArray) * 3);
    if (array_a && array_b)
    {
        for (int i = 0; i < 3; i++)
        {
            fscanf_s(fp, "(%lf,%d)", &array_a[i].coef, &array_a[i].exp);
            fscanf_s(fp, "(%lf,%d)", &array_b[i].coef, &array_b[i].exp);
        }
    }
    CreatePoly(&ha, 3, array_a);
    CreatePoly(&hb, 3, array_b);
    hc = (PolyNode*)malloc(sizeof(PolyNode));
    hc->next = NULL;
    AddPoly(ha, hb, hc);
    PrintPoly(fp, hc);
    free(ha);
    free(hb);
    free(hc);
    free(array_a);
    free(array_b);
    fclose(fp);

    return 0;
}

注意,在该代码中,打开文件的方式是“at”,即打开文件后将指针移动到文件结尾,新内容将追加到文件末尾。如果指针不能移动到文件结尾,或者出现其他问题,可能会导致结果不符合预期。因此,在测试时需要保证输入文件的正确性。同时,在输出结果之后,需要关闭文件指针。