| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3242 人关注过本帖, 1 人收藏
标题:一元多项式的C程序
只看楼主 加入收藏
jiang107
Rank: 1
来 自:西安
等 级:新手上路
帖 子:9
专家分:0
注 册:2008-4-12
收藏(1)
 问题点数:0 回复次数:3 
一元多项式的C程序
我现在学的是数据结构,老师留个作业是C语言完成一元多项式的相加,麻烦各位能给我一个具体点的思路,如果可以最好给下程序的大概过程,2项相加或3项相加都可以,请注明。
搜索更多相关主题的帖子: 多项式 数据结构 C语言 相加 
2008-05-03 10:24
lili123
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-5-24
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{ /*多项式的存储结构定义*/
int coef;
int expn;
struct LNode *next;
}LNode,*linklist;
linklist creat()
{ /*一元多项式以链表存储形式的建立*/
linklist head,s,p,pre;
int coef;
int expn;
head=(linklist)malloc(sizeof(LNode));
head->next=NULL;
printf("put in coef and expn:");
scanf("%d %d",&coef,&expn);
while (coef!=0)
{
s=(linklist)malloc(sizeof(LNode));
s->coef=coef;
s->expn=expn;
s->next=NULL;
pre=head;
p=head->next;
while (p&&p->expn>s->expn)
{
pre=p;
p=p->next;
}
s->next=p;
pre->next=s;
printf("put in coef and expn:");
scanf("%d %d",&coef,&expn);
printf("\n");
}
return head;
}
linklist add(linklist La,linklist Lb)
{ /*两个一元多项式的加法运算*/
linklist Lc,pa,pb,pt,pc;
int temp;
Lc=(linklist)malloc(sizeof(LNode));
Lc->next=NULL;
pa=La->next;
pb=Lb->next;
pc=Lc;
while (pa&&pb)
{
if (pa->expn==pb->expn)
{
pt=(linklist)malloc(sizeof(LNode));
pt->expn=pa->expn;
pt->coef=pa->coef+pb->coef;
pt->next=NULL;
pa=pa->next;
pb=pb->next;
}
else if (pa->expn>pb->expn)
{
pt=(linklist)malloc(sizeof(LNode));
pt->expn=pa->expn;
pt->coef=pa->coef;
pt->next=NULL;
pa=pa->next;
}
else
{
pt=(linklist)malloc(sizeof(LNode));
pt->expn=pb->expn;
pt->coef=pb->coef;
pt->next=NULL;
pb=pb->next;
}
pc->next=pt;
pc=pt;
}
if (pa)
pc->next=pa;
else
pc->next=pb;
return Lc;
}
int main ()
{
}

2008-05-24 22:57
tang5188
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-6-25
收藏
得分:0 
//关于申请空间的释放问题 ,还没有解决……   表达式的加减乘都有。这是我的课程设计,还没有最后改进好,但是已经可以编译和正确运行并输出结果了。输入的时候,先输系数值按回车,再输指数值按回车,一直到输入结束。不用按指数大小顺序输入,程序会自动按指数大小降序排列多项式。如果输入同一个多项式时候,输入了两个相同的指数,程序则会将他们对应的系数相加……  自己看吧!  第一次课程设计啊!!
#include"iostream"
#include<stdlib.h>
using namespace std;
#define NULL 0

typedef struct node{
    int zhishu,xishu;
    struct node *next;
}Nodetype,*PNodetype;               //定义结点类型

PNodetype Creat_(void)              //创建表达式
{
    PNodetype Head,p,pre,pree;
    int x,z;
    Head=(PNodetype)malloc(sizeof(Nodetype));
    Head->next=NULL;
    printf("When you input xishu==0,the programe will finish!\n");
    printf("Please input the first xishu:");
    scanf("%d",&x);
    if(x==0)
    {
        p=(PNodetype)malloc(sizeof(PNodetype));
        p->xishu=0;
        p->zhishu=0;
        Head->next=p;
        p->next=NULL;
    }
    while(x!=0)
    {
        printf("Please input the zhishu:");
        scanf("%d",&z);
        p=(PNodetype)malloc(sizeof(Nodetype));
        p->xishu=x;
        p->zhishu=z;
        pre=Head;
        while(pre->next&&pre->next->zhishu>=z)
        {
            pree=pre;
            pre=pre->next;
        }
        p->next=pre->next;
        pre->next=p;
        if(pre->zhishu==p->zhishu)
        {
            pre->xishu+=p->xishu;
            pre->next=p->next;
            free(p);
        }
        if(pre->xishu==0)
        {
            pree->next=pre->next;
            free(pre);
        }
        printf("Please input next xishu:");
        scanf("%d",&x);
    }
    if(Head->next==NULL)
    {
        pre=(PNodetype)malloc(sizeof(PNodetype));
        pre->xishu=0;
        pre->zhishu=0;
        pre->next=Head->next;
        Head->next=pre;
    }
    return Head;
}

void OutPut_(PNodetype Head)         //输出表达式
{        
    PNodetype pre;
    pre=Head->next;
    if(pre->zhishu==0)
        printf("%d",pre->xishu);
    else
        printf("%d*X(%d)",pre->xishu,pre->zhishu);
    pre=pre->next;
    while(pre)
    {
        if(pre->zhishu==0)
        {
            if(pre->xishu>0)
                printf("+%d",pre->xishu);
            else if(pre->xishu<0)
                printf("%d",pre->xishu);
        }
        else
        {
            if(pre->xishu>0)
                printf("+%d*X(%d)",pre->xishu,pre->zhishu);
            else if(pre->xishu<0)
                printf("%d*X(%d)",pre->xishu,pre->zhishu);
        }
        pre=pre->next;
    }
       printf("\n");
}

PNodetype Plus_(PNodetype H1,PNodetype H2)                   //表达式相加
{
    PNodetype H3,p1,p2,p3,pre;
    H3=(PNodetype)malloc(sizeof(PNodetype));       
    H3->next=NULL;         
    p1=H1->next;        
    p2=H2->next;      
    pre=H3;
    while(p1&&p2)
    {
        if(p1->zhishu>p2->zhishu)
        {
            p3=(PNodetype)malloc(sizeof(PNodetype));
            p3->zhishu=p1->zhishu;
            p3->xishu=p1->xishu;
            p3->next=pre->next;
            pre->next=p3;
            pre=p3;
            p1=p1->next;
        }
        else if(p1->zhishu<p2->zhishu)
        {
            p3=(PNodetype)malloc(sizeof(PNodetype));
            p3->zhishu=p2->zhishu;
            p3->xishu=p2->xishu;
            p3->next=pre->next;
            pre->next=p3;
            pre=p3;
            p2=p2->next;
        }
        else if(p1->xishu+p2->xishu!=0)
        {
            p3=(PNodetype)malloc(sizeof(PNodetype));      
            p3->zhishu=p1->zhishu;
            p3->xishu=p1->xishu+p2->xishu;
            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=(PNodetype)malloc(sizeof(PNodetype));   
            p3->zhishu=p2->zhishu;
            p3->xishu=p2->xishu;
            p3->next=pre->next;
            pre->next=p3;
            pre=p3;
            p2=p2->next;
        }
    }
    else if(p1)
    {
        while(p1)
        {
            p3=(PNodetype)malloc(sizeof(PNodetype));
            p3->zhishu=p1->zhishu;
            p3->xishu=p1->xishu;
            p3->next=pre->next;
            pre->next=p3;
            pre=p3;
            p1=p1->next;
        }
    }
    if(H3->next==NULL)
    {
        p3=(PNodetype)malloc(sizeof(PNodetype));         
        p3->xishu=0;
        p3->zhishu=0;
        p3->next=NULL;
        H3->next=p3;
    }            
    return H3;
}

PNodetype Minus_(PNodetype H1,PNodetype H2)                 //表达式相减
{
    PNodetype H3,pre;
    pre=H2->next;
    while(pre)
    {
        pre->xishu=-pre->xishu;
        pre=pre->next;
    }
    H3=Plus_(H1,H2);
    pre=H2->next;
    while(pre)
    {
        pre->xishu=-pre->xishu;
        pre=pre->next;
    }
    return H3;
}

PNodetype Multiply_(PNodetype H1,PNodetype H2)            //表达式相乘
{
    PNodetype H3,H4,p1,p2,p3,pre;
    p2=H2->next;
    H4=(PNodetype)malloc(sizeof(PNodetype));
    H4->next=NULL;
    while(p2)
    {
        H3=(PNodetype)malloc(sizeof(PNodetype));
        pre=H3;
        H3->next=NULL;
        p1=H1->next;
        while(p1)
        {
            p3=(PNodetype)malloc(sizeof(PNodetype));
            p3->xishu=p1->xishu*p2->xishu;
            p3->zhishu=p1->zhishu+p2->zhishu;
            p3->next=pre->next;
            pre->next=p3;
            pre=p3;
            p1=p1->next;
        }
        H4=Plus_(H4,H3);
        p2=p2->next;
    }
    return H4;
}

void main()
{
    PNodetype Head1,Head2,Head3,Head4,Head5;
    Head1=Creat_();
    printf("表达式Head1为:");
    OutPut_(Head1);
    Head2=Creat_();
    printf("表达式Head2为:");
    OutPut_(Head2);
    Head3=Plus_(Head1,Head2);
    printf("Head3=Head1+Head2\n");
    printf("表达式Head3为:");
    OutPut_(Head3);
    Head4=Minus_(Head1,Head2);
    printf("Head4=Head1-Head2\n");
    printf("表达式Head4为:");
    OutPut_(Head4);
    Head5=Multiply_(Head1,Head2);
    printf("Head5=Head1*Head2\n");
    printf("表达式Head5为:");
    OutPut_(Head5);
}

[[it] 本帖最后由 tang5188 于 2008-6-25 15:25 编辑 [/it]]
2008-06-25 15:19
suncindy
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-10-10
收藏
得分:0 
谢谢,我好有用啊
2008-10-10 20:55
快速回复:一元多项式的C程序
数据加载中...
 
   



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

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