c++一元多项式的乘法和加法

 
 

首先是结点类:

Term.h代码如下:

#ifndef CH_H
#define CH_H
#include<iostream>
#include <stdlib.h>
#include <fstream>
#include <assert.h>
using namespace std;
#endif

struct Term
{
	float coef;
	int exp;
	Term *link;
	Term(float c, int e, Term* next = NULL) :coef(c), exp(e), link(next){ }
	Term* InsertAfter(float c, int e);
	friend ostream& operator<<(ostream&, const Term&);
};


Term.cpp代码如下:

#include "Term.h"

Term* Term::InsertAfter(float c, int e)
{
	//插入一个新项
	link = new Term(c, e, link);
	return link;
}

ostream& operator<<(ostream& out, const Term& x)
{
	if (x.coef == 0.0)
	{
		return out;
	}
	switch (x.exp)
	{
	case 0:
		out << x.coef;
		break;
	case 1:
		out << x.coef << "X";
		break;
	default:
		if (1 == x.coef)
		{
			out << "X^" << x.exp;
		}
		else if (-1 == x.coef)
		{
			out << "-X^" << x.exp;
		}
		else
		{
			out << x.coef << "X^" << x.exp;
		}
		break;
	}
	return out;
}

Polynomial.h代码如下:

#ifndef  POLYNOMAL_H
#define  POLYNOMAL_H
#include "Term.h"

class Polynomial
{
public:
	Polynomial(){ first = new Term(0, -1); }
	Polynomial(Polynomial& R);
	int maxOrder();
	Term *getHead(){ return first; }
private:
	Term *first;
	friend ostream& operator<<(ostream&, const Polynomial&);
	friend istream& operator>>(istream&, Polynomial&);
	friend Polynomial operator+(Polynomial&, Polynomial&);
	friend Polynomial operator*(Polynomial&, Polynomial&);
};



#endif

polynomial.cpp代码如下 :

#include "Polynomal.h"

Polynomial::Polynomial(Polynomial& R)
{
	first = new Term(0, -1);
	Term* destptr = first, *srcptr = R.getHead()->link;
	while (srcptr != NULL)
	{
		destptr->InsertAfter(srcptr->coef, srcptr->exp);//在最后面添加一个新节点,他的指针域为空。
		srcptr = srcptr->link;
		destptr = destptr->link;
	}
}

int Polynomial::maxOrder()
{
	Term* current = first;
	while (current->link != NULL)
	{
		current = current->link;
	}
	return current->exp;
}

istream& operator>>(istream& in, Polynomial& x)
{
	Term* rear = x.getHead();
	float c;
	int e;
	while (1)
	{
		//cout << "输入了一个个节点的coef,exp!" << endl;
		in >> c >> e;
		if (e < 0)
		{
			break;
		}
		rear = rear->InsertAfter(c, e);
	}
	return in;
}

ostream& operator<<(ostream& out, const Polynomial& x)
{
	Term *current = x.first->link;//x.getHead()->link;
	cout << "The Polynomial is:" << endl;
	bool h = true;
	while (current != NULL)
	{
		if (h == false && current->coef > 0.0)
		{
			out << "+";
		}
		h = false;
		out << *current;
		current = current->link;
	}
	out << endl;
	return out;
}

Polynomial operator+(Polynomial& A, Polynomial& B)
{
	Term *pa, *pb, *pc, *p;
	float temp;
	Polynomial C;
	pc = C.first;
	pa = A.getHead()->link;
	pb = B.getHead()->link;
	while (pa != NULL&&pb != NULL)
	{
		if (pa->exp == pb->exp)
		{
			temp = pa->coef + pb->coef;
			if (fabs(temp) > 0.001)//相加后系数不为零
			{
				pc = pc->InsertAfter(temp, pa->exp);
				pa = pa->link;
				pb = pb->link;
			}
			else
			{
				pa = pa->link;
				pb = pb->link;
			}
		}
		else if (pa->exp < pb->exp)
		{
			pc = pc->InsertAfter(pa->coef, pa->exp);
			pa = pa->link;
		}
		else
		{
			pc = pc->InsertAfter(pb->coef, pb->exp);
			pb = pb->link;
		}
	}
	if (pa != NULL)
	{
		p = pa;
	}
	else
	{
		p = pb;
	}
	while (p != NULL)
	{
		pc = pc->InsertAfter(p->coef, p->exp);
		p = p->link;
	}
	return C;
}

Polynomial operator*(Polynomial& A, Polynomial& B)
{
	Term *pa, *pb, *pc;
	int AL, BL, i, k, maxExp;
	Polynomial C;
	pc = C.getHead();
	AL = A.maxOrder();
	BL = B.maxOrder();
	if (AL != -1 || BL != -1)
	{
		maxExp = AL + BL;
		float *result = new float[maxExp + 1];
		for (i = 0; i <= maxExp; i++)
		{
			result[i] = 0.0;
		}
		pa = A.getHead()->link;
		while (pa != NULL)
		{
			pb = B.getHead()->link;
			while (pb != NULL)
			{
				k = pa->exp + pb->exp;
				result[k] = result[k] + pa->coef*pb->coef;
				pb = pb->link;
			}
			pa = pa->link;
		}
		for (i = 0; i <= maxExp; i++)
		{
			if (fabs(result[i]) > 0.01)
			{
				pc = pc->InsertAfter(result[i], i);
			}
		}
		delete[] result;
	}
	pc->link = NULL;
	return C;
}

main.cpp代码如下:

#include "Polynomal.h"


void  main()
{
	Polynomial pl1,pl2,temp;
	ifstream fin("list.txt");
	fin >> pl1;
	cout << pl1;
	fin.close();

	ifstream fin1("list2.txt");
	fin1 >> pl2;
	cout << pl2;
	fin1.close();
	
	cout << "加法:" << endl;
	temp = pl1 + pl2;
	cout << temp;

	cout << "乘法:" << endl;
	temp = pl1*pl2;
	cout << temp;
	
	while (1)
	{

	}
}

其中 在根目录下建立两个list.txt 、list2.txt.

list.txt类容如下:5 0 3 2  7 3 2 5  -1 -1(两个数为一组,表示一元多项式的某一项的系数和指数,-1 -1 表示输入停止

list2.txt类容如下:2 1 4 3 9 4 -1 -1(同上)

效果图如下:

c++一元多项式的乘法和加法_第1张图片




你可能感兴趣的:(数据结构,C++,源代码,多项式)