Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C N-多项式加法不起作用_C_Pointers_Linked List - Fatal编程技术网

C N-多项式加法不起作用

C N-多项式加法不起作用,c,pointers,linked-list,C,Pointers,Linked List,我不确定我是否听起来很笨,但我仍然想开发一个使用链表的程序,在这个程序中我可以添加多达5个多项式。我能想出的密码是 struct node { int exp,coeff; struct node *next; } *start[5]={NULL}; void read() { struct node *current,*newnode; int n,i,max,j; printf("How many poly?"); scanf("%d",&a

我不确定我是否听起来很笨,但我仍然想开发一个使用链表的程序,在这个程序中我可以添加多达5个多项式。我能想出的密码是

struct node
{
    int exp,coeff;
    struct node *next;
} *start[5]={NULL};

void read()
{
    struct node *current,*newnode;
    int n,i,max,j;
    printf("How many poly?");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("\nDetails of poly %d\n",i);
        printf("Max degree:");
        scanf("%d",&max);
        for(j=max;j>=0;j--)
        {
            newnode=(struct node *)malloc(sizeof(newnode));
            printf("\nEnter coeff of %d:",j);
            scanf("%d",&newnode->coeff);
            newnode->exp=j;
            if(start[i]==NULL)
            {
                printf("a%d",start[i]);
                start[i]=newnode;
                newnode->next=NULL;
                printf("%d,%d",start[i],start[i]->next);
            }
            else if(start[i]->next==NULL)
            {
                printf("b");
                start[i]->next=newnode;
                newnode->next=NULL;
                printf("%d",start[i]);
            }
            else
            {
                printf("c");
                current=start[i];
                while(current->next!=NULL)
                    current=current->next;
                current->next=newnode;
                newnode->next=NULL;
                printf("%d",start[i]);
            }   
        }
    }   
}

void sum()
{
    struct node *curr1,*curr2,*tmp;
    int i,co,ex;
    curr1=start[0];
    while(curr1!=NULL)
    {   
        for(i=1;i<5;i++)
        {
            co=0;
            if(curr1==start[i])
                tmp=start[i];
            else
                tmp=tmp->next;
            curr2=tmp;
            if(curr1->exp==curr2->exp)
            {
                if(co==0)
                    co=co+curr1->coeff+curr2->coeff;
                else
                    co=co+curr2->coeff;
                ex=curr1->exp;
                break;
            }
            else if(curr1->exp>curr2->exp)
            {   
                ex=curr1->exp;
                if(co==0)
                    co=co+curr1->coeff;
                else
                    break;
            }
            else
            {   
                ex=curr2->exp;
                if(co==0)
                    co=co+curr2->coeff;
                else
                    break;
            }
        }
        printf("%dX^%d",co,ex);
        curr1=curr1->next;
    }
}

int main()
{
    read();
    printf("result:\n");
    sum();
    getch();
    return 0;
}
打印b是为了检查程序是否进入了这个块LOL。 谁来帮我拿一下这个…

这部分

newnode=(struct node *)malloc(sizeof(newnode));  // This will allocate space for 
                                                 // a pointer to struct node
应该是

newnode=(struct node *)malloc(sizeof(struct node));  // This will allocate space for 
                                                     // a struct node
或者按照@chux的建议更好

newnode = malloc(sizeof *newnode);         // This will allocate space for 
                                           // whatever newnode points to. In
                                           // this case a struct node
我不确定这会有什么不同,但我会改变:

start[5]={NULL};
进入


调试器…………无关:至少空间非常便宜,使用them@4386427spacesareinctriticalshortage.usetab(如果可能)。我们可以一起度过这段困难时期谢谢大家,我将试用并更新推荐
newnode=malloc(sizeof*newnode)
start[5]={NULL};
start[5]={NULL, NULL, NULL, NULL, NULL};