| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 307 人关注过本帖
标题:多项式合并的问题,请帮忙!!!
只看楼主 加入收藏
lizjohn
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2010-10-28
结帖率:70.59%
收藏
 问题点数:0 回复次数:0 
多项式合并的问题,请帮忙!!!
#include<stdio.h>
#include<stdlib.h>
struct list
{
    int exp;                //指数
    int ceof;                //系数
    struct list*next;
};
struct list* creat()                   //建立
{
    struct list*p1,*p2,*sq;
    p2=sq=(struct list*)malloc(sizeof(struct list));
    p1=(struct list*)malloc(sizeof(struct list));
    scanf(" %d,%d",&p1->exp,&p1->ceof);
    while(p1->exp!='#')
    {
        p2->next=p1;
        p2=p1;
        p1=(struct list*)malloc(sizeof(struct list));
        scanf(" %d%d",&p1->exp,&p1->ceof);
    }
    p2->next=NULL;
    return sq;
}
int compare(int x,int y)               //比较两多项式的指数
{
    if(x<y)
        return 1;
    if(x>y)
        return -1;
    if(x=y)
        return 0;
}
void main()
{
    struct list*l1,*l2,*pa,*pb,*ha,*hb,*t;
    int a,b,flag,sum;
    printf("多项式1:\n");
    l1=creat();
    printf("多项式2:\n");
    l2=creat();
    pa=l1->next;
    pb=l2->next;
    ha=l1;
    hb=l2;
    while((pa!=NULL)&&(pb!=NULL))
    {
        a=pa->exp;
        b=pb->exp;
        flag=compare(a,b);
        switch(flag)                            //通过比较,将新的多项式接在l1上
        {
        case 1:
            ha=pa;
            pa=pa->next;
            break;
        case -1:
            sum=(pa->ceof+pb->ceof);
            if(sum!=0)
            {pa->ceof=sum;
            ha=pa;
            pa=pa->next;
            }
            else
            {
                ha->next=pa->next;
                free(pa);
                hb->next=pb->next;
                free(pb);
                pb=hb->next;
            }
            break;
        case 0:
            pb->next=ha->next;
            ha->next=pb;
            ha=pb;
            pb=hb->next;
            break;
        }
        
        if(pb!=NULL)
            ha->next=pb;
    }
    t=l1->next;
    while(t!=NULL)
    {
        printf(" %d,%d\n",t->exp,t->ceof);
        t=t->next;
    }
   
}


我想实现两多项式的合并,但从输入就有问题,一直停不下来,好像陷入死循环,死机了,请高手帮我!!!
搜索更多相关主题的帖子: 多项式 
2010-11-10 13:24
快速回复:多项式合并的问题,请帮忙!!!
数据加载中...
 
   



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

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