您的位置:首页 > 编程语言 > C语言/C++

通过链表实现的多项式乘法【C++】

2014-10-31 04:41 344 查看
被某喵发给我让我看的代码丑到哭。。。。就随手再写一个吧。。。

话说最近一直在用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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 链表
相关文章推荐