数据结构课程设计_两个一元稀疏多项式相加 第4页

两个一元稀疏多项式相加_C语言

源程序清单

#include "stdio.h"

#define LEN sizeof(struct term)

#define NULL 0

 

 struct term{

       int coef;/*系数*/

    struct term *creatpolyn(void){

      struct term *p1,*p2,*head;

      int n,i;

     scanf("%d",&n);

       head=p1=p2=(struct term*)malloc(LEN);

       printf("Decline the sequence importation according to the index number\n");/*按指数降序列输入*/

printf("Input the format is coef,expn \n");/*输入格式是 系数,指数*/

       printf("input 1 term: ");

           scanf("%d,%d",&p1->coef,&p1->expn);

       for(i=2;i<=n;i++){

              p1=(struct term*)malloc(LEN);

              printf("input %d term:",i);

              scanf("%d,%d",&p1->coef,&p1->expn);

              p1->next=NULL;

              p2->next=p1;

              p2=p2->next;

       }/*for*/

  需要完整内容的请联系QQ3249114,本文免费,转发请注明源于www.751com.cn

           n=n+1;

           if(n==1){

           if(p1->expn>p2->expn){

              head=p3=p1;p1=p1->next;

              }/*if*/

else if(p1->expn==p2->expn){

              p1->coef=p1->coef+p2->coef;

           if(p1->coef==0){

                  n=0;p1=p1->next;p2=p2->next;

                         if(!p1)head=p2;

                         if(!p2)head=p1;

                         if(!p1&&!p2){

                                printf("answer is 0\n");

                                head=NULL;}/*if*/

                         }/*if*/

else{

                         head=p3=p1;p1=p1->next;p2=p2->next;

                         }/*else*/

                  }/*else if*/

else{

                  head=p3=p2;p2=p2->next;

                  }/*else*/

                  }/*if*/

                  else{

                         if(p1->expn>p2->expn){

                  p3->next=p1;p1=p1->next;p3=p3->next;

                  }/*if*/

           else if(p1->expn==p2->expn){

                  p1->coef=p1->coef+p2->coef;

                  if(p1->coef==0){

                         p1=p1->next;p2=p2->next;

                         } /*if*/

                  else{

                         p3->next=p1;p1=p1->next;p2=p2->next;p3=p3->next;

                         }/*else*/

                  }/*else if*/

           else{

                  p3->next=p2;p2=p2->next;p3=p3->next;

                  }/*else*/

           } /*else*/

                  }/*while*/

        while(p1!=NULL){

               p3->next=p1;p1=p1->next;p3=p3->next;

               }/*while*/

           while(p2!=NULL){

                  p3->next=p2;p2=p2->next;p3=p3->next;

                  }/*while*/

p3->next=NULL;

           return(head);

 }/*daapolyn*/

 

 struct term *printpolyn(struct term *p){

      int m=0;

while(p!=NULL){

                  m++;

printf("c%d=%d,e%d=%d\n",m,p->coef,m,p->expn);

                  p=p->next;

           }/*while*/

printf("number=%d\n",m);

           }/*printpolyn*/

 

main(){

       struct term *p1,*p2,*head;

       printf("Please input the first formula number: ");/*请输入第一个一元二项式项数*/

                  p1=creatpolyn();

    printf("Please input the second formula number: ");/*请输入第二个一元二项式项数*/

               p2=creatpolyn();

               head=addpolyn(p1,p2);

           printpolyn(head);       

}/*main*/                                        

上一页  [1] [2] [3] [4] 

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有