通過鏈表實現的多項式乘法【C++】

被某喵發給我讓我看的代碼醜到哭。。。。就隨手再寫一個吧。。。

話說最近一直在用C#。突然換回C++好不爽。竟然有點嫌棄C++了。。。

廢話不多說。直接上代碼吧。基本沒有註釋。不過挺水的也挺好理解。

哦對。題目是這樣的。。。


#include<iostream>

using namespace std;

struct Term
{

	Term *Pre, *Next;
	int Exp;
	double Coef;

};

class polynomial
{
public:

	Term *Head,*Tail;

	polynomial();
	~polynomial();
	void Add(double Coef, int Exp);
	void Sort();
	void Merge();
	void inputPoly();
	void outputPoly();

	friend const polynomial operator*(const polynomial& polyA, const polynomial& polyB);

private:

};

polynomial::polynomial()
{

	Head = new Term;

	Head->Pre = nullptr;
	Head->Coef = 0;
	Head->Exp = 1;
	Head->Next = NULL;

	Tail = Head;

}

polynomial::~polynomial()
{
}

void polynomial::Add(double Coef, int Exp)
{

	Term *term = new Term;

	term->Pre = Tail;
	term->Coef = Coef;
	term->Exp = Exp;
	term->Next = NULL;
	Tail->Next = term;

	Tail = term;

}

void polynomial::inputPoly()
{

	double coef;
	int exp;
	cout << "請輸入多項式的係數和指數,當指數過小(小於-99)時將自動結束輸入" << endl;
	while (true)
	{
		cin >> coef >> exp;
		if (exp < -99)
		{
			break;
		}
		this->Add(coef, exp);
	}

	this->Merge();

}

void polynomial::outputPoly()
{

	Term *Current = Head;
	bool First = true;
	cout << "多項式爲:";

	while (Current->Next != NULL)
	{
		Current = Current->Next;

		if (First)
		{
			First = false;

			if (Current->Coef != 1)
			{
				if (Current->Exp == 0)
				{
					cout << "1";
				}
				else if (Current->Exp == 1)
				{
					cout << Current->Coef << "x";
				}
				else
				{
					cout << Current->Coef << "x^" << Current->Exp;
				}
			}
			else
			{
				if (Current->Exp == 0)
				{
					cout << "1";
				}
				else if (Current->Exp == 1)
				{
					cout << "x";
				}
				else
				{
					cout << "x^" << Current->Exp;
				}
			}
		}
		else
		{
			if (Current->Coef > 0)
			{
				if (Current->Coef != 1)
				{
					if (Current->Exp == 0)
					{
						cout << "+" << Current->Coef;
					}
					else if (Current->Exp == 1)
					{
						cout << "+" << Current->Coef << "x";
					}
					else
					{
						cout << "+" << Current->Coef << "x^" << Current->Exp;
					}
				}
				else
				{
					if (Current->Exp == 0)
					{
						cout << "+" << Current->Coef;
					}
					else if (Current->Exp == 1)
					{
						cout << "+" << "x";
					}
					else
					{
						cout << "+" << "x^" << Current->Exp;
					}
				}
			}
			else
			{
				if (Current->Coef != -1)
				{
					if (Current->Exp == 0)
					{
						cout << Current->Coef;
					}
					else if (Current->Exp == 1)
					{
						cout << Current->Coef << "x";
					}
					else
					{
						cout << Current->Coef << "x^" << Current->Exp;
					}
				}
				else
				{
					if (Current->Exp == 0)
					{
						cout << Current->Coef;
					}
					else if (Current->Exp == 1)
					{
						cout << "-x";
					}
					else
					{
						cout << "-x^" << Current->Exp;
					}
				}
			}

		}

	}

	cout << endl;

}

//按指數排序
void polynomial::Sort()
{

	
	double tempCoef;
	int tempExp;
	int ActionNum = 0;

	do
	{
		Term *Current = Head;
		ActionNum = 0;
		while (Current->Next != NULL)
		{

			Current = Current->Next;
			if (Current->Next == NULL)
			{
				break;
			}
			if (Current->Exp > Current->Next->Exp)
			{
				tempCoef = Current->Coef;
				Current->Coef = Current->Next->Coef;
				Current->Next->Coef = tempCoef;
				tempExp = Current->Exp;
				Current->Exp = Current->Next->Exp;
				Current->Next->Exp = tempExp;
				ActionNum++;
			}

		}
	} while (ActionNum > 0);

}

//合併多項式
void polynomial::Merge()
{

	this->Sort();

	Term *Current = Head;
	bool Action = false;

	while (Current->Next != NULL)
	{

		if (!Action)
		{
			Current = Current->Next;
		}

		Action = false;
		if (Current->Next == NULL)
		{
			Tail = Current;
			break;
		}

		if (Current->Exp == Current->Next->Exp)
		{

			Current->Coef = Current->Coef + Current->Next->Coef;
			if (Current->Next->Next == NULL)
			{
				Current->Next = NULL;
				Tail = Current;
				break;
			}
			else
			{
				Current->Next->Next->Pre = Current;
				Current->Next = Current->Next->Next;
			}

			Action = true;

			if (Current->Coef == 0)
			{
				
				Term *temp = Current->Next;
				Current->Next->Pre = Current->Pre;
				Current->Pre->Next = Current->Next;
				Current = temp;

			}

		}

		if (Current->Next == NULL)
		{
			Tail = Current;
		}

	}

}

//*運算符重載
polynomial const operator*(const polynomial& polyA, const polynomial& polyB)
{

	polynomial polyC;

	Term *CurrentA = polyA.Head;
	Term *CurrentB = polyB.Head;
	
	while (CurrentA->Next != NULL)
	{

		CurrentB = polyB.Head;
		CurrentA = CurrentA->Next;
		while (CurrentB->Next != NULL)
		{

			CurrentB = CurrentB->Next;
			polyC.Add(CurrentA->Coef * CurrentB->Coef, CurrentA->Exp + CurrentB->Exp);

		}

	}

	polyC.Merge();

	return polyC;

}

int main()
{

	polynomial ah,bh,ch;

	ah.inputPoly();
	ah.outputPoly();
	bh.inputPoly();
	bh.outputPoly();

	ch = ah * bh;
	ch.outputPoly();

	system("pause");
	return 0;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章