m0_74793916 2023-03-21 20:00 采纳率: 66.7%
浏览 23
已结题

c语言链表实现超大十进制数的加法,但是输出不出结果,请问有什么问题?

c语言链表实现超大十进制数的加法,但是输出不出结果,请问有什么问题?

#include<stdio.h>
#include<malloc.h>
typedef struct PolyArray
{
    int coef;
    int exp;
}PolyArray;
typedef struct PolyNode
{
    PolyArray data;
    struct PolyNode* next;
}PolyNode;
void IntArray(PolyArray** array)
{
    FILE* fp;
    char str[] = "D:\\visualstudio\\数据结构作业\\input.txt";
    fopen_s(&fp, str, "rt");
    if (!fp)
    {
        printf("open file error");
    }
    int* i = NULL;
    i[0] = 0, i[1] = 0;
    char c = '\0';
    if (fp)
    {
        while(array)
        {
            while (c = fgetc(fp))
            {
                (*array)[*i].coef = c - '0';
                (*array)[*i].exp = *i;
                (*i)++;
                if (c == '\n')
                {
                    array++;
                    i++;
                }
            }
        }
        fclose(fp);
    }
}
void CreatePoly(PolyNode** head, int m, PolyArray* array)
{
    PolyNode* prev = NULL;
    *head = (PolyNode*)malloc(sizeof(PolyNode));
    if ((*head))
        (*head)->next = NULL;
    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:
            newh->next = p;
            newh = p;
            p = p->next;
            break;
        case -1:
            newh->next = q;
            newh = q;
            q = q->next;
            break;
        case 0:
            int sum = p->data.coef + q->data.coef;
            int remain = sum % 10;
            if (sum != 0.0)
            {
                newp = (PolyNode*)malloc(sizeof(PolyNode));
                if (newp)
                {
                    newp->data.coef = remain;
                    newp->data.exp = q->data.exp;
                    newp->next = NULL;
                    newh->next = newp;
                    newp->data.coef = newp->data.coef + (sum - (sum % 10)) / 10;
                    newh = newp;
                }
            }
            p = p->next;
            q = q->next;
            break;
        }
    }
    newh->next = p ? p : q;
    return hc;
}
void ExchangePoly(PolyNode* head)
{
    PolyNode* p, * q, * tmp;
    p = (PolyNode*)malloc(sizeof(PolyNode));
    q = (PolyNode*)malloc(sizeof(PolyNode));
    tmp = (PolyNode*)malloc(sizeof(PolyNode));
    p = head->next;
    while (p)
    {
        q = p->next;
        while (q)
        {
            if (q->data.exp > p->data.exp)
            {
                if (tmp)
                {
                    tmp->data = p->data;
                    p->data = q->data;
                    q->data = tmp->data;
                }
            }
            q = q->next;
        }
        p = p->next;
    }
}
void PrintPoly(FILE* fp, PolyNode* head)
{
    ExchangePoly(head);
    PolyNode* s = head->next;
    while (s)
    {
        printf("(%d %d)\n", s->data.coef, s->data.exp);
        fprintf(fp, "(%d,%d)\n", s->data.coef, s->data.exp);
        s = s->next;
    }
}

int main()
{
    FILE* fp;
    char str[] = "D:\\visualstudio\\数据结构作业\\input.txt";
    PolyNode* ha, * hb, * hc;
    PolyArray** array = NULL;
    if (array)
    {
        array[0] = (PolyArray*)malloc(sizeof(PolyArray) * 128);
        array[1] = (PolyArray*)malloc(sizeof(PolyArray) * 128);
    }
    IntArray(array);
    fopen_s(&fp, str, "at");
    if (!fp)
    {
        printf("open file error");
        return 0;
    }
    CreatePoly(&ha, 12, array[0]);
    CreatePoly(&hb, 12, array[1]);
    hc = (PolyNode*)malloc(sizeof(PolyNode));
    AddPoly(ha, hb, hc);
    PrintPoly(fp, hc);
    free(ha);
    free(hb);
    free(hc);
    free(array[0]);
    free(array[1]);
    fclose(fp);

    return 0;
}

  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2023-03-21 20:41
    关注
    
    PolyArray** array = NULL;
        if (array)
        {
            array[0] = (PolyArray*)malloc(sizeof(PolyArray) * 128);
            array[1] = (PolyArray*)malloc(sizeof(PolyArray) * 128);
        }
    
    

    这个if代码块就是废代码啊,array肯定是NULL啊

    int* i = NULL;
        i[0] = 0, i[1] = 0;
    
    

    这代码也是错的,马上崩溃了啊。i指针你没分配空间啊!!!就开始赋值啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 3月29日
  • 已采纳回答 3月21日
  • 创建了问题 3月21日

悬赏问题

  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥60 GFSK两点调制锁相环simulink模型
  • ¥20 双非跨考工科哪个专业和方向就业前景好?
  • ¥20 求会6sv辐射传输模型,辅导(可py6s🙏🏻有偿
  • ¥15 .xla后缀的文件拖到excel里什么内容也没有怎么办
  • ¥20 Workbench中Mechanical打不开、闪退是什么原因?
  • ¥240 MapReduce应用实践 学生课程
  • ¥15 hlss视频显示AUTHORITY_INVALID
  • ¥15 MAX9296A+MAX96717,美信gmsl解串有人做过吗?
  • ¥15 求帮我解决一下inode 爆满的问题(有偿)