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;
}















發佈了29 篇原創文章 · 獲贊 2 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章