zoukankan      html  css  js  c++  java
  • 一元多项式乘法

    #include<iostream>
    #include<malloc.h>
    #include<stdlib.h>
    #include<stdio.h>
    using namespace std;
    typedef struct node
    {
    float xs;
    int zs;
    struct node *next;
    }lnode,*linklist;

    linklist creat()
    {
    lnode *p,*q,*s;
    lnode *head=NULL;
    int zs;
    float xs;
    head=(linklist)malloc(sizeof(lnode));
    if(!head)
    return NULL;
    head->xs=0;
    head->zs=0;
    head->next=NULL;
    do
    {
    printf("输入系数xs(系数和指数都为零时结束)");
    cin>>xs;
    printf("输入系数zs(系数和指数都为零时结束)");
    cin>>zs;
    if(xs==0&&zs==0)
    {
    break;
    }
    s=(linklist)malloc(sizeof(lnode));
    if(!s)
    return NULL;
    s->zs=zs;
    s->xs=xs;
    q=head->next;
    p=head;
    while(q&&zs<q->zs)
    {
    p=q;
    q=q->next;
    }
    if(q==NULL||zs>q->zs)
    {
    p->next=s;
    s->next=q;
    }
    else
    q->xs +=xs;
    }while(1);
    return head;
    }
    lnode *reverse(linklist head)
    {
    lnode *q,*r,*p=NULL;
    q=head->next;
    while(q)
    {
    r=q->next;
    q->next=p;
    p=q;
    q=r;
    }
    head->next=p;
    return head;
    }
    lnode * mul(linklist A,linklist B)
    {
    lnode *pa,*pb,*pc,*u,*head;
    int k,maxexp;
    float xs;
    head=(linklist)malloc(sizeof(lnode));
    if(!head)
    return NULL;
    head->xs=0.0;
    head->zs=0;
    head->next=NULL;
    if(A->next!=NULL && B->next!=NULL)
    maxexp =A->next->zs+B->next->zs;
    else
    return head;
    pc=head;
    B=reverse(B);
    for(k=maxexp;k>=0;k--)
    {
    pa=A->next;
    while(pa!=NULL&&pa->zs>k)
    pa=pa->next;
    pb=B->next;
    while(pb!=NULL&&pa!=NULL&&pa->zs+pb->zs < k)
    pb=pb->next;
    xs=0.0;
    while(pa!=NULL&&pb!=NULL)
    {
    if(pa->zs+pb->zs==k)
    {
    xs+=pa->xs * pb->xs ;
    pa=pa->next;
    pb=pb->next;
    }
    else if(pa->zs+pb->zs > k)
    pa=pa->next;
    else
    pb=pb->next;
    }
    if(xs!=0.0)
    {
    u=(linklist)malloc(sizeof(lnode));
    u->xs=xs;
    u->zs=k;
    u->next=pc->next;
    pc->next=u;
    pc=u;
    }
    }
    B=reverse(B);
    return head;
    }

    void output(linklist head)
    {
    lnode *p=head->next;
    while(p)
    {
    cout<<p->xs ;
    if(p->zs)
    cout<<"*x^"<<p->zs ;
    if(p->next&&p->next->xs>0)
    cout<<"+";
    p=p->next;
    }
    }
    int main()
    {
    linklist A,B,C;
    A=creat();
    cout<<"A(X)=";
    output(A);
    cout<<endl;
    B=creat();
    cout<<"B(X)=";
    output(B);
    cout<<endl;
    C=mul(A,B);
    printf("C(X)=A(X) * B(X)=");
    output(C);
    cout<<endl;

    }

  • 相关阅读:
    MySQL多实例,主从同步
    MySQL+Sphinx实现全文搜索
    Es+kafka搭建日志存储查询系统(设计)
    CSS中的rem
    JavaScript 中变量、作用域和内存问题的学习
    LVS使用整理(1)
    【Spark】---- Spark 硬件配置
    javascript的正则表达式总结
    javascript中的contains方法和compareDocumentPosition方法
    JavaScript DOM的一些扩展
  • 原文地址:https://www.cnblogs.com/mykonons/p/6596671.html
Copyright © 2011-2022 走看看