关闭

链表实现多项式相加

标签: inputstruct编译器nullc++
2108人阅读 评论(3) 收藏 举报
分类:

//均已调试通过,编译器为DEV C++

 /多项式相加程序,用链表实现
//系数 coefficient  指数exponent   多项式 multinomial
 #include <stdio.h>
 typedef struct Lnode {
         int coe;
        int exp;
        struct Lnode *next;
        }Lnode,*LinkList;
   //*******************建立两个链表*************************    
int main()
    {
         
          Start:
            printf("Welcome to use!/n");         //????????????????????????????????
            int length1,length2;
           int i,j,sum;
           char ch;
        LinkList  L1=(LinkList)malloc(sizeof(Lnode));    //注意申请链表的方法
         LinkList   L2=(LinkList)malloc(sizeof(Lnode));
       
           printf("please input the A(x) length:/n");
               scanf("%d",&length1);
               printf("please input A(x) coefficient and exponent:/n");
             LinkList p1=L1;
             LinkList pr1=L1;           //不要因循环改变 L1
            for(i=0;i<length1;i++){ 
                   LinkList q1=(LinkList)malloc(sizeof(Lnode));
                   printf("coefficient:");
                    scanf("%d",&q1->coe);
                    printf("exponent:");
                    scanf("%d",&q1->exp);
                    p1->next=q1;
                    p1=q1;
                     q1->next=NULL;                                
                   }
             while(pr1->next){
               printf("%d*X^%d",pr1->next->coe,pr1->next->exp);
               pr1=pr1->next;
               if(pr1->next)
               printf(" + ");
               }
                  
           printf("/nplease input the B(x) length:/n");
                scanf("%d",&length2);
         
          printf("please input B(x) coefficient and exponent:/n");
             LinkList p2=L2;
             LinkList pr2=L2;
             for(j=0;j<length2;j++){
                 LinkList q2=(LinkList)malloc(sizeof(Lnode));
                 printf("coefficient:");
                    scanf("%d",&q2->coe);
                    printf("exponent:");
                    scanf("%d",&q2->exp);
                  p2->next=q2;
                   p2=q2;
                   q2->next=NULL;                
                } 
                while(pr2->next){
               printf("%d*X^%d",pr2->next->coe,pr2->next->exp);
                    pr2=pr2->next;
                    if(pr2->next)
                       printf(" + ");
               }
//******************合并两个多项式***************************
   
           LinkList p=L1;
            LinkList q=L2,temp;
               while(p->next&&q->next)   {
                 if(p->next->exp==q->next->exp)   //注意此处  ,
                     {   
                       sum=p->next->coe+q->next->coe;
                      if(sum)
                          {
                           p->next->coe=sum;
                         LinkList k2=q->next;
                          p=p->next;
                          q=q->next;
                      //   free(k2);       //不能释放结点
                         }
                      
                       else {
                        LinkList k1=p->next;
                        LinkList k2=q->next;
                           p->next=p->next->next;
                          p=p->next;
                          q=q->next;
                          // free(k1);
                           // free(k2);
                          }
            }
              
          else if(p->next->exp > q->next->exp)             
                 {
                    temp=q->next->next;
                   q->next->next=p->next;                        
                   p->next=q->next;
                   q->next=temp;
                  } 
        else  if(p->next->exp < q->next->exp)
               p=p->next; 
      }
         if(!(p->next)&&q->next)
            p->next=q->next;
        if(!(q->next)&&p->next)         
               ;
//***************输出结果************************         
        LinkList L=L1;
      printf("/nSum(x) :");
           while(L->next){
               printf("%d*X^%d",L->next->coe,L->next->exp);
               L=L->next;  // 别忘了向后走
               if(L->next)
                  printf(" + ");
                }
      printf("/n Do you want again:(y or n)/n");
       ch=getch();                  //与getchar()的区别
       printf("%c/n",ch);
         if(ch=='y')
             goto Start;
         else
            printf("Bye,Bye!/n");
         return 0;
       

                             
                                 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3235413次
    • 积分:31256
    • 等级:
    • 排名:第162名
    • 原创:622篇
    • 转载:225篇
    • 译文:50篇
    • 评论:425条
    最新评论