数据结构——链表实现两个多项式加减乘除

任务概述:

使用链表实现两个多项式的基本操作。初级要求:实现加法、减法和微分操作。高级要求:实现乘法和除法操作。(除法操作中,当不能除尽时还要求列出余式。)

要点:

    链表中的每个结点形式如下:

                                                          

链表中,各结点按指数升幂或降幂排列,生成的结果多项式仍使用原多项式所占用的存储空间:两个同类项相加或相减时,如果系数不为0,则修改该结点的系数值,而指数值不变,将该结点链入结果多项式中,而另一项则释放空间;如果系数为0,则这两项占用的空间均需释放。不同类项则直接放入结果多项式中。

 

输入数据格式:

使用文件保存初始的多项式数据,文件名为inputfile.txt,文件中应含有表示两个多项式的初始数据。文件中的数据格式为:

(a,n)…(a,n)(0,0)

(a,n)…(a,n)(0,0)

AND/SUB/DIFF/MUL/DIV(#,#)

其中,a是系数,n是指数,两数之间使用逗号分隔,并用括号括起来。其后以(0,0)作为一个多项式输入的结束。以字符串AND表示加法操作,以字符串SUB表示减法操作,以字符串DIFF表示微分操作,以字符串MUL表示乘法操作,以字符串DIV表示除法操作,以(#,#)作为全部数据输入的结束标志。

 

输出数据格式:

输出结果也保存在文件中,文件名为outputfile.txt,文件中应含有结果多项式的值,以多项式的形式输出结果,例如如下所示的多项式:

2x3 –5x2 + x1 +9

在输出文件中表示为:2x3 –5x2 + x1 +9

 

输入数据:

以下面示例中的数据作为输入数据,将所得结果写入输出文件中

测试示例一:

(5,100)(9,31)(-7,2)(1,1)(10,0)(0,0)

(-60,81)(-9,31)(5,2)(1,1)(0,0)

AND(#,#)

测试示例二:

(5,100)(9,31)(-7,2)(1,1)(10,0)(0,0)

(-60,81)(-9,31)(5,2)(1,1)(0,0)

SUB(#,#)

测试示例三:

(5,100)(9,31)(-7,2)(1,1)(10,0)(0,0)

(-60,81)(-9,31)(5,2)(1,1)(0,0)

MUL(#,#)

 

代码如下(vs2012下):

#include
#include
using namespace std;
class  node
{
public:
	float coef;//系数  
	int exp;//指数  
	node *next;
	//构造函数
	node(float a, int b, node* c = NULL)
	{
		coef = a;
		exp = b;
		next = c;
	}
	node(node* c = NULL)
	{
		next = c;
	}
	//析构函数
	~node() {};
};
//多项式类
class polynomia
{
public:
	node* head;
	node* curr;
	//构造函数
	polynomia()
	{
		head = curr = new node;
		head->coef = NULL;
		head->exp = -1;
		head->next = NULL;
		curr = head;
	}	//插入元素
	void insert(node *a)
	{
		node *h = new node();
		h->coef = a->coef;
		h->exp = a->exp;
		h->next = curr->next;
		curr->next = h;
		curr = h;
	}
	void clear()
	{
		node *ctem;
		while (head->next != NULL)
		{
			ctem = head->next;
			head->next = ctem->next;
			delete ctem;
		}
	}
	//析构函数
	~polynomia() {}
};
//加法实现
void ADD(polynomia &i, polynomia &j)
{
	node* p, *q, *u, *pre;
	p = i.head->next;
	q = j.head->next;
	pre = i.head;
	while ((p != NULL) && (q != NULL))
	{
		if (p->exp > q->exp)
		{
			pre = p;
			p = p->next;
		}
		else if (p->exp == q->exp)
		{
			float x;
			x = p->coef + q->coef;
			if (x != 0)
			{
				p->coef = x;
				pre = p;
			}
			else
			{
				pre->next = p->next;
				free(p);
			}
			p = pre->next;
			u = q;
			q = q->next;
			free(u);
		}
		else if (p->exp exp)
		{
			u = q->next;
			q->next = p;
			pre->next = q;
			pre = q;
			q = u;
		}
	}
	if (q != NULL)
	{
		pre->next = q;
	}
}
//减法实现
void SUB(polynomia &i, polynomia &j)
{
	node *p, *q, *u, *pre;
	p = i.head->next;
	q = j.head->next;
	pre = i.head;
	while ((p != NULL) && (q != NULL))
	{
		if (p->exp > q->exp)
		{
			pre = p;
			p = p->next;
		}
		else if (p->exp == q->exp)
		{
			float x;
			x = p->coef - q->coef;
			if (x != 0)
			{
				p->coef = x;
				pre = p;
			}
			else
			{
				pre->next = p->next;
				free(p);
			}
			p = pre->next;
			u = q;
			q = q->next;
			free(u);
		}
		else if (p->exp < q->exp)
		{
			q->coef = -q->coef;
			u = q->next;
			q->next = p;
			pre->next = q;
			pre = q;
			q = u;
		}
	}
	if (q != NULL)
	{
		pre->next = q;
		pre->next->coef = -(q->coef);
	}
}
//乘法实现
void MUL(polynomia &i, polynomia &j)
{
	polynomia ans;
	node *p, *q, *u;
	p = i.head->next;
	int flag = 0;
	while (p != NULL)
	{
		polynomia tem;
		q = j.head->next;
		while (q != NULL)
		{
			u = new node();
			u->coef = p->coef*q->coef;
			u->exp = p->exp + q->exp;
			tem.insert(u);
			q = q->next;
			delete u;
		}
		if (flag == 0)
		{
			ans = tem;
			flag = 1;
		}
		else
		{
			ADD(ans, tem);
		}
		p = p->next;
	}
	i = ans;
}
//除法实现
void DIV(polynomia &i, polynomia &j, polynomia &k)
{
	node *p, *q, *dtem = new node(), *dtemp = new node();
	polynomia ans;
	p = i.head->next;
	q = j.head->next;
	while (p != NULL&&q != NULL)
	{
		polynomia tem;
		if (p->exp < q->exp)
		{
			k = ans;
			break;
		}
		else
		{
			dtem->coef = p->coef / q->coef;
			dtem->exp = p->exp - q->exp;
			ans.insert(dtem);
		}
		while (q != NULL)
		{
			dtemp->coef = dtem->coef*q->coef;
			dtemp->exp = dtem->exp + q->exp;
			tem.insert(dtemp);
			q = q->next;
		}
		q = j.head->next;
		SUB(i, tem);
		p = i.head->next;
	}
}
int main()
{
	float x;
	int y;
	char *oper = new char(), a, b, c;
	ifstream infile1("inputfile.txt");
	infile1 >> a >> x >> b >> y >> c;
	polynomia dong, wen;
	while (x != 0)
	{
		node *li = new node(x, y);
		dong.insert(li);
		delete li;
		infile1 >> a >> x >> b >> y >> c;
	}
	infile1 >> a >> x >> b >> y >> c;
	while (x != 0)
	{
		node *li = new node(x, y);
		wen.insert(li);
		delete li;
		infile1 >> a >> x >> b >> y >> c;
	}
	infile1 >> oper;
	oper[strlen(oper)] = '\0';
	infile1.close();
	if (!strcmp(oper, "AND(#,#)"))
	{
		ADD(dong, wen);
		ofstream out("outputfile.txt");
		char x = 'x';
		node* tem;
		tem = dong.head;
		while (tem->next != NULL)
		{
			if (tem->next->exp != 0)
			{
				out << tem->next->coef << "x" << tem->next->exp;
			}
			else
			{
				out << tem->next->coef;
			}
			while (tem->next->next != NULL)
			{
				if (tem->next->next->coef > 0)
					out << " +";
				break;
			}
			tem = tem->next;
		};
		out.close();
		return 0;
	}
	if (!strcmp(oper, "SUB(#,#)"))
	{
		SUB(dong, wen);
		ofstream out("outputfile.txt");
		char x = 'x';
		node* tem;
		tem = dong.head;
		while (tem->next != NULL)
		{
			if (tem->next->exp != 0)
			{
				out << tem->next->coef << "x" << tem->next->exp;
			}
			else
			{
				out << tem->next->coef;
			}
			while (tem->next->next != NULL)
			{
				if (tem->next->next->coef > 0)
					out << " +";
				break;
			}
			tem = tem->next;
		};
		out.close();
		return 0;
	}
	if (!strcmp(oper, "MUL(#,#)"))
	{
		MUL(dong, wen);
		ofstream out("outputfile.txt");
		node* tem;
		tem = dong.head;
		while (tem->next != NULL)
		{
			if (tem->next->exp != 0 && tem->next->coef != 0)
			{
				out << tem->next->coef << "x" << tem->next->exp;
			}
			else if (tem->next->coef != 0)
			{
				out << tem->next->coef;
			}
			while (tem->next->next != NULL)
			{
				if (tem->next->next->coef > 0)
					out << " +";
				break;
			}
			tem = tem->next;
		}
		out.close();
		return 0;
	}

	if (!strcmp(oper, "DIV(#,#)"))
	{
		polynomia cur;
		DIV(dong, wen, cur);
		ofstream out("outputfile.txt");
		out << "结果是:";
		node* tem;
		tem = cur.head;
		while (tem->next != NULL)
		{
			if (tem->next->exp != 0 && tem->next->coef != 0)
			{
				out << tem->next->coef << "x" << tem->next->exp;
			}
			else if (tem->next->coef != 0)
			{
				out << tem->next->coef;
			}
			while (tem->next->next != NULL)
			{
				if (tem->next->next->coef > 0)
					out << " +";
				break;
			}
			tem = tem->next;
		}
		out << endl << "余项是:";
		tem = dong.head;
		while (tem->next != NULL)
		{
			if (tem->next->exp != 0 && tem->next->coef != 0)
			{
				out << tem->next->coef << "x" << tem->next->exp;
			}
			else if (tem->next->coef != 0)
			{
				out << tem->next->coef;
			}
			while (tem->next->next != NULL)
			{
				if (tem->next->next->coef > 0)
					out << " +";
				break;
			}
			tem = tem->next;
		}
		out.close();

		return 0;
	}
	ofstream out("outputfile.txt");
	out << "输入有误";
	out.close();
	return 0;
	system("pause");
}

 

你可能感兴趣的:(breadcrumb,Play,Me,——,Algorithm,and,Structure)