zoukankan      html  css  js  c++  java
  • 实现一元多项式

      1 //利用链表的方式给输入的项边插入排序边进行合并同类项;
      2 #include<stdio.h>
      3 #include<stdlib.h>
      4 
      5 struct Function//一元多项式
      6 {
      7     int coef;//系数;
      8     int exp;//指数;
      9     struct Function *next;
     10 };
     11 
     12 struct Function *insert(struct Function *head,struct Function *p)//插入并排序一元多项式
     13 {
     14     struct Function *q,*q1,*q2;
     15     q=(struct Function *)malloc(sizeof(struct Function));//建立q节点
     16     q->coef=p->coef;//让把插入的p节点给q节点
     17     q->exp=p->exp;
     18     q->next=NULL;//q节点下一个指向空
     19     if(head==NULL)//如果头节点是空的,则让把Q节点给头节点
     20     {
     21         head=q;
     22         head->next=NULL;
     23         return head;
     24     }
     25     q2=q1=head;//q1和q2节点都指向头节点;
     26     while(q1!=NULL)
     27     {
     28         while(q1->exp<p->exp)//如果q1的指数小于插入的p节点则让把q1赋值给q2,
     29         {                   //然后在让下一个数和插入的比较先比较,一直到最后一个相等或者大于。
     30             q2=q1;
     31             if(q1->next==NULL)
     32                 break;
     33             q1=q1->next;
     34         }
     35         if(q1->exp==p->exp)//指数相同则系数相加,如果系数为0则释放q1指向头节点;
     36         {
     37             q1->coef=q1->coef+p->coef;
     38             if(q1->coef==0)
     39             {
     40                 if(q1==head)
     41                 {
     42                     head=head->next;
     43                     break;
     44                 }
     45                 else
     46                 {
     47                     q2->next=q1->next;
     48                 }
     49             }
     50             else
     51                 break;
     52         }
     53         else if(q1->exp<p->exp)//如果插入的数指数大于q1的指数则把插入的数接在q1后面
     54         {
     55             q1->next=q;
     56             break;
     57         }
     58         else//如果碰到插入的数的指数小于q1的指数
     59         {
     60             if(q2==head&&q2->exp>q->exp)  //所有的数的指数都比插入的数的指数大,则把他放第一个
     61                                           //如果不加q2->exp>q->exp会出现二义性
     62             {
     63                 q->next=head;
     64                 head=q;
     65                 break;
     66             }
     67             else//把要插入的数放在q2-q1中间,q2<q<q1的指数;
     68             {
     69                 q2->next=q;
     70                 q->next=q1;
     71                 break;
     72             }
     73         }
     74     }
     75     return head;
     76 };
     77 
     78 void print(struct Function *p)//输出多项式。
     79 {
     80     struct Function *p1;
     81     p1=p;
     82     if(p==NULL)
     83     {
     84         printf("0
    ");
     85         return;
     86     }
     87     while(p1!=NULL)
     88     {
     89         if(p1==p)  //第一个结点输出时的情况
     90         {
     91             if(p1->coef==1)
     92                {
     93                     if(p1->exp!=0)
     94                         printf("x^%d ",p1->exp);
     95                     else
     96                         printf(" ");
     97                 }
     98              else{
     99                 printf(" %d",p1->coef);
    100                 if(p1->exp!=0)
    101                 printf("x^%d ",p1->exp);
    102                 else
    103                 printf(" ");
    104              }
    105         }
    106         else
    107         {
    108             if(p1->coef>0)
    109             {
    110                     if(p1->coef==1)
    111                {
    112                         if(p1->exp!=0)
    113                         printf("x^%d ",p1->exp);
    114                         else
    115                         printf(" ");
    116                 }
    117                     else{
    118                         printf("+%d",p1->coef);
    119                         if(p1->exp!=0)
    120                         printf("x^%d ",p1->exp);
    121                         else
    122                         printf(" ");
    123                     }
    124             }
    125             else
    126             {
    127                  if(p1->coef==-1)
    128                {
    129                         if(p1->exp!=0)
    130                         printf("x^%d ",p1->exp);
    131                         else
    132                         printf(" ");
    133                 }
    134                     else{
    135                         printf("%d",p1->coef);
    136                         if(p1->exp!=0)
    137                         printf("x^%d ",p1->exp);
    138                         else
    139                         printf(" ");
    140                     }
    141             }
    142         }
    143         p1=p1->next;
    144     }
    145     printf("
    ");
    146 }
    147 
    148 
    149 struct Function *create()//建立这样一个多项式链表,插入多项式。
    150 {
    151     struct Function *p,*head;
    152     p=(struct Function *)malloc(sizeof(struct Function));
    153     int len;
    154     int i;
    155     head=NULL;
    156     scanf("%d", &len);
    157     for(i = 0; i < len; i++)
    158     {
    159         printf("分别输入第%d项的系数c、指数e:", i+1);
    160         scanf("%d%d", &p->coef, &p->exp);
    161         if(p->coef!=0||p->exp!=0)//只要多项式系数和指数不为0,就一直延长链表。
    162             {                           //如果头节点是空的,则让把Q节点给头节点
    163                 head=insert(head,p);
    164             }
    165     }
    166         return head;
    167 }
    168 
    169 
    170 void add(struct Function *p1,struct Function *p2)//q为相加的最后结果的链
    171 {
    172     struct Function*p,*q,*head;
    173     p=(struct Function *)malloc(sizeof(struct Function));
    174     head=q=p;
    175     while(p1!=NULL||p2!=NULL)//输入的两个多项式不为空则进行运算
    176     {
    177         if(p1==NULL)
    178         {
    179             while(p2!=NULL)//当p1的一元多项式加完了,不够长时,让p后面的部分都是p2的部分。
    180             {
    181                  q=p;
    182                  p->exp=p2->exp;
    183                  p->coef=p2->coef;
    184                  p=(struct Function *)malloc(sizeof(struct Function));
    185                  q->next=p;
    186                  p2=p2->next;
    187             }
    188         }
    189         else if(p2==NULL)//和上面同理:如果p2先加完则让p后面部分为P1后面的部分。
    190         {
    191             while(p1!=NULL)
    192             {
    193                  q=p;
    194                  p->exp=p1->exp;
    195                  p->coef=p1->coef;
    196                  p=(struct Function *)malloc(sizeof(struct Function));
    197                  q->next=p;
    198                  p1=p1->next;
    199             }
    200         }
    201         else//不然的话就直接比较每一项,指数相同就进行系数相加,否则指针后移,比较后面一项。
    202         {
    203             if(p1->exp<p2->exp)//p1有但是p2没有,且p1的指数小于p2的指数则把这部分节点加给p
    204             {
    205                  q=p;
    206                  p->exp=p1->exp;
    207                  p->coef=p1->coef;
    208                  p=(struct Function *)malloc(sizeof(struct Function));
    209                  q->next=p;
    210                  p1=p1->next;
    211             }
    212             else if(p1->exp==p2->exp)//p1,p2相同的项合并,系数相加赋给p
    213             {
    214                  q=p;
    215                  p->exp=p1->exp;
    216                  p->coef=p1->coef+p2->coef;
    217                  if(p->coef!=0)//如果系数相加为0则释放这个节点,指向下一个节点
    218                  {
    219                      p=(struct Function *)malloc(sizeof(struct Function));
    220                      q->next=p;
    221                      p1=p1->next;
    222                      p2=p2->next;
    223                  }
    224                  else
    225                  {
    226                      p1=p1->next;
    227                      p2=p2->next;
    228                  }
    229             }
    230             else//p2有但是p1没有,且p2的指数小于p1的指数则把这部分节点加给p
    231             {
    232                  q=p;
    233                  p->exp=p2->exp;
    234                  p->coef=p2->coef;
    235                  p=(struct Function *)malloc(sizeof(struct Function));
    236                  q->next=p;
    237                  p2=p2->next;
    238             }
    239         }
    240     }
    241     q->next=NULL;
    242     print(head);
    243 };
    244 
    245 void sub(struct Function *p1,struct Function *p2)//和add部分相同,只是把相加的部分变成减号
    246 {
    247     struct Function*p,*q,*head;
    248     p=(struct Function *)malloc(sizeof(struct Function));
    249     head=q=p;
    250     while(p1!=NULL||p2!=NULL)
    251     {
    252         if(p1==NULL)
    253         {
    254             while(p2!=NULL)//被减数加负号,赋值给p;
    255             {
    256                  q=p;
    257                  p->exp=p2->exp;
    258                  p->coef=-p2->coef;
    259                  p=(struct Function *)malloc(sizeof(struct Function));
    260                  q->next=p;
    261                  p2=p2->next;
    262             }
    263         }
    264         else if(p2==NULL)
    265         {
    266             while(p1!=NULL)
    267             {
    268                  q=p;
    269                  p->exp=p1->exp;
    270                  p->coef=p1->coef;
    271                  p=(struct Function *)malloc(sizeof(struct Function));
    272                  q->next=p;
    273                  p1=p1->next;
    274             }
    275         }
    276         else
    277         {
    278             if(p1->exp<p2->exp)
    279             {
    280                  q=p;
    281                  p->exp=p1->exp;
    282                  p->coef=p1->coef;
    283                  p=(struct Function *)malloc(sizeof(struct Function));
    284                  q->next=p;
    285                  p1=p1->next;
    286             }
    287             else if(p1->exp==p2->exp)
    288             {
    289                  q=p;
    290                  p->exp=p1->exp;
    291                  p->coef=p1->coef-p2->coef;
    292                  if(p->coef!=0)
    293                  {
    294                      p=(struct Function *)malloc(sizeof(struct Function));
    295                      q->next=p;
    296                      p1=p1->next;
    297                      p2=p2->next;
    298                  }
    299                  else
    300                  {
    301                      p1=p1->next;
    302                      p2=p2->next;
    303                  }
    304             }
    305             else
    306             {
    307                  q=p;
    308                  p->exp=p2->exp;
    309                  p->coef=-p2->coef;
    310                  p=(struct Function *)malloc(sizeof(struct Function));
    311                  q->next=p;
    312                  p2=p2->next;
    313             }
    314         }
    315 
    316     }
    317     q->next=NULL;
    318     print(head);
    319 };
    320 
    321 
    322 
    323 
    324 int main()
    325 {
    326     struct Function *p1,*p2;
    327     printf("输入多项式A(x)的项数:len = ");
    328     p1=create();
    329     printf("输入多项式B(x)的项数:len = ");
    330     p2=create();
    331     printf("
    ");
    332     printf("A(x)=");
    333     print(p1);
    334     printf("
    ");
    335     printf("B(x)=");
    336     print(p2);
    337     printf("
    
    ");
    338     printf("A(x)+B(x)=");
    339     add(p1,p2);
    340     printf("
    
    ");
    341     printf("A(x)-B(x)=");
    342     sub(p1,p2);
    343     printf("
    
    ");
    344     return 0;
    345 }

    //利用链表的方式给输入的项边插入排序边进行合并同类项;#include<stdio.h>#include<stdlib.h>
    struct Function//一元多项式{    int coef;//系数;    int exp;//指数;    struct Function *next;};
    struct Function *insert(struct Function *head,struct Function *p)//插入并排序一元多项式{    struct Function *q,*q1,*q2;    q=(struct Function *)malloc(sizeof(struct Function));//建立q节点    q->coef=p->coef;//让把插入的p节点给q节点    q->exp=p->exp;    q->next=NULL;//q节点下一个指向空    if(head==NULL)//如果头节点是空的,则让把Q节点给头节点    {        head=q;        head->next=NULL;        return head;    }    q2=q1=head;//q1和q2节点都指向头节点;    while(q1!=NULL)    {        while(q1->exp<p->exp)//如果q1的指数小于插入的p节点则让把q1赋值给q2,        {                   //然后在让下一个数和插入的比较先比较,一直到最后一个相等或者大于。            q2=q1;            if(q1->next==NULL)                break;            q1=q1->next;        }        if(q1->exp==p->exp)//指数相同则系数相加,如果系数为0则释放q1指向头节点;        {            q1->coef=q1->coef+p->coef;            if(q1->coef==0)            {                if(q1==head)                {                    head=head->next;                    break;                }                else                {                    q2->next=q1->next;                }            }            else                break;        }        else if(q1->exp<p->exp)//如果插入的数指数大于q1的指数则把插入的数接在q1后面        {            q1->next=q;            break;        }        else//如果碰到插入的数的指数小于q1的指数        {            if(q2==head&&q2->exp>q->exp)  //所有的数的指数都比插入的数的指数大,则把他放第一个                                          //如果不加q2->exp>q->exp会出现二义性            {                q->next=head;                head=q;                break;            }            else//把要插入的数放在q2-q1中间,q2<q<q1的指数;            {                q2->next=q;                q->next=q1;                break;            }        }    }    return head;};
    void print(struct Function *p)//输出多项式。{    struct Function *p1;    p1=p;    if(p==NULL)    {        printf("0 ");        return;    }    while(p1!=NULL)    {        if(p1==p)  //第一个结点输出时的情况        {            if(p1->coef==1)               {                    if(p1->exp!=0)                        printf("x^%d ",p1->exp);                    else                        printf(" ");                }             else{                printf(" %d",p1->coef);                if(p1->exp!=0)                printf("x^%d ",p1->exp);                else                printf(" ");             }        }        else        {            if(p1->coef>0)            {                    if(p1->coef==1)               {                        if(p1->exp!=0)                        printf("x^%d ",p1->exp);                        else                        printf(" ");                }                    else{                        printf("+%d",p1->coef);                        if(p1->exp!=0)                        printf("x^%d ",p1->exp);                        else                        printf(" ");                    }            }            else            {                 if(p1->coef==-1)               {                        if(p1->exp!=0)                        printf("x^%d ",p1->exp);                        else                        printf(" ");                }                    else{                        printf("%d",p1->coef);                        if(p1->exp!=0)                        printf("x^%d ",p1->exp);                        else                        printf(" ");                    }            }        }        p1=p1->next;    }    printf(" ");}

    struct Function *create()//建立这样一个多项式链表,插入多项式。{    struct Function *p,*head;    p=(struct Function *)malloc(sizeof(struct Function));    int len;int i;head=NULL;    scanf("%d", &len);    for(i = 0; i < len; i++)    {        printf("分别输入第%d项的系数c、指数e:", i+1);scanf("%d%d", &p->coef, &p->exp);        if(p->coef!=0||p->exp!=0)//只要多项式系数和指数不为0,就一直延长链表。            {                           //如果头节点是空的,则让把Q节点给头节点                head=insert(head,p);            }    }        return head;}

    void add(struct Function *p1,struct Function *p2)//q为相加的最后结果的链{    struct Function*p,*q,*head;    p=(struct Function *)malloc(sizeof(struct Function));    head=q=p;    while(p1!=NULL||p2!=NULL)//输入的两个多项式不为空则进行运算    {        if(p1==NULL)        {            while(p2!=NULL)//当p1的一元多项式加完了,不够长时,让p后面的部分都是p2的部分。            {                 q=p;                 p->exp=p2->exp;                 p->coef=p2->coef;                 p=(struct Function *)malloc(sizeof(struct Function));                 q->next=p;                 p2=p2->next;            }        }        else if(p2==NULL)//和上面同理:如果p2先加完则让p后面部分为P1后面的部分。        {            while(p1!=NULL)            {                 q=p;                 p->exp=p1->exp;                 p->coef=p1->coef;                 p=(struct Function *)malloc(sizeof(struct Function));                 q->next=p;                 p1=p1->next;            }        }        else//不然的话就直接比较每一项,指数相同就进行系数相加,否则指针后移,比较后面一项。        {            if(p1->exp<p2->exp)//p1有但是p2没有,且p1的指数小于p2的指数则把这部分节点加给p            {                 q=p;                 p->exp=p1->exp;                 p->coef=p1->coef;                 p=(struct Function *)malloc(sizeof(struct Function));                 q->next=p;                 p1=p1->next;            }            else if(p1->exp==p2->exp)//p1,p2相同的项合并,系数相加赋给p            {                 q=p;                 p->exp=p1->exp;                 p->coef=p1->coef+p2->coef;                 if(p->coef!=0)//如果系数相加为0则释放这个节点,指向下一个节点                 {                     p=(struct Function *)malloc(sizeof(struct Function));                     q->next=p;                     p1=p1->next;                     p2=p2->next;                 }                 else                 {                     p1=p1->next;                     p2=p2->next;                 }            }            else//p2有但是p1没有,且p2的指数小于p1的指数则把这部分节点加给p            {                 q=p;                 p->exp=p2->exp;                 p->coef=p2->coef;                 p=(struct Function *)malloc(sizeof(struct Function));                 q->next=p;                 p2=p2->next;            }        }    }    q->next=NULL;    print(head);};
    void sub(struct Function *p1,struct Function *p2)//和add部分相同,只是把相加的部分变成减号{    struct Function*p,*q,*head;    p=(struct Function *)malloc(sizeof(struct Function));    head=q=p;    while(p1!=NULL||p2!=NULL)    {        if(p1==NULL)        {            while(p2!=NULL)//被减数加负号,赋值给p;            {                 q=p;                 p->exp=p2->exp;                 p->coef=-p2->coef;                 p=(struct Function *)malloc(sizeof(struct Function));                 q->next=p;                 p2=p2->next;            }        }        else if(p2==NULL)        {            while(p1!=NULL)            {                 q=p;                 p->exp=p1->exp;                 p->coef=p1->coef;                 p=(struct Function *)malloc(sizeof(struct Function));                 q->next=p;                 p1=p1->next;            }        }        else        {            if(p1->exp<p2->exp)            {                 q=p;                 p->exp=p1->exp;                 p->coef=p1->coef;                 p=(struct Function *)malloc(sizeof(struct Function));                 q->next=p;                 p1=p1->next;            }            else if(p1->exp==p2->exp)            {                 q=p;                 p->exp=p1->exp;                 p->coef=p1->coef-p2->coef;                 if(p->coef!=0)                 {                     p=(struct Function *)malloc(sizeof(struct Function));                     q->next=p;                     p1=p1->next;                     p2=p2->next;                 }                 else                 {                     p1=p1->next;                     p2=p2->next;                 }            }            else            {                 q=p;                 p->exp=p2->exp;                 p->coef=-p2->coef;                 p=(struct Function *)malloc(sizeof(struct Function));                 q->next=p;                 p2=p2->next;            }        }
        }    q->next=NULL;    print(head);};



    int main(){    struct Function *p1,*p2;    printf("输入多项式A(x)的项数:len = ");    p1=create();    printf("输入多项式B(x)的项数:len = ");    p2=create();    printf(" ");    printf("A(x)=");    print(p1);    printf(" ");    printf("B(x)=");    print(p2);    printf(" ");    printf("A(x)+B(x)=");    add(p1,p2);    printf(" ");    printf("A(x)-B(x)=");    sub(p1,p2);    printf(" ");    return 0;}

  • 相关阅读:
    Django创建模型。
    Kubernetes APIService资源
    应用系统管理风险
    [Golang] go list命令查看依赖的版本
    [Golang] 升级gin框架和jwtgo
    [Git] 解决git错误 OpenSSL SSL_read: Connection was reset, errno 10054
    [Github] 配置ssh免密码登录解决 You've successfully authenticated, but GitHub does not provide shell access.
    go循环遍历小坑
    Go 字符串拼接6种,最快的方式 strings.builder
    uniapp安卓真机调试提示检测不到手机【解决办法】
  • 原文地址:https://www.cnblogs.com/myErebos/p/9262203.html
Copyright © 2011-2022 走看看