0
点赞
收藏
分享

微信扫一扫

数据结构与算法作业2:一元多项式的基本运算

拾光的Shelly 2022-03-27 阅读 32
c++算法

例如:

输入Result
C
1 2 3 4 5 6 7 8 0
2 3 4 5 6 7 0
S
P
M
D
V
2
X
C(x)=x^2-2x^3+3x^4-4x^5+5x^6-6x^7+7x^8
C(x)=x^2+2x^3+3x^4+4x^5+5x^6+6x^7+7x^8
C(x)=2x^5+10x^7+28x^9+52x^11+58x^13+42x^15
C(x)=2x+12x^3+30x^5+56x^7
2164.00
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef struct node
{
    int dishu;
    int zhishu;
    struct node *next;
}Node;
class poly
{
private:
    Node *head;
    int leng;
public:
    poly();
    int len();
    void input(int tag);
    void Insert(int index,int x1,int x2);
    void add(poly a,poly b);
    void sub(poly a,poly b);
    void mul(poly a,poly b);
    void dif(poly a);
    void val(float v);
    void clr();
    void show();
};
poly::poly()
{
    head=new Node;
    if(head==NULL)
    {
        printf("memory allocation error!");
        exit(1);
    }
    head->next=NULL;
    leng=0;
}
int poly::len()
{
    return leng;
}
void poly::input(int tag)
{
    int a,b;
    Node *p=head;
    scanf("%d",&a);
    scanf("%d",&b);
    while(a!=0)
    {
        Node*t=new Node;
        if(t==NULL)
        {
            printf("memory allocation error!");
            exit(1);
        }
        t->dishu=a;
        t->zhishu=b;
        p->next=t;
        p=t;
        leng++;
        scanf("%d",&a);
        if(a==0)break;
        scanf("%d",&b);
    }
    p->next=NULL;
}
void poly::Insert(int index,int x1,int x2)
{
    if(index>leng+1||index<0)
    {
        printf("not insert!");
        exit(1);
    }
    int i=0;
    Node *p=head;
    while(p->next!=NULL&&i<=index-2)
    {
        p=p->next;
        i++;
    }
    Node *t=new Node;
    t->dishu=x1;
    t->zhishu=x2;
    t->next=p->next;
    p->next=t;
    leng++;
}
void poly::add(poly a,poly b)
{
	if(a.len()==0||b.len()==0)
    {
        printf("error!\n");
        return;
    }
    Node *t1,*t2;
    int k=0;
    t1=a.head->next;
    t2=b.head->next;
    while(t1!=NULL&&t2!=NULL)
    {
        if(t1->zhishu<t2->zhishu)
        {
            Insert(++k,t1->dishu,t1->zhishu);
            t1=t1->next;
        }
        else if(t1->zhishu>t2->zhishu)
        {
            Insert(++k,t2->dishu,t2->zhishu);
            t2=t2->next;
        }
        else
        {
            if(t1->dishu+t2->dishu!=0)Insert(++k,t1->dishu+t2->dishu,t2->zhishu);
            t1=t1->next;
            t2=t2->next;
        }
    }
    while(t1!=NULL)
    {
        Insert(++k,t1->dishu,t1->zhishu);
        t1=t1->next;
    }
    while(t2!=NULL)
    {
        Insert(++k,t2->dishu,t2->zhishu);
        t2=t2->next;
    }
}
void poly::sub(poly a,poly b)
{
	if(a.len()==0||b.len()==0)
    {
        printf("error!\n");
        return;
    }
    Node *t1,*t2;
    int k=0;
    t1=a.head->next;
    t2=b.head->next;
    while(t1!=NULL&&t2!=NULL)
    {
        if(t1->zhishu<t2->zhishu)
        {
            Insert(++k,t1->dishu,t1->zhishu);
            t1=t1->next;
        }
        else if(t1->zhishu>t2->zhishu)
        {
            Insert(++k,-t2->dishu,t2->zhishu);
            t2=t2->next;
        }
        else
        {
            if(t1->dishu-t2->dishu!=0)Insert(++k,t1->dishu-t2->dishu,t2->zhishu);
            t1=t1->next;
            t2=t2->next;
        }
    }
    while(t1!=NULL)
    {
        Insert(++k,t1->dishu,t1->zhishu);
        t1=t1->next;
    }
    while(t2!=NULL)
    {
        Insert(++k,-t2->dishu,t2->zhishu);
        t2=t2->next;
    }
}
void poly::mul(poly a,poly b)
{
	if(a.len()==0||b.len()==0)
    {
        printf("error!\n");
        return;
    }
    Node*t1,*t2,*t3;
    t1=a.head->next;
    t2=b.head->next;
    int k=0,x,y;
    while(t2!=NULL)
    {
        Insert(++k,t1->dishu*t2->dishu,t1->zhishu+t2->zhishu);
        t2=t2->next;
    }
    t1=t1->next;
    while(t1!=NULL)
    {
        t2=b.head->next;
        t3=head->next;
        while(t2!=NULL)
        {
            x=t1->dishu*t2->dishu;
            y=t1->zhishu+t2->zhishu;
            while(t3->next!=NULL&&t3->zhishu<y)
            {
                if(t3->next->zhishu<=y)t3=t3->next;
                else break;
            }
            if(t3->next==NULL)Insert(++k,x,y);
            else
            {
                if(t3->zhishu==y)
                {
                    if(t3->dishu+x!=0)t3->dishu+=x;
                    else
                    {
                        Node *t=new Node;
                        t=t3->next;
                        t3->next=t->next;
                        delete t;
                    }
                }
                else if(t3->zhishu<y)
                {
                    Node *temp=new Node;
                    temp->dishu=x;
                    temp->zhishu=y;
                    temp->next=t3->next;
                    t3->next=temp;
                    t3=t3->next;
                }
            }
            t2=t2->next;
        }
        t1=t1->next;
    }
    t1=head;
    while(t1->next!=NULL)
    {
        t2=t1->next;
        while(t2->next!=NULL)
        {
            if(t1->next->zhishu==t2->next->zhishu)
            {
                if(t1->next->dishu+t2->next->dishu==0)
                {
                    Node *t,*t0;
                    t=t1->next;
                    t1->next=t->next;
                    delete t;
                    leng--;
                    t0=t2->next;
                    t2->next=t0->next;
                    delete t0;
                    leng--;
                }
                else
                {
                    t1->next->dishu+=t2->next->dishu;
                    Node*t;
                    t=t2->next;
                    t2->next=t->next;
                    delete t;
                    leng--;
                }
            }
            t2=t2->next;
        }
        t1=t1->next;
    }
}
void poly::dif(poly a)
{
	if(a.len()==0)
    {
        printf("error!\n");
        return;
    }
    Node *t;
    int k=0;
    t=a.head;
    while(t->next!=NULL)
    {
        if(t->next->zhishu!=0)Insert(++k,t->next->dishu*t->next->zhishu,t->next->zhishu-1);
        t=t->next;
    }
    t->next=NULL;
}
void poly::val(float v)
{
    float sum=0;
    Node*t5;
    t5=head->next;
    while(t5!=NULL)
    {
        sum+=t5->dishu*pow(v,t5->zhishu);
        t5=t5->next;
    }
    printf("%.2f\n",sum);
}
void poly::clr()
{
    Node *t;
    while(head->next!=NULL)
    {
        t=head->next;
        head->next=t->next;
        delete t;
        leng--;
    }
}
void poly::show()
{
    Node*t;
    int k=1;
    t=head->next;
    printf("C(x)=");
    while(t!=NULL&&k<=leng)
    {
        if(t->dishu==-1)printf("-");
        else if(t->dishu!=1)
        {
            if(t->dishu>0&&t!=head->next)printf("+");
            if(t->dishu!=0)printf("%d",t->dishu);
        }
        if(t->zhishu!=0)
        {
            printf("x");
            if(t->zhishu!=1)printf("^%d",t->zhishu);
        }
        t=t->next;
        k++;
    }
    printf("\n");
}
int main()
{
    char op[5];
    float v;
    poly a,b,cadd,csub,cmul,cdiff;
    while(true)
    {
        scanf("%s",op);
        if(op[0]=='C')
        {
            if(a.len()==0)
            {
                a.input(0);
            }
            else a.clr();
            if(b.len()==0)
            {
                b.input(0);
            }
            else b.clr();
            if(cadd.len()!=0)cadd.clr();
            if(csub.len()!=0)csub.clr();
            if(cmul.len()!=0)cmul.clr();
            if(cdiff.len()!=0)cdiff.clr();
        }
        else if(op[0]=='S')
        {
            csub.sub(a,b);
            csub.show();
        }
        else if(op[0]=='P')
        {
            cadd.add(a,b);
            cadd.show();
        }
        else if(op[0]=='M')
        {
            cmul.mul(a,b);
            cmul.show();
        }
        else if(op[0]=='D')
        {
            cdiff.dif(a);
            cdiff.show();
        }
        else if(op[0]=='V')
        {
            scanf("%f",&v);
            a.val(v);
        }
        else if(op[0]=='X')exit(0);
    }
    return 0;
}

 

举报

相关推荐

0 条评论