• 一元多项式的乘积与和(C++)


    设计函数分别求两个一元多项式的乘积与和(含排序)

    C++代码(全指针,无引用):

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<cstring>
    #include<string.h>
    using namespace std;
    //多项式相乘与相加 
    const int maxn=110010;
    
    typedef struct Node{
        int expon;
        double coef;
        struct Node *next;    
    }node,*pNode;
    
    pNode ReadPoly(pNode p){
        int K;
        pNode q,r;
        scanf("%d",&K);
        p=new node;
        p->next=NULL;
        r=p;
        while(K--){
            q=new node;
            scanf("%d %lf",&q->expon,&q->coef);
            r->next=q;
            r=r->next;
        }
        r->next=NULL;
        return p;
    } 
    void printPoly(pNode p){
        pNode q,r;
        int k=0;
        r=p->next;
        while(r)
        {
            if(r->coef!=0)
            {
                k++;
            }
            r=r->next;
        }
        printf("%d ",k);
        q=p->next;
        while(q){
            if(q->coef==0){//系数为0,则不必输出 
                q=q->next;
                continue;
            } 
            if(q->next!=NULL)
            {
                printf("%d %.1f ",q->expon,q->coef);
            } 
            else{
                printf("%d %.1f",q->expon,q->coef);
            }
            q=q->next;
        }
        printf("
    ");
    }
    pNode Mult(pNode p1,pNode p2){
        pNode ps,psn,ppp;
        ps=new node;//新链表头部 
        ps->expon=0;
        ps->coef=0;
        ps->next=NULL;
    //    psn=pp;//新链表尾部
        pNode p11,p22;
        p11=p1->next;
        int flog=0;
    //    p22=p2->next;
        while(p11){
             p22=p2->next;//返回链表第一个结点 
            while(p22){
                ppp=new node;
                ppp->expon=p11->expon+p22->expon;
                ppp->coef=p11->coef*p22->coef;
                psn=ps;//返回链表头节点 
                flog=0;
                while(psn->next)
                {
                    if(psn->next->expon==ppp->expon){
                        flog=1;
                        break;
                    }
                    else{
                        psn=psn->next;
                    }
                } 
                if(flog==1){
                    psn->next->expon=ppp->expon;
                    psn->next->coef=psn->next->coef+ppp->coef;//注意不要写错哦 
                }
                else{
                    psn->next=ppp;
                    psn=psn->next;
                    psn->next=NULL;
                }
                p22=p22->next;
            }
            p11=p11->next;
        } 
        return ps;
        
    } 
    pNode Add(pNode p1,pNode p2){
        pNode ps,pp1,ppp;//pp指向链表头部,pp1指向链表尾部,ppp指向新节点 
        pNode p11,p22;//p11,p22都指向链表尾部 
        p11=p1->next;
        p22=p2->next;
        ps=new node;
        pp1=ps;
        while(p11&&p22){
            ppp=new node;
            if(p11->expon>p22->expon){
                ppp->expon=p11->expon;
                ppp->coef=p11->coef;
                pp1->next=ppp;
                pp1=pp1->next;
                p11=p11->next;
            }
            else if(p11->expon<p22->expon){
                ppp->expon=p22->expon;
                ppp->coef=p22->coef;
                pp1->next=ppp;
                pp1=pp1->next;
                pp1->next=NULL;
                p22=p22->next;
            }
            else if(p11->expon==p22->expon){
                ppp->expon=p11->expon;
                ppp->coef=p11->coef+p22->coef;
                pp1->next=ppp;
                pp1=pp1->next;
                p11=p11->next;
                p22=p22->next; 
            }
        } 
        if(p11!=NULL){
            pp1->next=p11;
        }
        if(p22!=NULL){
            pp1->next=p22;
        }
        return ps;
    }
    pNode sort(pNode p){//冒泡排序 
        pNode r,t;
        int ti;
        double td;
        r=p->next;
        while(r){
            t=r->next;
            while(t){
                if(t->expon>r->expon){
                ti=t->expon;
                td=t->coef;
                t->expon=r->expon;
                t->coef=r->coef;
                r->expon=ti;
                r->coef=td;            
            }
            t=t->next; 
            }
            r=r->next;
        }
        return p;
    }
    int main(){
        pNode p1,p2,pp,ps;
        p1=ReadPoly(p1);
        p2=ReadPoly(p2);
        pp=Mult(p1,p2);
        pp=sort(pp);
        printPoly(pp);
        ps=Add(p1,p2);
        pp=sort(pp);
        printPoly(ps);
        return 0;
    }

    输入样例:

    2 1 2.4 0 3.2
    2 2 1.5 1 0.5

    样本输出:

    3 3 3.6 2 6.0 1 1.6
    3 2 1.5 1 2.9 0 3.2

    运行截图:

  • 相关阅读:
    SQL Server 2005高可用性之镜像功能
    Linux的常见问题解答和管理技巧
    安装sharepoint server 2007步骤
    CISCO 中对OSI的解释
    CISCO 2600 密码恢复
    三层交换机与路由器的比较
    PHP的注释
    php的变量、常量和数据类型
    操作符与控制结构
    【1】淘宝sdk装修入门引言
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14284567.html
Copyright © 2020-2023  润新知