数据结构课设(C++)——一元稀疏多项式简单计数器

(1) 输入并建立多项式
(2) 输出多项式,输出形式为整数序列:n,c1,e1,c2,e2……cn,en,其中n是多项式的项数,ci,ei分别为第i项的系数和指数。序列按指数降序排列。
(3) 多项式a和b相加,建立多项式a+b,输出相加的多项式。
(4) 多项式a和b相减,建立多项式a-b,输出相减的多项式。
用带头结点的单链表存储多项式。
测试数据:
(1)(2x+5x8-3.1x11)+(7-5x8+11x9)
(2) (6x-3-x+4.4x2-1.2x9)-(-6x-3+5.4x2+7.8x15)
(3)(x+x2+x3)+0
(4)(x+x3)-(-x-x-3)

#include<iostream>
using namespace std;

class PolyNode;
ostream& operator<<(ostream& out, PolyNode& N);
istream& operator>>(istream& in, PolyNode& N);
typedef class PolyNode* PolyList;
class PolyNode
{
private:
	int m_n;//若不是头节点,则为对应项的幂次,若是头节点,则为整个多项式的项数
	float m_c;//若不是头节点,则为对应项的系数,若是头节点,则为零
	class PolyNode* m_next;
public:
	//三个构造函数的重载
	PolyNode(int n, float c) :m_n(n), m_c(c)
	{
	}
	PolyNode(int n) :m_n(n)
	{
		m_c = 0;
	}
	PolyNode()
	{
		m_n = 0;
		m_c = 0;
	}
	//复制构造函数
	PolyNode(const PolyNode& L)
	{
		m_n = L.m_n;
		m_c = L.m_n;
		m_next = new PolyNode;
		m_next = L.m_next;
	}
	void Init();//头节点的初始化
	void Insert(int flag);//输入单个项,当flag==1时即表示输入的是第一个结点,flag=0表示输入的是其他结点
	void PolyInsert();//按照降幂输入多项式
	PolyNode operator+(const PolyNode& L);//+重载
	PolyNode operator-(const PolyNode& L);//-重载
	void operator=(const PolyNode& L);//=重载
	void print();//打印输出该多项式
	void Inspect();//检查生成的多项式链表中是否有系数为零的结点,若有则删除该节点
	friend ostream& operator<<(ostream& out, PolyNode& N);//输出流重载
	friend istream& operator>>(istream& in, PolyNode& N);//输入流重载
};
ostream& operator<<(ostream& out, PolyNode& N)
{
	out << N.m_c << "," << N.m_n;
	return out;
}
istream& operator>>(istream& in, PolyNode& N)
{
	in >> N.m_c >> N.m_n;
	return in;
}
void PolyNode::Init()
{
	cout << "输入多项式的项数:";
	cin >> this->m_n;
	this->m_c = 0;
	this->m_next = new PolyNode;
}
void PolyNode::Insert(int flag)
{
	if (flag)
	{
		cin >> *(this->m_next);
		m_next->m_next = NULL;
	}
	else
	{
		PolyNode* temp1 = this->m_next;
		PolyNode* temp2 = this->m_next->m_next;
		PolyNode* Temp = new PolyNode;
		cin >> *Temp;
		Temp->m_next = NULL;
		if (Temp->m_n > temp1->m_n)
		{
			this->m_next = Temp;
			Temp->m_next = temp1;
		}
		else
		{
			while ((temp1->m_next != NULL) && (Temp->m_n < temp2->m_n))
			{
				temp1 = temp1->m_next;
				temp2 = temp2->m_next;
			}
			if (temp1->m_next == NULL)
				temp1->m_next = Temp;
			else
			{
				temp1->m_next = Temp;
				Temp->m_next = temp2;
			}
		}
	}
}
void PolyNode::PolyInsert()
{
	cout << "按项输入系数和次数:";
	int flag = 1;
	for (int i = 0;i < this->m_n;i++)
	{
		if (i != 0)
			flag = 0;
		Insert(flag);
	}
}
PolyNode PolyNode::operator+(const PolyNode& L)
{
	PolyNode* Temp = new PolyNode;
	Temp->m_next = new PolyNode;
	PolyNode* temp = Temp->m_next;
	const PolyNode* temp1 = &L;
	PolyNode* temp2 = this->m_next;
	int N = 0;
	while (temp1->m_next != NULL && temp2 != NULL)
	{
		if (temp1->m_next->m_n == temp2->m_n)
		{
			temp->m_c = temp1->m_next->m_c + temp2->m_c;
			temp->m_n = temp2->m_n;
			N++;
			temp->m_next = new PolyNode;
			temp = temp->m_next;
			temp1 = temp1->m_next;
			temp2 = temp2->m_next;
		}
		else if (temp1->m_next->m_n > temp2->m_n)
		{
			temp->m_c = temp1->m_next->m_c;
			temp->m_n = temp1->m_next->m_n;
			N++;
			temp->m_next = new PolyNode;
			temp = temp->m_next;
			temp1 = temp1->m_next;
		}
		else if (temp1->m_next->m_n < temp2->m_n)
		{
			temp->m_c = temp2->m_c;
			temp->m_n = temp2->m_n;
			N++;
			temp->m_next = new PolyNode;
			temp = temp->m_next;
			temp2 = temp2->m_next;
		}
	}
	while (temp1->m_next != NULL)
	{
		temp->m_c = temp1->m_next->m_c;
		temp->m_n = temp1->m_next->m_n;
		N++;
		temp->m_next = new PolyNode;
		temp = temp->m_next;
		temp1 = temp1->m_next;
	}
	while (temp2 != NULL)
	{
		temp->m_c = temp2->m_c;
		temp->m_n = temp2->m_n;
		N++;
		temp->m_next = new PolyNode;
		temp = temp->m_next;
		temp2 = temp2->m_next;
	}
	Temp->m_n = N;
	Temp->Inspect();
	return *Temp;
}
PolyNode PolyNode::operator-(const PolyNode& L)
{
	PolyNode* Temp = new PolyNode;
	Temp->m_next = new PolyNode;
	PolyNode* temp = Temp->m_next;
	const PolyNode* temp1 = &L;
	PolyNode* temp2 = this->m_next;
	int N = 0;
	while (temp1->m_next != NULL && temp2 != NULL)
	{
		if (temp1->m_next->m_n == temp2->m_n)
		{
			temp->m_c = temp2->m_c - temp1->m_next->m_c;
			temp->m_n = temp2->m_n;
			N++;
			temp->m_next = new PolyNode;
			temp = temp->m_next;
			temp1 = temp1->m_next;
			temp2 = temp2->m_next;
		}
		else if (temp1->m_next->m_n > temp2->m_n)
		{
			temp->m_c = -temp1->m_next->m_c;
			temp->m_n = temp1->m_next->m_n;
			N++;
			temp->m_next = new PolyNode;
			temp = temp->m_next;
			temp1 = temp1->m_next;
		}
		else if (temp1->m_next->m_n < temp2->m_n)
		{
			temp->m_c = temp2->m_c;
			temp->m_n = temp2->m_n;
			N++;
			temp->m_next = new PolyNode;
			temp = temp->m_next;
			temp2 = temp2->m_next;
		}
	}
	while (temp1->m_next != NULL)
	{
		temp->m_c = -temp1->m_next->m_c;
		temp->m_n = temp1->m_next->m_n;
		N++;
		temp->m_next = new PolyNode;
		temp = temp->m_next;
		temp1 = temp1->m_next;
	}
	while (temp2 != NULL)
	{
		temp->m_c = temp2->m_c;
		temp->m_n = temp2->m_n;
		N++;
		temp->m_next = new PolyNode;
		temp = temp->m_next;
		temp2 = temp2->m_next;
	}
	Temp->m_n = N;
	Temp->Inspect();
	return *Temp;
}
void PolyNode::operator=(const PolyNode& L)
{
	this->m_c = 0;
	this->m_n = L.m_n;
	this->m_next = L.m_next;
}
void PolyNode::print()
{
	PolyNode* temp = this;
	cout << temp->m_n << ",";
	temp = temp->m_next;
	for (int i = 0;i < m_n;i++)
	{
		cout << *temp << ",";
		temp = temp->m_next;
	}
	cout << endl;
}
void PolyNode::Inspect()
{
	PolyList Temp = this;
	for (int i = 0;i < this->m_n;i++)
	{
		if (!Temp->m_next->m_c)
		{
			Temp->m_next = Temp->m_next->m_next;
			this->m_n--;
		}
		Temp = Temp->m_next;
	}
	Temp->m_next = NULL;
}
void menu()
{
	cout << "********************" << endl;
	cout << "*1.输入并建立多项式*" << endl;
	cout << "*2.多项式相加      *" << endl;
	cout << "*3.多项式相减      *" << endl;
	cout << "*4.输出多项式      *" << endl;
	cout << "*0.退出程序        *" << endl;
	cout << "********************" << endl;
}
int main()
{
	int flag = 1, x = 0, n = 0, i = 0, j = 0, k = 0;
	cout << "输入需要的多项式的个数:";
	cin >> n;
	PolyList P = new PolyNode[n];
	while (flag)
	{
		menu();
		cin >> x;
		switch (x)
		{
		case 1:cout << "输入需要进行操作的多项式序号:";
			cin >> i;
			P[i - 1].Init();
			P[i - 1].PolyInsert();
			break;
		case 2:cout << "依次输入两个被加多项式的序号、结果多项式需要存储到的序号:";
			cin >> i >> j >> k;
			P[k - 1] = P[i - 1] + P[j - 1];
			cout << "结果多项式为:";
			P[k - 1].print();
			break;
		case 3:cout << "依次输入被减式、减式、结果多项式需要存储到的序号:";
			cin >> i >> j >> k;
			P[k - 1] = P[i - 1] - P[j - 1];
			cout << "结果多项式为:";
			P[k - 1].print();
			break;
		case 4:cout << "输入需要输出的多项式的序号:";
			cin >> i;
			P[i - 1].print();
			break;
		case 0:flag = 0;
			break;
		}
	}
	return 0;
}
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值