标题:求助 C单链表 一元多项式相减 帮我看一下
只看楼主
milllionclou
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-3-2
 问题点数:0 回复次数:0 
求助 C单链表 一元多项式相减 帮我看一下
第一行是多项式A;第二行是多项式B。输入0 0结束多项式的输入。

输入的多项式A和B的最多项数均不超过500,系数的绝对值不超过10000,指数大于等于0.

例如,
输入:
3 5 1 0 0 0 表示 3x5+1
9 6 0 0 表示 9x6
输出
输出结果只有一行,即A-B的结果。如果链表为空则输出“0”。

多项式输出时的要求:
(1)结果多项式按降幂方式排列。

(2)多项式中除常数项外的每一项的形式为 AnXN,其中An是一个整数,表示该项的系数,X是变量,N是该项的指数。

(3)多项式中的系数为0的项不输出;系数为1的项不显示系数。

(4)多项式中的各项与'+' 或 '-' 之间 没有 空格符。
输入样列
3 5 1 0 0 0
9 6 0 0
输出样例
-9x6+3x5+1
我写的
#include<stdio.h>
#include<stdlib.h>
typedef struct PolNode
{
    int coef;
    int exp;
    struct PolNode *next;
}PolNode,*PolList;
PolList CreateList(PolList head)/*初始化创建单链表*/
{
    PolNode *r,*s;
    int c,e;
    head=(PolList)malloc(sizeof(PolNode));
    head->next=NULL;
    r=head;
    while(scanf("%d%d",&c,&e))
    {
        if(c==0&&e==0)
            break;
        else{
        s=(PolNode *)malloc(sizeof(PolNode));
        s->coef=c;s->exp=e;
        r->next=s;
        r=s;
        }
    }
    r->next=NULL;
    return head;
}
PolList PolAdd(PolList LA,PolList LB)/*两表相加*/
{
    PolNode *pa,*pb,*r,*temp;
    int sum;
    pa=LA->next;
    pb=LB->next;
    r=LA;
    while(pa!=NULL&&pb!=NULL)
    {
        if(pa->exp > pb->exp)
        {
            r->next=pa;
            r=pa;
            pa=pa->next;
        }
        else if(pa->exp < pb->exp)
        {
            r->next=pb;
            r=pb;
            pb=pb->next;
        }
        else
        {
            sum=pa->coef +pb->coef;
            if(sum!=0)
            {
                pa->coef=sum;
                r->next=pa;
                r=pa;
                pa=pa->next;
            }
            else
            {
                temp=pa;
                pa=pa->next;
                free(temp);
            }
            temp=pb;
            pb=pb->next;
            free(temp);
        }
    }
    if(pa!=NULL)
        r->next=pa;
    else
        r->next=pb;
    free(LB);
    return LA;
}
void Output(PolList LA)
{
    PolNode *p=LA->next;
    if(p==NULL)
        printf("0\n");
    else
    {
        if(p->coef==1)
        {
            if(p->exp==0)
                printf("1");
            else if(p->exp==1)
                printf("x");
            else
                printf("x%d",p->exp);
            p=p->next;
        }
        else if(p->coef==0)
            p=p->next;
        else
        {
            if(p->exp==0)
                printf("%d",p->coef);
            else if(p->exp==1)
                printf("%dx",p->coef);
            else
                printf("%dx%d",p->coef,p->exp);
            p=p->next;
        }
        while(p!=NULL)
        {
            if(p->coef==1)
            {
                if(p->exp==0)
                    printf("+1");
                else if(p->exp==1)
                    printf("+x");
                else
                    printf("+x%d",p->exp);
                p=p->next;

            }
            else if(p->coef==0)
                p=p->next;
            else if(p->coef >0)
            {
                if(p->exp==0)
                    printf("+%d",p->coef);
                else if(p->exp==1)
                    printf("+%dx",p->coef);
                else
                    printf("+%dx%d",p->coef,p->exp);
                p=p->next;
            }
            else
            {
                if(p->exp==0)
                    printf("%d",p->coef);
                else if(p->exp==1)
                    printf("%dx",p->coef);
                else
                    printf("%dx%d",p->coef,p->exp);
                p=p->next;
            }
            
        }
        printf("\n");
    }
}
PolList NegaList(PolList LB)/*将被减表转成负值*/
{
    PolNode *p=LB->next;
    while(p!=NULL)
    {
        p->coef=-p->coef;
        p=p->next;
    }
    return LB;
}
int main()
{
    PolList LA=NULL,LB=NULL;
    LA=CreateList(LA);
    LB=CreateList(LB);
    LB=NegaList(LB);
    LA=PolAdd(LA,LB);
    Output(LA);
    return 0;
}
求解哪里错了
搜索更多相关主题的帖子: 系数 指数 多项式 绝对值 
2012-03-02 20:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-362073-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.263023 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved