• 一元多项式的加减乘法


    输入一元多项式的参数,然后根据一元多项式加法减法乘法的计算法则,求解最终结果。用到了结构体,指针,操作的时候要注意。

    不多说,上代码:

      1 #include <stdio.h>
      2 #include <malloc.h>
      3 
      4 typedef struct node{
      5     float coef;
      6     int expn;
      7     struct node *next;
      8 }PLOY;
      9 
     10 void start(){
     11     printf("*******************************
    ");
     12     printf("两个一元多项式相加/相减,相乘:
    ");
     13     printf("*******************************
    ");
     14     printf("请选择操作:
    ");
     15     printf("0.退出
    ");
     16     printf("1.两个一元多项式相加
    ");
     17     printf("2.两个一元多项式相乘
    ");
     18     printf("3.两个一元多项式相减
    "); 
     19 } 
     20 
     21 void insert(PLOY *head,PLOY  *inpt){
     22     PLOY *pre,*now;
     23     int signal=0;
     24     pre=head;//pre定义为现在的前一个链接
     25     if(pre->next==NULL){
     26         pre->next=inpt;
     27     } 
     28     else{
     29         now=pre->next;
     30         while(signal==0){
     31             if(inpt->expn<now->expn){
     32                 if(now->next==NULL){
     33                     now->next=inpt;
     34                     signal=1;
     35                 }
     36                 else{
     37                     pre=now;
     38                     now=pre->next;
     39                 }
     40             }
     41             else{
     42                 if(inpt->expn > now->expn){
     43                     inpt->next=now;
     44                     pre->next=inpt;
     45                     signal=1;    
     46                 }
     47                 else{
     48                     now->coef=now->coef+inpt->coef;
     49                     signal=1;
     50                     free(inpt);
     51                     if(now->coef==0){
     52                         pre->next=now->next;
     53                         free(now); 
     54                     }
     55                 }
     56             }
     57         }
     58     }
     59 }
     60 
     61 PLOY *creat(char ch){
     62     PLOY *head,*inpt;
     63     float x;
     64     int y;
     65     head=(PLOY *)malloc(sizeof(PLOY));
     66     head->next=NULL;
     67     printf("请输入一元多项式%c:(格式是系数 指数;以0 0结束!)
    ",ch);
     68     scanf("%f %d",&x,&y);
     69     while(x!=0){
     70         inpt=(PLOY *)malloc(sizeof(PLOY));
     71         inpt->coef=x;
     72         inpt->expn=y;
     73         inpt->next=NULL;
     74         insert(head,inpt);
     75         printf("请输入一元多项式%c的下一项:(以0 0结束!)
    ",ch);
     76         scanf("%f %d",&x,&y);
     77     }
     78     return head;
     79 } 
     80 
     81 PLOY *addPLOY(PLOY *head,PLOY *pre){
     82     PLOY *inpt;
     83     int flag=0;
     84     while(flag==0){
     85         if(pre->next==NULL)
     86             flag=1;
     87         else{
     88             pre=pre->next;
     89             inpt=(PLOY *)malloc(sizeof(PLOY));
     90             inpt->coef=pre->coef;
     91             inpt->expn=pre->expn;  
     92             inpt->next=NULL;
     93             insert(head,inpt);
     94         }
     95     }
     96     return head;
     97 }
     98 
     99 PLOY *minusPLOY(PLOY *head,PLOY *pre){
    100     PLOY *inpt;
    101     int flag=0;
    102     while(flag==0){
    103         if(pre->next==NULL){
    104             flag=1;
    105         }
    106         else{
    107             pre=pre->next;
    108             inpt=(PLOY *)malloc(sizeof(PLOY));
    109             inpt->coef=0-pre->coef;
    110             inpt->expn=pre->expn;
    111             inpt->next=NULL;
    112             insert(head,inpt);
    113         }
    114     }
    115     return head;
    116 }
    117 
    118 PLOY *byPLOY(PLOY *headl,PLOY *head2){
    119     PLOY *inpt,*res,*pre;
    120     int flag=0;
    121     res=(PLOY *)malloc(sizeof(PLOY));
    122     res->next=NULL;
    123     headl=headl->next;
    124     pre=head2;
    125     while(flag==0){
    126         if(pre->next==NULL){
    127             pre=head2;
    128             headl=headl->next;
    129             continue;
    130         }
    131         if(headl==NULL){
    132             flag=1;
    133             continue;
    134         }
    135         pre=pre->next;
    136         inpt=(PLOY *)malloc(sizeof(PLOY));
    137         inpt->coef=pre->coef * headl->coef;
    138         inpt->expn=pre->expn + headl->expn;
    139         inpt->next=NULL;
    140         insert(res,inpt);
    141     } 
    142     return res;
    143 }
    144 
    145 void print(PLOY *fun){
    146     PLOY *printing;
    147     int flag=0;
    148     printing=fun->next;
    149     if(fun->next==NULL){
    150         printf("0
    ");
    151         return ;
    152     }
    153     while(flag==0){
    154         if(printing->coef>0&&fun->next!=printing)
    155             printf("+");
    156         if(printing->coef==1);
    157         else if(printing->coef==-1)
    158             printf("-");
    159         else
    160             printf("%f",printing->coef);
    161         if(printing->expn!=0)
    162             printf("x^%d",printing->expn);
    163         else if((printing->coef==1)||(printing->coef==-1))
    164             printf("1");
    165         if(printing->next==NULL)
    166             flag=1;
    167         else
    168             printing=printing->next;
    169     }
    170     printf("
    ");
    171 }
    172 
    173 int main(){
    174     PLOY *f,*g;
    175     int sign=-1;
    176     start();
    177     while(sign!=0){
    178         scanf("%d",&sign);
    179         switch(sign){
    180             case 0:
    181                 break;
    182             case 1:
    183                 {
    184                     printf("你选择的操作是多项式相加:
    ");
    185                     f=creat('f');               //输入多项式f(x) 
    186                     printf("f(x)=");
    187                     print(f);
    188                     g=creat('g');               //输入多项式g(x) 
    189                     printf("g(x)=");
    190                     print(g);
    191                     printf("F(x)=f(x)+g(x)=");
    192                     f=addPLOY(f,g);
    193                     print(f);
    194                     sign=-1;
    195                     start();
    196                     break;
    197                 }
    198             case 2:
    199                 {
    200                     printf("你选择的操作是多项式相乘:
    ");
    201                     f=creat('f');
    202                     printf("f(x)=");
    203                     print(f);
    204                     g=creat('g');
    205                     printf("g(x)=");
    206                     print(g);
    207                     printf("F(x)=f(x)*g(x)=");
    208                     f=byPLOY(f,g);
    209                     print(f);
    210                     sign=-1;
    211                     start();
    212                     break;    
    213                 }
    214             case 3:
    215                 {
    216                     printf("你选择的操作是多项式相减:
    ");
    217                     f=creat('f');
    218                     printf("f(x)=");
    219                     print(f);
    220                     g=creat('g');
    221                     printf("g(x)=");
    222                     print(g);
    223                     printf("F(x)=f(x)-g(x)=");
    224                     f=minusPLOY(f,g);
    225                     print(f);
    226                     sign=-1;
    227                     start();
    228                     break;
    229                 } 
    230             default:
    231                 {
    232                     printf("输入有误!请重新选择操作!
    ");
    233                     start();
    234                     break;
    235                 } 
    236         }
    237         
    238     }
    239 }
    240 
    241 
    242 
    243  
    244  
  • 相关阅读:
    Android studio关于点击事件后的页面跳转,选择完成后返回(onActivityResult)
    关于Android对话框简单实用方法总结
    Eclipse键盘输出文字,显示到屏幕上方法
    indexOf实际试用方法
    LiteOS裸机驱动移植01-以LED为例说明驱动移植
    LiteOS内核教程06-内存管理
    LiteOS内核教程05-互斥锁
    LiteOS内核教程04-信号量
    LiteOS内核教程03-任务管理
    LiteOS内核教程02-HelloWorld
  • 原文地址:https://www.cnblogs.com/liugl7/p/4836730.html
Copyright © 2020-2023  润新知