| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 712 人关注过本帖
标题:一元多项式的简单计算
只看楼主 加入收藏
Sammy287
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-7-2
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
一元多项式的简单计算
输入两个一元多项式实现相加,相减。但运行结果没有将相同指数的系数合并,求大神找错误!!!
#include<stdio.h>
#include<stdlib.h>
typedef struct Lnode
{
    int coef;  //系数
    int exp;     //指数
    struct Lnode *next;
}Lnode,*LinkList;
//用尾插法建立一元多项式的链表
LinkList create()
{
    LinkList h;
    Lnode *r,*s;
    int c;
    int e;     
    h=(LinkList)malloc(sizeof(Lnode));
    h->next=NULL;
    r=h;
    printf("输入系数:");
    scanf("%d",&c);
    printf("输入指数:");
    scanf("%d",&e);
    while(c!=0)
    {
        s=(Lnode*)malloc(sizeof(Lnode));
        s->coef=c;
        s->exp=e;
        r->next=s;
        r=s;
        printf("输入系数:");
        scanf("%d",&c);
        printf("输入指数:");
        scanf("%d",&e);
    }
    r->next=NULL;
    return(h);
}
//多项式的输出
void print(LinkList h)
{
    LinkList pre;
    pre=h->next;
    if(pre->exp==0)
        printf("%d",pre->coef);
    else printf("%d*x^%d",pre->coef,pre->exp);
    pre=pre->next;
    while(pre)
    {
        if(pre->exp==0)
        {
            if(pre->coef>0)
                printf("+%d",pre->coef);
            else printf("%d",pre->coef);
        }
        else
        {
            if(pre->coef>0)
                printf("+%d*x^%d",pre->coef,pre->exp);
            else printf("%d*x^%d",pre->coef,pre->exp);
        }
        pre=pre->next;
    }
}
//两个多项式的加法
LinkList add(LinkList H1,LinkList H2)
{
    LinkList H3,p1,p2,p3,pre;
    H3=(LinkList)malloc(sizeof(Lnode));      
    H3->next=NULL;   
    p1=H1->next;
    p2=H2->next;
    pre=H3;
    while(p1&&p2)
    {
        if(p1->exp>p2->exp)
        {
            p3=(LinkList)malloc(sizeof(Lnode));
            p3->coef=p1->coef;
            p3->exp=p1->exp;
            p3->next=pre->next;
            pre->next=p3;
            pre=p3;
            p1=p1->next;
        }
        else if(p1->exp<p2->exp)
        {
            p3=(LinkList)malloc(sizeof(Lnode));
            p3->coef=p2->coef;
            p3->exp=p2->exp;
            p3->next=pre->next;
            pre->next=p3;
            pre=p3;
            p2=p2->next;
        }               
        else if(p1->coef+p2->coef!=0)
        {
           p3=(LinkList)malloc(sizeof(Lnode));
           p3->coef=p1->coef+p2->coef;
           p3->exp=p1->exp;
           p3->next=pre->next;
           pre->next=p3;
           pre=p3;
           p1=p1->next;
           p2=p2->next;
            }
        else
        {
            p1=p1->next;
            p2=p2->next;
        }
    }
    if(p2)
        {
           while(p2)
           {
             p3=(LinkList)malloc(sizeof(Lnode));
             p3->coef=p2->coef;
             p3->exp=p2->exp;
             p3->next=pre->next;
             pre->next=p3;
             pre=p3;
             p2=p2->next;
           }
        }
        else if(p1)
        {
             while(p1)
            {
             p3=(LinkList)malloc(sizeof(Lnode));
             p3->coef=p1->coef;
             p3->exp=p1->exp;
             p3->next=pre->next;
             pre->next=p3;
             pre=p3;
             p1=p1->next;
           }
         }
      if(H3->next==NULL)
      {
        p3=(LinkList)malloc(sizeof(Lnode));         
        p3->coef=0;
        p3->exp=0;
        p3->next=NULL;
        H3->next=p3;
      }
      return H3;
}     
//多项式的减法
LinkList substract(LinkList H1,LinkList H2)
{
    LinkList H3,pre;
    pre=H2->next;
    while(pre)
    {
        pre->coef=(-1)*pre->coef;
        pre=pre->next;
    }
    H3=add(H1,H2);
    return H3;
}
void main()
{
    LinkList head1,head2,head3,head4;
    head1=create();
    printf("表达式head1为:");
    print(head1);
    printf("\n");
    head2=create();
    printf("表达式head2为:");
    print(head2);
    printf("\n");
    head3=add(head1,head2);
    printf("表达式head3为:");
    print(head3);
    printf("\n");
    head4=substract(head1,head2);
    printf("表达式head4为:");
    print(head4);
    printf("\n");
}

搜索更多相关主题的帖子: include create 多项式 
2013-07-04 08:15
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:20 
调试下

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-07-10 20:43
快速回复:一元多项式的简单计算
数据加载中...
 
   



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

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