c实现一元多项式加乘

这学期数据结构老师是学校里最严格的
而我发现自己落后了
别人早就当天就把布置的实验完成了
我还........
惭愧
连夜补上  虽然自知不好
仅作纪念   要加油了


/***************************************************************************
 *   Copyright (C) 2008 by root   *
多项式  gcc 版本 4.1.2 20070925 (Red Hat 4.1.2-33)

 ***************************************************************************/


#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    double coef;
    int expn;
    struct node * next;
}lnode,*lklist;

int compare ( int,int );
void InsertLklist ( lklist L,double coef,int expn );  //有序插入
void CreateLklist ( lklist *L );
void InitLklist ( lklist *L );
void PrintPolyn ( lklist L );
int  DeleteLklist(lklist L,int expn);
lklist add(lklist a,lklist b);
lklist mul(lklist a,lklist b);
void ClearLklist(lklist L);
void show_function();                                          //显示程序功能菜单
void DestoryLklist(lklist L);

void DestoryLklist( lklist L)
{
    lklist p;
    lklist q=L;
    while(L->next)
    {
        p=L->next;
        L->next=p->next;
        free(p);
    }
    free(q);
    return;
   
}

void show_function()
    {
        printf("/n/n*****************请按数字键选择功能**********************/n");
        printf("/t1  ---  创建多项式a/t/t");
        printf("/t2  ---  创建多项式b/n");
        printf("/t3  ---  显示a+b/t/t");
        printf("/t4  ---  显示a*b/n");
        printf("/t5  ---  退出/n");
    }



void ClearLklist(lklist L)
{
    lklist p;
    while(L->next)
    {
        p=L->next;
        L->next=p->next;
        free(p);
    }
    return;
}

lklist mul(lklist a,lklist b)
{
    lklist c,d,e,f;
    InitLklist( &c);
   
    e=a->next;f=b->next;
    while(e!=NULL)
    {
        f=b->next;
        while(f!=NULL)
        {
            InsertLklist( c,(e->coef)*(f->coef),(e->expn)+(f->expn));
            f=f->next;
        }
        e=e->next;
    }
    return c;
   
   
}

lklist add(lklist a,lklist b)
{
    lklist c,d;
    InitLklist(&c);
    d=a->next;
   
    while(d!=NULL)
    {
        InsertLklist( c,d->coef,d->expn);
        d=d->next;
    }
    d=b->next;
    while(d!=NULL)
    {
        InsertLklist( c,d->coef,d->expn);
        d=d->next;
    }
    return c;
}

int  DeleteLklist(lklist L,int expn)
{
    lklist p,q;
    p=L;
    while(p!=NULL&&p->expn!=expn)
    {
        q=p;
        p=p->next;
    }
    if(p!=NULL&&p->expn==expn)
    {
        q->next=p->next;
        free(p);
        return 1;
    }
    else
    {
        return 0;
    }
}


void PrintPolyn ( lklist L )

{
    lklist p;
    p=L->next;
    while ( p!=NULL )
    {
        if(p->expn!=0)
        {
            if(p->coef==1)
            {
                if(p->expn!=1)
                {
                printf ( "x^%d",p->expn );}
                else
                {
                    printf ( "x");
                }
            }
            else
            {
                if(p->expn!=1)
                {
                printf ( "%lfx^%d", p->coef,p->expn );}
                else
                {
                    printf ( "%lfx", p->coef);
                   
                }
            }
           
        }
        else
        {
            printf ( "%lf",p->coef );
        }
       
        if ( p->next!=NULL )
        {
            if(p->next->coef>0)
            {
                printf ( " + " );
            }
            else
            {
                printf ( " " );
            }
           
         
       
        }
        p=p->next;
    }
   
    printf("/n");
}


void CreateLklist ( lklist *L )
{
    double coef;
    int expn;
    InitLklist ( L );
    int flag=1;
    while ( flag )
    {
        printf ( "输入插入的系数和次数 以 , 隔开/n" );
        scanf ( "%lf,%d",&coef,&expn );
        getchar();
   
        InsertLklist ( *L,coef,expn );
        printf ( " c 继续输入 q退出/n" );
        if ( getchar()!='c' )
        {
          getchar();
          break;
        }
        getchar();
    }
}



void InsertLklist ( lklist L,double coef,int expn )
{
   
    lklist p,q,s;
    p=L;
    q=L;

    while((p->expn)<expn&&p->next!=NULL)
    {       
        q=p;
        p=p->next;
    }
   
    //printf("dang qian p   %lf   %d/n",p->coef,p->expn);   
    if(expn<q->expn||(p==NULL))
    {
       
        printf("输入数据有误  不能插入/n");
    }
   
    if(expn==(p->expn))
    {
        p->coef=p->coef+coef;
        if(p->coef==0)
        {
            DeleteLklist(L,p->expn);
        }
       
        return;
    }
    if(expn>(p->expn))
    {
        s= ( lklist ) malloc ( sizeof ( lnode ) );
        s->expn=expn;
        s->coef=coef;
        //printf("dangqian s  %lf  ,  %d/n",s->coef,s->expn);
        s->next=p->next;
        p->next=s;
       
        //printf("dddddangqian sddddp  %lf  ,  %d/n",p->next->coef,p->next->expn);
        //PrintPolyn ( L );
        return;
    }
   
    if(expn>(q->expn))
    {
        s= ( lklist ) malloc ( sizeof ( lnode ) );
        s->expn=expn;
        s->coef=coef;
        //printf("dangqian s  %lf  ,  %d/n",s->coef,s->expn);
        s->next=q->next;
        q->next=s;
        //printf("dddddangqian sddddq  %lf  ,  %d/n",q->next->coef,q->next->expn);
        //PrintPolyn ( L );
        return;
    }



   

   
   

}


void InitLklist ( lklist *L )
{
    lklist s;
    s= ( lklist ) malloc ( sizeof ( lnode ) );
    s->coef=0;
    s->expn=-1;
    s->next=NULL;
    *L=s;
}


int compare ( int a,int b )
{
    if ( a==b )
    {
        return 0;
    }
    if ( a>b )
    {
        return 1;
    }
    if ( a<b )
    {
        return -1;
    }
}




int main ( int argc, char *argv[] )
{
    lklist a,b,c;
    InitLklist(&c);
   
    int flag=1;
    show_function();
    char x;
    while(flag)
    {
        x=getchar();
        getchar();
        switch(x)
        {
            case '1':
                CreateLklist(&a);
                printf("多项式a创建成功 显示如下:  /n");
                PrintPolyn( a);
                show_function();
               
                break;
            case '2':
                CreateLklist(&b);
                printf("多项式b创建成功 显示如下:  /n");
                PrintPolyn( b);
                show_function();
               
                break;
            case '3':
                ClearLklist(c);
                c=add(a,b);
                PrintPolyn( c);
                show_function();
               
                break;
            case '4':
                ClearLklist( c);
                c=mul( a,b);
                PrintPolyn( c);
                show_function();
                break;
            case '5':
                DestoryLklist(  a);
                DestoryLklist( b);
                DestoryLklist( c);
                flag=0;
                break;
            default :
               
                printf("Chose error!/n");
                break;
        }
    }
   

    return EXIT_SUCCESS;
}















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值