• 一元多项式的实现及其运算



    #include "stdio.h"
    #include "stdlib.h"
    #include "iostream.h"
    //定义多项式节点的结构
    typedef struct LNode
    {
    float coef; // 系数
    int expn; // 指数
    struct LNode *next; // 指向结构体的指针
    }LNode,*link;
    link create(link l);//创建
    int deleteLi(link L,int i);//删除节点
    int getLen(link L);//求多项式的项数
    void output(link l);//输出
    link add(link l1,link l2);//加法
    link sub(link l1,link l2);//减法
    link mul(link l1,link l2);//乘法
    link qd_one(link l);//形如l=a*x^b求导
    link qiudao(link l);//求导
    float qiuzhi(link l1,float x);//求值
    link getElem(link L,int i);//获得指向第 i 个节点的指针

    void main()
    {
    link l1=NULL;//,l2,l3;
    link l2=NULL;
    link l3=NULL;
    float value;
    float x;
    int sel=0;
    do
    {
    //菜单设计
    printf("0:退出\n");
    printf("1:创建\n");
    printf("2:输出\n");
    printf("3:加法\n");
    printf("4:减法\n");
    printf("5:乘法\n");
    printf("6:求导\n");
    printf("7:求值\n");

    //菜单选择程序
    cout<<"请选择菜单并输入代号:";
    cin>>sel;
    if((sel>=0)&&(sel<8))
    ;
    else
    {
    cout<<"输入错误,请重新输入代号:";
    cin>>sel;
    }

    switch(sel)
    {
    case 0://退出
    exit(0);
    break;
    case 1://创建
    l1=create(l1);
    break;
    case 2://输出
    output(l1);
    break;
    case 3://加法
    cout<<"请输入第一个多项式:";
    l1=create(l1);
    cout<<"请输入第二个多项式:";
    l2=create(l2);
    cout<<endl;
    output(l1);
    cout<<endl;
    output(l2);
    l3=add(l1,l2);
    cout<<endl<<"结果为:";
    output(l3);
    cout<<endl;
    break;
    case 4://减法
    cout<<"请输入被减多项式:";
    l1=create(l1);
    cout<<endl;
    cout<<"请输入减式:";
    l2=create(l2);
    cout<<"您输入的两个多项式为:"<<endl;
    output(l1);
    cout<<endl;
    output(l2);
    l3=sub(l1,l2);
    cout<<endl;
    cout<<"减法计算的结果为:";
    output(l3);
    cout<<endl;
    break;
    case 5://乘法
    cout<<"请输入第一个乘式:";
    l1=create(l1);
    cout<<"请输入第二个乘式:";
    l2=create(l2);
    output(l1);
    cout<<endl;
    output(l2);
    cout<<endl;
    l3=mul(l1,l2);
    cout<<"乘法计算结果为:";
    output(l3);
    break;
    case 6://求导
    link lll;
    cout<<"原始多项式为:";
    output(l1);
    cout<<endl;
    lll=qiudao(l1);
    cout<<"求导结果是:";
    output(lll);
    break;
    case 7://求值
    cout<<"请输入x的数值:";
    cin>>x;
    value=qiuzhi(l1,x);
    output(l1);
    cout<<endl;
    cout<<"计算结果是:"<<value<<endl;
    break;
    default:
    break;
    }
    }while(1);
    }
    //建立连表,返回指向多项式头节点的指针
    link create(link L)
    {
    link temp,r; //声明结构体指针
    float c; //系数
    int e; //指数
    //建立头节点,头节点指针设置为空
    L=(LNode *)malloc(sizeof(LNode));
    L->next=NULL;
    //是指针r指向头节点
    r=L;
    printf("\n请输入多项式的指数和系数,以 0 0 结束:");
    cin>>c>>e;
    while(c!=0) //输入的第一项系数不允许为0,防止无意义的输入
    {
    //初始化节点
    temp=(LNode *)malloc(sizeof(LNode));
    temp->coef=c;
    temp->expn=e;
    temp->next=NULL;
    r->next=temp; //连接节点
    r=temp; //指针r指向当前节点,用于连接下一节点
    printf("\n请继续输入多项式的指数和系数:");
    cin>>c>>e;
    }
    return L;
    }

    //获得多项式的长度,参数为多项式头节点指针
    int getLen(link L)
    {
    link p;
    int count=0;
    p=L->next;
    while(p)
    {
    count++;
    p=p->next ;
    }
    return count;
    }
    //打印多项式
    void output(link l)
    {
    int i;
    link p=l->next;
    cout<<"您的多项式为:y=";
    for(i=0;i<getLen(l);i++)
    {
    if(p->coef>0)
    cout<<p->coef;
    else
    cout<<"("<<p->coef<<")";
    if(p->expn>0)
    cout<<"x^"<<p->expn;
    else
    cout<<"x^("<<p->expn<<")";
    if(p->next==NULL)
    return;
    else
    cout<<"+";
    p=p->next;
    }
    cout<<endl;
    }

    LNode *add(link f,link g) //多项式相加
    {
    link fg;
    link t,q,s,r;
    float m;
    t=f->next;
    q=g->next;
    fg=r=(LNode*)malloc(sizeof(LNode));
    fg->next=NULL;
    while(t&&q)
    {
    if(t->expn==q->expn) //指数相等时系数相加
    {
    m=t->coef+q->coef;
    if(m!=0) //系数为不0时加到结果中去
    {
    s=(LNode *)malloc(sizeof(LNode));
    s->coef=m;
    s->expn=t->expn;
    s->next=NULL;
    }
    t=t->next;
    q=q->next;
    }
    else //指数小的加到结果中去再后移
    if(t->expn<q->expn)
    {
    s=(LNode *)malloc(sizeof(LNode));
    s->coef=t->coef;
    s->expn=t->expn;
    s->next=NULL;
    t=t->next;
    }
    else
    {
    s=(LNode *)malloc(sizeof(LNode));
    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;
    }
    }//while
    r->next=t?t:q; //把没加完的接上
    return fg;
    }
    //删除节点i
    int deleteLi(link L,int i)
    {
    LNode *q,*p;
    //获得节点i的前一个节点,便于删除节点i
    p=getElem(L,i-1);
    q=p->next;
    if(q) //如果P为空,则说明节点p也就是节点i-1就是尾节点,节点i实际并不存在
    {
    p->next=q->next;
    free(q);
    return 1;
    }
    else
    return 0;
    }

    //获得指向第 i 个节点的指针,主要在删除节点的时候用来获得准备删除的前一节点
    link getElem(link L,int i)
    {
    LNode *p;
    int j=0;
    p=L;
    while(p->next&&j!=i) //当p不是尾节点,并且不是第 i 个节点
    {
    j++;
    p=p->next;
    }
    if(i==j)
    return p; //找到节点
    else
    return NULL; //没有找到节点
    }
    float qiuzhi(link l,float x)//求值
    {
    link q;
    float value=0.0;
    int i;
    int j=0;
    float temp=1.0;
    q=l->next;
    for(i=0;i<getLen(l);i++)
    {
    for(j=0;j<q->expn;j++)
    temp*=x;
    value+=(q->coef)*temp;
    q=q->next;
    }
    return value;
    }
    link qd_one(link l)//形如l=a*x^b求导
    {
    link q=l;
    q->coef=(q->coef)*(q->expn);
    q->expn=q->expn-1;
    return q;
    }
    link qiudao(link l)
    {
    int i;
    link p,tem;
    p=l;
    tem=p->next;
    for(i=0;i<getLen(l);i++)
    {
    tem=qd_one(tem);
    tem=tem->next;
    }
    return p;
    }
    link sub(link L1,link L2)
    {
    int i;
    link q,t;
    q=L2;
    t=q->next;
    for(i=0;i<getLen(L2);i++)//将减式取反,用加法实现
    {
    t->coef=-t->coef;
    t=t->next;
    }
    L2=q;
    return add(L1,L2);
    }

    link mul(link f,link g)//多项式相乘
    {
    LNode *h;
    LNode *t,*q,*s,*r;
    h=(LNode *)malloc(sizeof(LNode));
    h->next=NULL;
    r=(LNode *)malloc(sizeof(LNode));
    r->next=NULL;
    for(t=f->next;t;t=t->next) //相乘时把第一项多项式的每一项
    { //与第二个多项式中的每一项相乘
    for(q=g->next;q;q=q->next) //用双重循环实现
    {
    s=(LNode *)malloc(sizeof(LNode));
    r->next=s;
    s->coef=q->coef*t->coef;
    s->expn=q->expn+t->expn;
    s->next=NULL;
    h=add(r,h); //把每项相乘结果加起来
    }
    }
    return h;
    }
  • 相关阅读:
    Shared Memory in Windows NT
    Layered Memory Management in Win32
    软件项目管理的75条建议
    Load pdbs when you need it
    Stray pointer 野指针
    About the Rebase and Bind operation in the production of software
    About "Serious Error: No RTTI Data"
    Realizing 4 GB of Address Space[MSDN]
    [bbk4397] 第1集 第一章 AMS介绍
    [bbk3204] 第67集 Chapter 17Monitoring and Detecting Lock Contention(00)
  • 原文地址:https://www.cnblogs.com/feisky/p/1586664.html
Copyright © 2020-2023  润新知