⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 poly.c

📁 c语言编写的关于多项式创建
💻 C
字号:
#include   <stdlib.h>
#include   <malloc.h>
#include   <stdio.h>
#include <conio.h>
#include<math.h>
typedef struct LNode{

  float coef;  /*系数项*/

  int expn;  /*指数项*/

  struct LNode *next;

}LNode,*polyn;


 
  void open() /*主菜单*/

{   clrscr();
    printf("                  **********************************\n");
    printf("                       The program of multinomials\n");
    printf("                       Written by chen you jian\n");
    printf("                  ***********************************\n");
    printf("                  menu:\n") ;
    printf("                        0.Exit\n");
    printf("                        1.Create multinomials\n ");
    printf("                       2.Print multinomials\n");
    printf("                        3.Make multinomials add\n");
    printf("                        4.Make multinomials sub\n");

    printf(" \n Please choose one number to go on...\n");
}

 

void input_p(polyn *p){

  /*输入并生成一元多项式*/

  int i,min=-32768;

  float f;

  polyn q,r;
 

  /*生成头结点*/

  *p=(polyn)malloc(sizeof(LNode));

  if (!*p) exit(-2);

  (*p)->coef=0;

  printf("Its length is:");

  scanf("%d",&((*p)->expn));  /*利用头结点的指数域保存项数*/

  (*p)->next=NULL;

 

  /*输入多项式,各项应按指数递增有序*/
  clrscr();
  printf("Attention:\n");
  printf("  ..................................................................\n");
  printf("  .The terms of the poly you are going to creat is:%d               .\n",(*p)->expn);
  printf("  .The exponent you input must not be smaller than the former one! .\n");
  printf("  ..................................................................\n");
  for(i=1;i<=(*p)->expn;i++){

    q=(polyn)malloc(sizeof(LNode));

    if (!q) exit(-2);

    printf("The %dth coefficient:",i);

    scanf("%f",&f);

    q->coef=f;

 

    do{

      printf("The %dth exponent:",i);

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

      if (q->expn<min)

           printf("\nThe former term's exponent is %d, this term's exponent must not be smaller than it!\nplease input again!\n",(*p)->next->expn);

    }while(q->expn<min);

    min=q->expn;

 

    q->next=(*p)->next;

    (*p)->next=q;

  }/*for*/

 

  /*合并多项式中指数值相同的项*/

  q=(*p)->next;

  while(q){

    r=q->next;

    while (r&&r->expn==q->expn){

q->coef+=r->coef;

         q->next=r->next;

free(r);

         r=q->next;

         (*p)->expn--;

    }/*while_r*/

    q=q->next;

  }/*while_q*/

 

}/*input_p*/

 

void output_p(polyn p){

    /*输出多项式p,输出形式为:n,c1,e1,c2,e2,...,cn,en,

      其中n是多项式的项数,ci是第i项的系数,ei是第i项的指数,

      序列按指数降序排列*/

    polyn q;
    int a=0;

    q=p->next;

    while(q){
      if(a==0)
      { printf("\n%0.2f*x^%d ",q->coef,q->expn);a=1;}
      else
      {
          if(q->coef>0) printf("+%0.2f*x^%d ",q->coef,q->expn);
          else if(q->coef==0) ;
              else printf("%0.2f*x^%d ",q->coef,q->expn) ;
       }

      q=q->next;

    }

  }/*output_p*/

 

polyn add_p(polyn pa,polyn pb){

    /*pa=pa+pb*/

 

    polyn ha,hb,hc,q,t;
    int a=0;
    q=(polyn)malloc(sizeof(LNode));
    if (!q) exit(-2);


    ha=pa->next;

    hb=pb->next;

    hc=q;

    hc->next=NULL;

    q->expn=0;

 

    while (ha&&hb){

      if (ha->expn>hb->expn){

       t=(polyn)malloc(sizeof(LNode));
       t->coef=ha->coef;
       t->expn=ha->expn;

       hc->next=t;;

       hc=t;

       ha=ha->next;

       hc->next=NULL;

       q->expn++;

      }/*if ha->expn>hb->expn */

      else

        if (ha->expn<hb->expn){

         t=(polyn)malloc(sizeof(LNode));
         t->coef=hb->coef;
         t->expn=hb->expn;

         hc->next=t;;

         hc=t;


         hb=hb->next;

         hc->next=NULL;

         q->expn++;

       }/*if ha->expn<hb->expn*/

        else{/*ha->expn=hb->expn*/


         a=ha->coef+hb->coef;

         if (a!=0){

           t=(polyn)malloc(sizeof(LNode));
           t->coef=a;
           t->expn=hb->expn;
           hc->next=t;
           hc=t;


           hc->next=NULL;

           q->expn++;
           ha=ha->next;

           hb=hb->next;
           a=0;

           }/*if ha->coef!=0*/

          else{/*ha->coef=0*/


           ha=ha->next;



           hb=hb->next;


         }/*else*/

        }/*else*/

    }/*while*/

 

    if (ha){
           t=(polyn)malloc(sizeof(LNode));
           t->coef=ha->coef;
           t->expn=ha->expn;
           hc->next=t;
           hc=t;
           hc->next=NULL;
      while(ha){

       q->expn++;

       ha=ha->next;

      }/*while*/

    }/*if*/

 

    if (hb) {
           t=(polyn)malloc(sizeof(LNode));
           t->coef=hb->coef;
           t->expn=hb->expn;
           hc->next=t;
           hc=t;
           hc->next=NULL;

      while (hb){

       q->expn++;

       hb=hb->next;

      }/*while*/

    }/*if*/
  return(q);
  }/*add_p*/

 

polyn sub_p(polyn pa,polyn pb){

  /*pa=pa-pb*/
 polyn ha,hb,hc,p,t;
    int a=0;
    p=(polyn)malloc(sizeof(LNode));
    if (!p) exit(-2);


    ha=pa->next;

    hb=pb->next;

    hc=p;

    hc->next=NULL;

    p->expn=0;

 

    while (ha&&hb){

      if (ha->expn>hb->expn){

       t=(polyn)malloc(sizeof(LNode));
       t->coef=ha->coef;
       t->expn=ha->expn;

       hc->next=t;;

       hc=t;

       ha=ha->next;

       hc->next=NULL;

       p->expn++;

      }/*if ha->expn>hb->expn */

      else

        if (ha->expn<hb->expn){

         t=(polyn)malloc(sizeof(LNode));
         t->coef=-1*(hb->coef);
         t->expn=hb->expn;

         hc->next=t;;

         hc=t;


         hb=hb->next;

         hc->next=NULL;

         p->expn++;

       }/*if ha->expn<hb->expn*/

        else{/*ha->expn=hb->expn*/


         a=ha->coef-hb->coef;

         if (a!=0){

           t=(polyn)malloc(sizeof(LNode));
           t->coef=a;
           t->expn=hb->expn;
           hc->next=t;
           hc=t;


           hc->next=NULL;

           p->expn++;
           ha=ha->next;

           hb=hb->next;
           a=0;

           }/*if ha->coef!=0*/

          else{/*ha->coef=0*/


           ha=ha->next;



           hb=hb->next;
           p->expn--;

         }/*else*/

        }/*else*/

    }/*while*/

 

    if (ha){
           t=(polyn)malloc(sizeof(LNode));
           t->coef=ha->coef;
           t->expn=ha->expn;
           hc->next=t;
           hc=t;
           hc->next=NULL;
      while(ha){

       p->expn++;

       ha=ha->next;

      }/*while*/

    }/*if*/

 

    if (hb) {
           t=(polyn)malloc(sizeof(LNode));
           t->coef=-1*(hb->coef);
           t->expn=hb->expn;
           hc->next=t;
           hc=t;
           hc->next=NULL;

      while (hb){

       p->expn++;

       hb=hb->next;

      }/*while*/

    }/*if*/
  return(p);
  }
/*minus_p*/

float result_(polyn p,float x)
{
 polyn q;
 float s=0.0,y=0.0,t=0.0;
 q=p->next;
 /*q=q->next;*/
 while(q)
 { t=pow(x,q->expn);
   y=(q->coef)*t;
   s=s+y;
   q=q->next;
 }
return(s);

}

 

 

  main(){

    polyn pa=NULL,pb=NULL,pc=NULL,pd=NULL;
    float a=0;
    char i=-1;
    open();
    while(i!=0)
    {
      scanf("%d",&i);

      switch(i)
      {
         case 0: exit(0);
         case 1:
                clrscr();
                printf("Now create the first multinomial:\n");
                input_p(&pa);
                printf("result:");
                output_p(pa);
                printf("\nPrint any key to go on ...");
                getch();
                clrscr();
                printf("\nNow create the second multinomial:\n");
                input_p(&pb);
                printf("result:");
                output_p(pb);
                printf("\nPrint any key to go on ...");
                getch();

                open();
                break;
         case 2:
                clrscr();
                printf("The two multinomials are:\n");

                output_p(pa);


                output_p(pb);
                printf("\n\nPrint any key to the menu");
                getch();
                open();
                break;

         case 3:
                clrscr();
                pc=add_p(pa,pb);

                printf("\n\nSum of two multinomials is: ");
                output_p(pc);
                printf("\nGive a value to X,then return a exact numerical value.\nX=" );
                scanf("%f",&a);
                printf("\nWhen x=%0.2f,",a);
                output_p(pc);
                printf("=%0.2f",result_(pc,a));
                printf("\n\nPrint any key to return to the menu...");
                getch();

                open();
                break;



        case 4:
               clrscr();
               pd=sub_p(pa,pb);

               printf("\n\nDifference of two multinomials is:");

               output_p(pd);
               printf("\nGive a value to X,then return a exact numerical value.\nX=" );
               scanf("%f",&a);
               printf("\nWhen x=%0.2f,",a);
               output_p(pd);
               printf("=%0.2f",result_(pd,a));
               printf("\n\nPrint any key to return to the menu...");
               getch();

               open();
               break;
          default :  printf("\nInput wrong!!!Input again!\n"); open();


  }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -