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

📄 chenggongduoxiang.c

📁 这个算法可以很好的实现实现一元多项式的计算
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>

typedef struct node
{
    float coef; 
    int expn;
    struct node * next; 
} PolyNode;

 PolyNode * Create_Poly(char ch)
{ 
    PolyNode * p, *s,*r;
    float x; int y;
    p=(PolyNode *)malloc(sizeof(PolyNode));
    p->next=NULL;
    printf("Please input a duoxiangshi%c:(geshi:xishu zhishu,zhishudizeng,end with0 0.)\n",ch);
    scanf("%f %d",&x,&y);
    while(x!=0)
    { 
        s=(PolyNode *)malloc(sizeof(PolyNode));
        s->coef=x;
        s->expn=y;
        s->next=NULL;
        if(p->next==NULL)
        {
            p->next=s;
            r=s;
        }
        else
        { 
            r->next=s;
            r=s;
        }
        scanf("%f %d",&x,&y);
    }
    return p;
}

PolyNode * Add_Poly(PolyNode * f,PolyNode * g)
{ 
    PolyNode * fg;
    PolyNode *t,*q,*s,*r;
    float m;
    t=f->next;
    q=g->next;
    fg=r=(PolyNode*)malloc(sizeof(PolyNode));
    fg->next=NULL;
    while(t&&q)
    { 
        if(t->expn==q->expn)
        { 
            m=t->coef+q->coef;
            if(m!=0)
            {
                s=(PolyNode *)malloc(sizeof(PolyNode));
                s->coef=m;
                s->expn=t->expn;
                s->next=NULL;                  
            }
            t=t->next;
            q=q->next; 
        }
        else
            if(t->expn<q->expn)                
            {                
                s=(PolyNode *)malloc(sizeof(PolyNode));
                s->coef=t->coef;
                s->expn=t->expn;
                s->next=NULL;
                t=t->next; 
            }
            else 
            {
                s=(PolyNode *)malloc(sizeof(PolyNode));
                s->coef=q->coef;
                s->expn=q->expn;
                s->next=NULL;
                q=q->next;
            }

        if(fg->next==NULL)
        { 
            fg->next=s;
            r=s; 
        }
        else
        {
            r->next=s;
            r=s;
        } 
    }
    r->next=t?t:q;
    return fg;
}

 PolyNode * Reduce_Poly(PolyNode * f,PolyNode * g)
{ 
    PolyNode * fg;
    PolyNode *t,*q,*s,*r;
    float m;
    t=f->next;
    q=g->next;
    fg=r=(PolyNode*)malloc(sizeof(PolyNode));
    fg->next=NULL;
    while(t&&q)
    { 
        if(t->expn==q->expn)
        { 
            m=t->coef-q->coef;
            if(m!=0)
            {
                s=(PolyNode *)malloc(sizeof(PolyNode));
                s->coef=m;
                s->expn=t->expn;
                s->next=NULL;                  
            }
            t=t->next;
            q=q->next; 
        }
        else
            if(t->expn<q->expn)                
            {                
                s=(PolyNode *)malloc(sizeof(PolyNode));
                s->coef=t->coef;
                s->expn=t->expn;
                s->next=NULL;
                t=t->next; 
            }
            else 
            {
                s=(PolyNode *)malloc(sizeof(PolyNode));
                s->coef=q->coef;
                s->expn=q->expn;
                s->next=NULL;
                q=q->next;
            }

        if(fg->next==NULL)
        { 
            fg->next=s;
            r=s; 
        }
        else
        {
            r->next=s;
            r=s;
        } 
    }
    r->next=t?t:q;
    return fg;
}
void Out_Poly(PolyNode * f)
{
    PolyNode *t;
    t=f->next;
    if(!f->next){
        printf("0\n");  return;
    }
    while(t)
    {   if(t->coef>0&&f->next!=t) printf("+");
        if(t->expn==0)
            printf("%f",t->coef);
        else
            printf("%f*X^%d",t->coef,t->expn);
        t=t->next;
    }
   printf("\n");
}

void main()
{

    PolyNode * f,* g,* fg;
    int i=-1;
    while(i!=0) 
    { printf("Press 0 to exit.\n");
      printf("Press 1 to start Adding  duoxiangshi .\n");
       printf("Press 2 to start Reducing  duoxiangshi.\n");
       printf("Please choose the cose:\n");
        scanf("%d",&i);
        getchar();

        switch(i)
        {    
        case 0:
            return;
        case 1:  

            f=Create_Poly('A');
            printf("A=");
            Out_Poly(f);
            g=Create_Poly('B');
            printf("B=");
            Out_Poly(g);
            printf("A+B=");
            fg=Add_Poly(f,g);
            Out_Poly(fg);break;
        case 2:

            f=Create_Poly('A');
            printf("A=");
            Out_Poly(f);
            g=Create_Poly('B');
            printf("B=");
            Out_Poly(g);
            printf("A-B=");
            fg=Reduce_Poly(f,g);
            Out_Poly(fg);break;
         default:
           printf("Error inputing!\n");
          }
     }
  }

⌨️ 快捷键说明

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