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日

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?