前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【C++实验】多项式加减

【C++实验】多项式加减

作者头像
哈__
发布2024-06-23 12:37:31
520
发布2024-06-23 12:37:31
举报
文章被收录于专栏:哈哈熊哈哈熊

题目:一元多项式运算

基本要求:

    (1) 输入并建立多项式;

    (2) 输出多项式;

    (3) 多项式加法

    (4) 多项式减法。

测试数据:


 代码展示:

代码语言:javascript
复制
#include<iostream>
using namespace std;
class LinkedNode
{
public:
	LinkedNode(double COEF, double INDEX, LinkedNode *ptr = NULL)
	{
		this->coef = COEF;
		this->index = INDEX;
		this->next = ptr;
	}
	double coef;
	double index;
	LinkedNode *next;
};

class LinkedList
{
public:
	LinkedList()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	void create_polumerization(); //创建多项式  并且进行排序和重复次方的合并 
	void add_polumerization(LinkedList *p, char a); //多项式的运算 
	void print_result(LinkedList *p);  //打印输出多项式 
	void sort_polumerization();  //多项式的	排序	 
	void delete_samenode();//相同次方进行合并 
private:
	LinkedNode *head;
	LinkedNode *tail;
};
void LinkedList::create_polumerization()
{
	double index, coef;
	cout << "开始输入多项式(默认系数 为0时结束)" << endl;
	for (int i = 0;;i++)
	{
		printf("\n请输入第%d项的系数:", i + 1);
		cin >> coef;
		if (coef == 0) break;
		printf("\n请输入第%d项的指数:", i + 1);
		cin >> index;
		if (tail == NULL)
		{
			tail = new LinkedNode(coef, index);
			head = tail;
		}
		else
		{
			LinkedNode *temp = new LinkedNode(coef, index);
			tail->next = temp;
			tail = temp;
		}
	}
	//进行排序
	sort_polumerization();
	delete_samenode();
};
void LinkedList::sort_polumerization()
{
	LinkedNode *pre=NULL, *p=NULL, *t = NULL;
	if (head != NULL)
	{
		while (head->next != t)
		{
			pre = head;
			p = head->next;
			while (p != t)
			{
				if (pre->index > p->index)
				{
					swap(pre->coef, p->coef);
					swap(pre->index, p->index);
					pre = p;
					p = p->next;
				}
				else
				{
					pre = p;
					p = p->next;
				}
			}
			t = pre;
		}
	}
	
};
void LinkedList::delete_samenode()
{
	if (head == NULL) return;
	for (LinkedNode* temp = head;temp != NULL && temp->next != NULL;)
	{
		if (temp->index == temp->next->index)
		{
			LinkedNode *p = temp;
			temp = temp->next;
			while (p->index == temp->index)
			{
				p->coef += temp->coef;
				p->next = temp->next;
				delete temp;
				temp = p->next;
			}
		}
		else temp = temp->next;
	}
}
void LinkedList::print_result(LinkedList *p)
{
	double count = 0;
	for (LinkedNode *temp = p->head->next;temp != NULL;temp = temp->next)
	{
		if (temp == p->head->next)
		{
			if (temp->coef == 0) continue;
			else
			{
				count += temp->coef;
				if(temp->index!=0) cout << temp->coef << "x^" << temp->index;
				else cout << temp->coef;
			}
		}
		else
		{
			if (temp->coef == 0) continue;
			else
			{
				if (temp->coef > 0)
				{
					count += temp->coef;
					if(temp->index!=0) cout << "+" << temp->coef << "x^" << temp->index;
					else cout<<"+" << temp->coef;
				}
				else
				{
					count += temp->coef;
					if(temp->index!=0) cout << temp->coef << "x^" << temp->index;
					else cout << temp->coef;
				}
			}
		}
	}
	if (count == 0) cout << count;
}
void LinkedList::add_polumerization(LinkedList *p, char a)
{
	LinkedNode *h1 = this->head;
	LinkedNode *h2 = p->head;
	LinkedList *result = new LinkedList();
	LinkedNode *h = new LinkedNode(0, 0);
	result->head = h;
	result->tail = result->head;
	cout << endl;
	if (a == '+')
	{
		while (h1 != NULL && h2 != NULL) 
		{
			if (h1->index < h2->index)
			{
				LinkedNode *temp = new LinkedNode(h1->coef, h1->index);
				result->tail->next = temp;
				result->tail = temp;
				h1 = h1->next;
			}
			else if (h2->index < h1->index)
			{
				LinkedNode *temp = new LinkedNode(h2->coef, h2->index);
				result->tail->next = temp;
				result->tail = temp;
				h2 = h2->next;
			}
			else 
			{
				LinkedNode *temp = new LinkedNode(h1->coef + h2->coef, h1->index);
				result->tail->next = temp;
				result->tail = temp;
				h1 = h1->next;
				h2 = h2->next;
			}
		}
		while (h1 != NULL)
		{
			LinkedNode *temp = new LinkedNode(h1->coef, h1->index);
			result->tail->next = temp;
			result->tail = temp;
			h1 = h1->next;
		}
		while (h2 != NULL)
		{
			LinkedNode *temp = new LinkedNode(h2->coef, h2->index);
			result->tail->next = temp;
			result->tail = temp;
			h2 = h2->next;
		}
	}
	else
	{
		while (h1 != NULL && h2 != NULL)
		{
			if (h1->index < h2->index)
			{
				LinkedNode *temp = new LinkedNode(h1->coef, h1->index);
				result->tail->next = temp;
				result->tail = temp;
				h1 = h1->next;
			}
			else if (h2->index < h1->index)
			{
				LinkedNode *temp = new LinkedNode(-h2->coef, h2->index);
				result->tail->next = temp;
				result->tail = temp;
				h2 = h2->next;
			}
			else 
			{
				LinkedNode *temp = new LinkedNode(h1->coef - h2->coef, h1->index);
				result->tail->next = temp;
				result->tail = temp;
				h1 = h1->next;
				h2 = h2->next;
			}
		}
		while (h1 != NULL)
		{
			LinkedNode *temp = new LinkedNode(h1->coef, h1->index);
			result->tail->next = temp;
			result->tail = temp;
			h1 = h1->next;
		}
		while (h2 != NULL)
		{
			LinkedNode *temp = new LinkedNode(-h2->coef, h2->index);
			result->tail->next = temp;
			result->tail = temp;
			h2 = h2->next;
		}
	}

	print_result(result);
}
int main()
{
	char a;
	LinkedList l1;
	cout << "开始输入第一个多项式" << endl;
	l1.create_polumerization();
	cout << endl << "开始输入第二个多项式" << endl;
	LinkedList l2;
	l2.create_polumerization();
	cout << "请输入两个多项式之间的运算关系('+'或'-'):";
	cin >> a;
	l1.add_polumerization(&l2, a);
}

运行结果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-06-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档