!!!Polynomial ADT

A simple Polynomial ADT

1. can input each term and display the result

2. can add/multiply two polynomials

3. linked list implementation

Main.cpp

#include<iostream>
#include"POLY_DEF.h"
using namespace std;
int main()
{
// input the first polynomial
	Polynomial poly1=NewPoly();
	int coeff,exponent;
	cout << "input the first polynomial" << endl;
	while(cin >>coeff >> exponent)
	{
		Insert(coeff, exponent, poly1);
	}
	PrintPoly(poly1);
// input the seconde polynomial
	cout << "input the second polynomial" << endl;
	Polynomial poly2=NewPoly();
	/*clear the bad bit, and we should use EOF when finish input*/
	cin.clear();
	while(cin >>coeff >> exponent)
	{
		Insert(coeff, exponent, poly2);
	}
	PrintPoly(poly2);
// Add two polynomial
	Polynomial polysum = NewPoly();
	AddPolynomial(poly1, poly2, polysum);
	cout << "The sume is:" << endl;
	PrintPoly(polysum);
// Multiple two polynomial
	Polynomial polyprod = NewPoly();
	MultPolynomial(poly1, poly2, polyprod);
	cout << "The prod is:"<< endl;
	PrintPoly(polyprod);
}
POLY_DEF.h

#ifndef POLY_DEF

struct Node;
typedef struct Node *Polynomial;
typedef Polynomial Position;
typedef int Coeff;
typedef int Exponent;

void AddPolynomial( const Polynomial Poly1, const Polynomial Poly2, Polynomial PolySum );
void MultPolynomial( const Polynomial Poly1, const Polynomial Poly2, Polynomial PolyProd );
Position Find(Exponent e, Polynomial Poly);
void Insert(Coeff c, Exponent e, Polynomial poly);
Polynomial NewPoly();
void PrintPoly(Polynomial poly);

#endif
POLY_DEF.cpp

#include "POLY_DEF.h"
#include<iostream>
using namespace std;

struct Node
{
	Coeff c;
	Exponent e;
	Position Next;
};

//If the polynomial contains nothing, it's 0
Polynomial NewPoly()
{
	Polynomial Header=new(struct Node);
	Header->c=0;       //Header is sentinal
	Header->e=0;
	Header->Next=0;
	return Header;
}

void Insert(Coeff c, Exponent e, Polynomial poly)  //insert new term at the end of the polynomial
{
	Position newterm=new(struct Node);
	newterm->c=c;
	newterm->e=e;
	newterm->Next=0;
	Position p=poly->Next;
	Position pPrev = poly;
	while(p!=0 && p->e<e)
	{
		pPrev = p;
		p = p->Next;
	}
	if(p!=0 && p->e==e)
		p->c+=c;
	else
	{
		pPrev->Next=newterm;
		newterm->Next=p;
	}
}

void PrintPoly(Polynomial poly)
{
	if(poly->Next==0)
	{
		cout << "empty Polynomial" << endl;
		return;
	}
	else
	{
		Position p=poly->Next;
		cout << p->c << "X" << p->e;	
		p = p->Next;
		while(p!=0)
		{
			 cout << " + " << p->c << "X" << p->e;	
			 p = p->Next;	 
		}
		cout << endl;
	}
}

void AddPolynomial( const Polynomial poly1, const Polynomial poly2, Polynomial polySum )
{
	Position p1=poly1->Next, p2=poly2->Next;
	while(p1!=0 && p2!=0)
	{
		if(p1->e < p2->e)
		{
			Insert(p1->c, p1->e, polySum);
			p1 = p1->Next;
			continue;
		}
		else if(p1->e > p2->e)
		{
			Insert(p2->c, p2->e, polySum);
			p2 = p2->Next;
			continue;
		}
		else
		{
			Insert(p1->c+p2->c, p1->e, polySum);
			p1 = p1->Next;
			p2 = p2->Next;
			continue;
		}
	}
	if(p1==0)
	{
		while(p2!=0)
		{
			Insert(p2->c, p2->e, polySum);
			p2 = p2->Next;
		}
	}
	else if(p2==0)
	{
		while(p1!=0)
		{
			Insert(p1->c, p1->e, polySum);
			p1 = p1->Next;
		}
	}
}

void MultPolynomial( const Polynomial poly1, const Polynomial poly2, Polynomial polyProd )
{
	Position p1=poly1->Next,p2;
	while(p1!=0)
	{
		p2 = poly2->Next;
		while(p2!=0)
		{
			Insert(p1->c*p2->c,p1->e+p2->e,polyProd);
			p2 = p2->Next;
		}
		p1=p1->Next;
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值