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;
}
}