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

📄 rrr.cpp

📁 数据结构C语言实现一元多项式运算
💻 CPP
字号:
#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;   
    int x; int y;   
    p=(PolyNode *)malloc(sizeof(PolyNode));   
    p->next=NULL;   
    printf("请输入多项式%c:(格式:系数 指数,以0 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("加法按1.\n");   
       printf("减法按2.\n");   
       printf("请选择:\n");   
        scanf("%d",&i);   
        getchar();   
   
        switch(i)   
        {         
        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;   
 
          }   
     }   
  }   

⌨️ 快捷键说明

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