码迷,mamicode.com
首页 > 其他好文 > 详细

一元多项式的加法和乘法

时间:2015-04-13 09:35:43      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:数据结构   一元多项式   加法   乘法   链表   

#include <iostream>
#include <stdlib.h>
using namespace std;

typedef struct
{
	int coef;	//系数项
	int exp;	//指数项 
}ElemType; 

typedef struct Node
{
	ElemType data;
	struct Node *next;
}LNode,*LinkList;

void InitList(LinkList &L);
void PolyCreate(LinkList l);
void Output(LinkList l);
LinkList PolyAdd(LinkList La,LinkList Lb);
LinkList Multiply(LinkList l,int coef,int exp);
LinkList PolyMultiply(LinkList la,LinkList lb);

void ShowMenu()
{
    cout<<"1、演示一元多项式的加法:"<<endl;
    cout<<"2、演示一元多项式的乘法:"<<endl;
    cout<<"0、退出"<<endl;
}

int main()
{
    ShowMenu();
    int x;
    while(1)
    {
        cout<<"请输入你要进行的操作:";
        cin>>x;
        switch(x)
        {
            case 1:{
                LinkList l;
                InitList(l);
                PolyCreate(l);
                Output(l); 
                
                LinkList L;
                InitList(L);
                PolyCreate(L);
                Output(L);
                LNode *p = PolyAdd(l,L);
                cout<<"相加后的多项式为:"<<endl;
                Output(p);
                break;
            }
            case 2:{
                LinkList l1;
                InitList(l1);
                PolyCreate(l1);
                Output(l1); 
                LinkList l2;
                InitList(l2);
                PolyCreate(l2);
                Output(l2); 
                LNode *temp = PolyMultiply(l1,l2);
                cout<<"相乘后的多项式为:"<<endl;
                Output(temp);
                break;
            }
            case 0:{
                exit(0);
                break;
            }
        }
    }
   
	return 0;
}

void InitList(LinkList &L)
{
	L = new LNode;
	L->next = NULL;
}

void PolyCreate(LinkList l)
{
	int c,e;
	LNode *p = l;
	cout<<"请按照指数递增的顺序建立该一元多项式:"<<endl; 
	while(1)
	{
		cout<<"input the coef,exp of term ,exp=-1 will end:"<<endl;
		cin>>c;
		cin>>e;
		if(e==-1)
			break;
		LNode *s = new LNode;
		s->data.coef = c;
		s->data.exp = e;
		p->next = s;
		p = s;
	}
	p->next = NULL;
}

void Output(LinkList l)
{
	cout<<"The poly is:"<<endl;
	LNode *p;
	p=l->next;
	while(p)
	{
		cout<<p->data.coef;
		if(p->data.exp)
			cout<<"X^"<<p->data.exp;
		if(p->next && p->next->data.coef>0)
			cout<<"+";
		
		p = p->next;
	}
	cout<<"\n";
}

LinkList PolyAdd(LinkList La,LinkList Lb)
{
	LNode *p,*q,*tail,*temp;
	int sum;
	p = La->next;
	q = Lb->next;
	tail = La;
	
	while(p && q)
	{
		if(p->data.exp<q->data.exp)
		{
			tail->next = p;
			tail = p;
			p = p->next;
		}
		else if(p->data.exp == q->data.exp)
		{
			sum = p->data.coef+q->data.coef;
			if(sum!=0)
			{
				p->data.coef = sum;
				tail->next = p;		tail = p;
				p = p->next;
				temp = q;	q = q->next;
				delete temp;
			}
			else	//若系数和为0,删除节点p,q;并将指针指向下一个节点 
			{
				temp = p;	p = p->next; 	delete temp;
				temp = q;	q = q->next;	delete temp;				 
			}
		}
		else
		{
			tail->next = q;	
			tail = q;
			q = q->next;
		}
	}
	
	if(p)
		tail->next = p;
	else
		tail->next = q;
		
		delete Lb;
	
	return La;
}

//多项式与单项式相乘,该单项式为coef X^exp
LinkList Multiply(LinkList l,int coef,int exp)
{
    LNode *p,*q;
    LinkList Poly = new LNode;
    p = Poly;
    q = l->next;
    while(q)
    {
        p->next = new LNode;
        p = p->next;
        p->data.coef = (q->data.coef*coef);
        p->data.exp = (q->data.exp+exp);
        q = q->next;
    }
    p->next = NULL;

    return Poly;
}


//多项式与多项式相乘
LinkList PolyMultiply(LinkList la,LinkList lb)
{
    LinkList Poly;
    InitList(Poly);
    LNode *p = lb->next;
    while(p)
    {
        PolyAdd(Poly,Multiply(la,p->data.coef,p->data.exp));
        p = p->next;
    }

    return Poly;
}

一元多项式的加法和乘法

标签:数据结构   一元多项式   加法   乘法   链表   

原文地址:http://blog.csdn.net/huolang_vip/article/details/45015587

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!