| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1287 人关注过本帖, 1 人收藏
标题:单链表实现一元多项式相加
只看楼主 加入收藏
ganruoxun
Rank: 1
来 自:广州
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-10-12
收藏(1)
 问题点数:0 回复次数:1 
单链表实现一元多项式相加
帮我看看这个程序有什么错误,谢谢
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>  
#include <math.h>
#include <process.h>
#include <iostream.h>

typedef struct LNode
{ /*多项式的存储结构定义*/
    int coef;
    int expn;
    struct LNode *next;
}LNode,*polynomail;

void creatpolyn(polynomail &p,int m)
{
    int i;
    int coef;
    int expn;
    polynomail s;
    p=(polynomail)malloc(sizeof(LNode));
    p->next = NULL;    
    for(i=1;i<=m;++i)
    {
        s=(polynomail)malloc(sizeof(LNode));
        printf("请输入一元多项式pa的系数和指数:");
        scanf("%d %d",s->coef,s->expn);
        printf("\n");
        s->coef=coef;
        s->expn=expn;
        s->next=p->next;
        p->next=s;
    }
}//CreatPolyn


void addpolyn(polynomail &pa,polynomail &pb)
{
    polynomail qa,qb,c,pc;
    pc=(polynomail)malloc(sizeof(LNode));
    pc->next=NULL;
    qa=pa->next;
    qb=pb->next;
    while(qa&&qb)
    {
        if(qa->expn=qb->expn)
        {
            c=(polynomail)malloc(sizeof(LNode));
            c->expn=pa->expn;
            c->coef=qa->coef+qb->coef;
            c->next=NULL;
            qa=qa->next;
            qb=qb->next;
        }
        if(qa->expn>qb->expn)
        {
            c=(polynomail)malloc(sizeof(LNode));
            c->expn=qa->expn;
            c->coef=qa->coef;
            c->next=NULL;
            qa=qa->next;
        }
        if(qa->expn<qb->expn)
        {
            c=(polynomail)malloc(sizeof(LNode));
            c->expn=qb->expn;
            c->coef=qb->coef;
            c->next=NULL;
            qb=qb->next;
        }
        pc->next=c;
        pc=c;
    }//while
    if(qa)
        c->next=qa;
    else
        c->next=qb;

}//AddPolyn


void printpolyn(polynomail p)
{
    while(p->next!=NULL)
    {   
        p=p->next;
        printf(" %g*x^%d",p->coef,p->expn);   
    }

}

int main()
{
    int n,m;
    polynomail pa,pb;
    printf("请输入一元多项式pa的项数:");
    scanf("%d\n",n);
    printf("\n");
    creatpolyn(&pa,n);
    printf("请输入一元多项式pb的项数:");
    scanf("%d\n",m);
    printf("\n");
    creatpolyn(&pb,m);
    addpolyn(&pa,&pb);
    printf("结果是:pa+pb=");
    printpolyn(pa);
    printf("\n");
    return 0;
}
搜索更多相关主题的帖子: 多项式 单链 相加 
2008-10-15 22:27
guohuangbo
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2008-9-3
收藏
得分:0 
看不懂!可以写详细点不?
2008-10-21 12:33
快速回复:单链表实现一元多项式相加
数据加载中...
 
   



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

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