| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 486 人关注过本帖
标题:关于“链式存储结构实现一元多项式的相加运算”的问题,请大神指教
只看楼主 加入收藏
c语言新手yu
Rank: 2
等 级:论坛游民
帖 子:38
专家分:17
注 册:2012-11-5
结帖率:62.5%
收藏
 问题点数:0 回复次数:0 
关于“链式存储结构实现一元多项式的相加运算”的问题,请大神指教
代码.rar (1.28 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
检测的时候没错,但有些功能运行不了
图片附件: 游客没有浏览图片的权限,请 登录注册
应该怎么改,求指教#include<stdio.h>
#include<stdlib.h>
typedef struct pnode
{
    int exp;
    float coef;
    struct pnode *next;
}polynode;
polynode *A,*B,*C,*D;
polynode *creatA()
{
    polynode *p1,*r;
    int i,n;
    A=(polynode*)malloc(sizeof(polynode));
    A->next=NULL;
    r=A;
    printf("请输入A多项式的项数:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        p1=(polynode*)malloc(sizeof(polynode));
        printf("请输入A的第i项的系数和指数:",i);
        scanf("%f%d",&p1->coef,&p1->exp);
        r->next=p1;
        r=p1;
    }
    r->next=NULL;
    return A;
}
polynode *creatB()
{
    polynode *p2,*r;
    int i,n;
    B=(polynode*)malloc(sizeof(polynode));
    B->next=NULL;
    r=B;
    printf("请输入B多项式的项数:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        p2=(polynode*)malloc(sizeof(polynode));
        printf("请输入B的第i项的系数和指数:",i);
        scanf("%f%d",&p2->coef,&p2->exp);
        r->next=p2;
        r=p2;
    }
    r->next=NULL;
    return B;
}
void printA(polynode *A)
{
    polynode *p1;
    p1=A->next;
    while(p1->next!=NULL)
    {
        if(p1->next->coef>0)
        printf("%.2fx^%d+",p1->coef,p1->exp);
        else
        printf("%.2fx^%d",p1->coef,p1->exp);
        p1=p1->next;
    }
    printf("%.2fx^%d",p1->coef,p1->exp);

}
void printB(polynode *B)
{
    polynode *p2;
    p2=B->next;
    while(p2->next!=NULL)
    {
        if(p2->next->coef>0)
        printf("%.2fx^%d+",p2->coef,p2->exp);
        else
        printf("%.2fx^%d",p2->coef,p2->exp);
        p2=p2->next;
    }
    printf("%.2fx^%d",p2->coef,p2->exp);

}
void printC(polynode *C)
{
    polynode *p=C->next;
    while(p->next!=NULL)
    {
        if(p->next->coef>0)
        printf("%.2fx^%d+",p->coef,p->exp);
        else
        printf("%.2fx^%d",p->coef,p->exp);
        p=p->next;
    }
    printf("%.2fx^%d",p->coef,p->exp);
}

polynode *polyadd(polynode *A,polynode *B)
{
    polynode *p1,*p2,*p,*C;
    float x;
    p1=A->next;
    p2=B->next;
    p=(polynode*)malloc(sizeof(polynode));
    p->next=NULL;
    C=p;
    while(p1&&p2)
    {
        if(p1->exp==p2->exp)
        {
            x=p1->coef+p2->coef;
            if(x!=0)
            {
                p->next=(polynode*)malloc(sizeof(polynode));
                p=p->next;
                p->coef=x;
                p->exp=p1->exp;
            }
            p1=p1->next;
            p2=p2->next;
        }
        else
        {
            p->next=(polynode*)malloc(sizeof(polynode));
            p=p->next;
            if(p1->exp>p2->exp)
            {
                p->coef=p2->coef;
                p->exp=p2->exp;
                p2=p2->next;
            }
            else
            {
                p->coef=p1->coef;
                p->exp=p1->exp;
                p1=p1->next;
            }
        }
    }
    while(p1!=NULL)
    {
         p->next=(polynode*)malloc(sizeof(polynode));
         p=p->next;
         p->coef=p1->coef;
         p->exp=p1->exp;
         p1=p1->next;
    }
    while(p2!=NULL)
    {
         p->next=(polynode*)malloc(sizeof(polynode));
         p=p->next;
         p->coef=p2->coef;
         p->exp=p2->exp;
         p2=p2->next;
    }
    p->next=NULL;
    return C;
}

int main()
{
    polynode *p1,*p2,*p;
    int i;
    while(i)
    {
        printf("\t\t0:创建多项式A\n\t\t1:创建多项式B\n\t\t2:多项式A与多项式B相加\n\t\t3:输出多项式A\n\t\t4:输出多项式B\n\t\t5:输出相加后的多项式C\n");
        scanf("%d",&i);
        switch(i)
        {
        case 0:creatA();break;
        case 1:creatB();break;
        case 2:p=polyadd(A,B);break;
        case 3:printA(A);break;
        case 4:printB(B);break;
        case 5:printC(p);break;
        }
    printf("\n\t\t0:结束\n\t\t1:继续\n");
    scanf("%d",&i);
    }
}

[ 本帖最后由 c语言新手yu 于 2014-1-2 00:41 编辑 ]
搜索更多相关主题的帖子: include 多项式 
2014-01-02 00:37
快速回复:关于“链式存储结构实现一元多项式的相加运算”的问题,请大神指教
数据加载中...
 
   



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

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