c++数据结构试验用链表实现多项式类

#include"iostream"
using namespace std;
struct Node {
	float coef;//多项式系数
	int expn;//指数
	Node  *next;//<T>可省略
	Node(float _coef = 0,int _expn = 0, Node*_next = NULL) {
		coef = _coef;
		expn = _expn;
		next = _next;
	}
};
class PolyLinklist{
private:
	Node*front;
public:
	PolyLinklist() { front = new Node; front->next = NULL; front->coef = 0; front->expn = 0; }//默认构造函数
	PolyLinklist(float a[],int b[], int n);//多项式类的构造函数
	PolyLinklist(PolyLinklist&a) { front = new Node; front->next=a.GetFront()->next;};//复制构造函数
	void append(float c,int e);//在末尾插入元素
	void add(PolyLinklist &a);//相加函数
	void minus(PolyLinklist&a);//相减函数
	void Derivation();//求导求导函数
	void PrintList();//遍历输出多项式
	void ReadPoly();//输入多项式
	double cal(double x = 0);//计算
	Node*GetFront() { return front; }//返回对应的头结点
	void clear();//清空类
};
PolyLinklist::PolyLinklist(float a[],int b[], int n)
{
	front = new Node;
	Node*p = NULL;
	for (int k = n - 1; k >= 0; k--)
	{
		p = new Node;
		p->expn = b[k];
		p->coef = a[k];
		p->next = front->next;
		front->next = p;

	}
	
}
void PolyLinklist::add(PolyLinklist &b) {
	Node*p_prior = front;
	Node*p = p_prior->next;
	Node*q = b.GetFront()->next;
	while (p&&q) {
		if (p->expn < q->expn)//当节点的指数p<q时
		{
			p_prior = p;
			p = p->next;
		}
		else if (p->expn > q->expn)//指数p>q时
		{
			p_prior->next = q;
			p_prior = q;
			q = q->next;
			p_prior->next = p;
		}
		else {
			p->coef += q->coef;
			if (p->coef == 0)//系数相加为零时,删除
			{
				p_prior->next = p->next;
				delete p;
				p = p_prior->next;
			}
			else {//不为零时,p向下移动
				p_prior = p;
				p = p_prior->next;
			}
			q = q->next;
		}
	}
	if (q) {//当b的项数大于对应的a的项数时
		while (q) {
			Node*temp = new Node;
			p_prior->next = temp;
			p_prior = temp;
			temp->expn = q->expn;
			temp->coef = q->coef;
			q = q->next;
				}
			}
}
void PolyLinklist::minus(PolyLinklist&b)
{
	Node*p_prior = front;
	Node*p = p_prior->next;
	Node*q = b.GetFront()->next;
	while (p&&q) {
		if (p->expn < q->expn)
		{
			p_prior = p;
			p = p->next;
		}
		else if (p->expn > q->expn)
		{
			p_prior->next = q;
			p_prior = q;
			q = q->next;
			p_prior->next = p;
		}
		else {
			p->coef -= q->coef;
			if (p->coef == 0)
			{
				p_prior->next = p->next;
				delete p;
				p = p_prior->next;
			}
			else {
				p_prior = p;
				p = p_prior->next;
			}
			q = q->next;
		}
	}
	if (q) {
		while (q) {
			Node*temp = new Node;
			p_prior->next = temp;
			p_prior = temp;
			temp->expn = q->expn;
			temp->coef = -q->coef;
			q = q->next;
		}
	}
}
void PolyLinklist::PrintList() {
	Node*p =front->next;
	while (p) {
		cout << "(" << p->coef << "," << p->expn << ")" ;
		p = p->next;
	}
	cout << endl;
}
void PolyLinklist::Derivation() {
	Node*p_front = front;
	Node*p = p_front->next;
	while (p)
	{
		if (p->expn == 0)//当对应节点为常数时,删去
		{
			p_front->next = p->next;
			Node*temp = p;
			p = p->next;
			delete temp;
		}
		p->coef *= p->expn;//对于非常数的项,进行对应的求导运算法则
		p->expn -= 1;
		p = p->next;
	}
}
void PolyLinklist::append(float c,int e) {
	Node*rear = front;
	while (rear->next){rear = rear->next;}
	Node*s = new Node;
	s->coef = c;
	s->expn = e;
	rear->next = s;
	s->next = NULL;
}
void PolyLinklist::ReadPoly()
{
	cout << "输入多项式的项数" << endl;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		float c; int e;
		cin >> c >> e;
		append(c, e);
	}
}
double PolyLinklist::cal(double x) {//计算值
	Node*p = front->next;
	double sum = 0;
	while (p)
	{
		sum += p->coef*pow(x, p->expn);
		p = p->next;
	}
	return sum;
}
void PolyLinklist::clear() {//清空
	Node*temp = GetFront()->next;
	while (temp)
	{
		Node*p = temp->next;
		delete temp;
		temp = p;
	}
	GetFront()->next = NULL;

}
PolyLinklist Mul(PolyLinklist&a,PolyLinklist&b) {//乘法
	Node*p = a.GetFront()->next;
	PolyLinklist clist;
	while (p)
	{
		Node*q = b.GetFront()->next;
		PolyLinklist dlist;//生成对应的添加元素对象
		while (q) {
			float coef = (p->coef)*(q->coef);
			int expn = (p->expn) + (q->expn);
			dlist.append(coef, expn);
			q = q->next;
		          }
		clist.add(dlist);//两个对象相加
		dlist.clear();//对dlist清空,确保不会影响下一次内循环
		p = p->next;
	}
	return clist;//返回对象
}
int main()
{
	float a[3] = { 1,2,3 }, c[4] = { 1,2,3,4 };
	int b[3] = { 1,3,4 }, d[4] = { 1,3,4,5 };
	PolyLinklist alist(a,b,3), blist(c,d,4);
	cout << "alist" << endl;
	alist.PrintList();
	cout << "blist" << endl;
	blist.PrintList();
    cout << "alist,blist相加" << endl;
	alist.add(blist);
	alist.PrintList();
	cout << "alist计算" << endl;
	cout << alist.cal() << endl;
	cout << alist.cal(2) << endl;
	cout << "alist求导" << endl;
	alist.Derivation();
	alist.PrintList();
	cout << "alist,blist求乘" << endl;
	PolyLinklist clist = Mul(alist, blist);
	clist.PrintList();
	PolyLinklist dlist;
	dlist.ReadPoly();
	dlist.PrintList();
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值