多项式相加

题目描述
在数学上,一个一元多项式可按升幂写成a0+a1x+a2x2+…+anxn。求2个多项式相加后的结果。
输入
输入2个一元多项式。
由于通过键盘不能输入上标,故多项式的输入格式为a0+a1x+a2x2+…+anxn。其中,a0~an都是整数,n为正整数。
输出
输出有2行。
第一行是2个一元多项式相加后的结果。
第二行是将第一行结果各项逆置后的结果。
样例输入 Copy
7+3x+9x8+5x17
8x+22x7-9x8
样例输出 Copy
7+11x+22x7+5x17
5x17+22x7+11x+7

#include<iostream>
#include<string>
using namespace std;

struct node {
	int value;
	int weight;
	node* next;
};

class list {
public:
	list();
	int length()const { return count; }


	node* get_head() { return head; }

	int stoi(string s);
	void dataplus(string s);
	void create();						//表尾插入,顺序与输入一致 

	void print();

	void add_R(node* n);
	void add_H(node* n);
private:
	node* head;
	node* rear;
	int count;
};
//----------------------------------------------

list::list() {
	head = new node;
	rear = head;
	head->next = rear;
	count = 0;
}

/*{error_code list::get_element(const int i,int &x){	//此i表示第几个数,和下标差1
	if(i<1||i>count)	return range_error;
	node *p=head->next;
	int j=1;
	while(p!=NULL && j!=i){
		p=p->next;
		j++;
	}
	x=p->data;
	return success;
}*/

void list::create() {
	string s, temp;
	int flag = 1;
	cin >> s;
	if (s[0] != '-') s = "+" + s;
	int n, m, j = 0;
	n = s.find('+', 1);
	m = s.find('-', 1);
	while (n != -1 || m != -1) {	//还有+-号 

		if (n != -1 && m != -1)
			if (n < m)
				j = n;
			else
				j = m;
		else if (n != -1)
			j = n;
		else
			j = m;

		temp = s.substr(0, j);
		dataplus(temp);
		s = s.substr(j);

		n = s.find('+', 1);
		m = s.find('-', 1);
	}
	dataplus(s);
	return;
}

int list::stoi(string s) {
	if (s == "+")
		return 1;
	else if (s == "-")
		return -1;

	int i = 1, n = 0;
	if (isdigit(s[0])) i = 0;

	while (i < s.size())
		n = n * 10 + (s[i++] - '0');

	if (s[0] == '-') n = -1 * n;
	return n;
}

void list::dataplus(string s) {
	count++;
	node* n = new node;
	if (s.find('x') == -1)		//0 阶 
	{
		n->weight = 0;
		n->value = stoi(s);
	}
	else if (s.find("x^") == -1)	//1阶 
	{
		n->weight = 1;
		n->value = stoi(s.substr(0, s.size() - 1));
	}
	else
	{
		int i = s.find("x^");
		n->weight = stoi(s.substr(i + 2));
		n->value = stoi(s.substr(0, i));
	}
	rear->next = n;
	rear = n;
	rear->next = head;
	return;
}

void list::print() {
	node* temp = head->next;
	for (int i = 0;i < count;i++) {

		if (temp->weight == 0)
			if (temp->value < 0 || i == 0)
				cout << temp->value;
			else
				cout << "+" << temp->value;
		else if (temp->weight == 1)
			if (temp->value < 0 || i == 0)
				cout << temp->value << "x";
			else
				cout << "+" << temp->value << "x";
		else
			if (temp->value < 0 || i == 0)
				cout << temp->value << "x^" << temp->weight;
			else if(temp->value==1)
				cout << "+"<< "x^" << temp->weight;
			else
				cout << "+" << temp->value << "x^" << temp->weight;
		temp = temp->next;
	}
	cout<<endl;
}

void list::add_R(node* n) {	//增添在末尾,正序 
	rear->next = n;
	rear = n;
	rear->next = head;
	count++;
	return;
}
void list::add_H(node* n) {	//倒序 
	if (count == 0)
	{
		head->next = n;
		rear = n;
		rear->next = head;
	}
	else
	{
		n->next = head->next;
		head->next = n;
	}
	count++;
	return;
}

void add_up(list A, list B, list& C, list& D) {
	node* ra, * rb, * a, * b;
	a = A.get_head();
	b = B.get_head();
	ra = a->next;
	rb = b->next;
	while (ra != a && rb != b) {
		node* n = new node;
		node *m = new node;
		if (ra->weight < rb->weight)
		{
			*n = *ra;
			*m = *n;
			C.add_R(n);
			D.add_H(m);
			ra = ra->next;
		}
		else if (ra->weight == rb->weight)
		{
			if (ra->value + rb->value != 0)
			{
				n->weight = ra->weight;
				n->value = ra->value + rb->value;
				*m = *n;
				C.add_R(n);
				D.add_H(m);
			}
			ra = ra->next;
			rb = rb->next;
		}
		else
		{
			*n = *rb;
			*m = *n;
			C.add_R(n);
			D.add_H(m);
			rb = rb->next;
		}
	}
	
	while(ra != a){
		node* n = new node;
		node *m = new node;		
		*n = *ra;
		*m = *n;
		C.add_R(n);
		D.add_H(m);
		ra = ra->next;		
	}
	while(rb != b){
		node* n = new node;
		node *m = new node;			
		*n = *rb;
		*m = *n;
		C.add_R(n);
		D.add_H(m);
		rb = rb->next;		
	}
	return;
}

int main() {
	list A, B, C, D;
	A.create();
	B.create();

	add_up(A, B, C, D);
	C.print();
	D.print();

	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shuo..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值