源代码:
// 2_e1.cpp -- 一元多项式的相加
/*
* -> 程序要求:
* 1. 写出两个一元多项式相加的算法。
* 2. 用链表来存储一元多项式,并且要在程序中验证其功能实现。
* 3. 此题的源程序保存为2_e1.cpp
*/
/*
* -> 程序分析:
* 1. 一元多项式相加的基本思想:若两项的指数相等,则系数相加,若两项的指数不等,则将两项加在结果中。
* 2. 本程序决定将结果相加至一个新的链表中,即相加的时候使用return函数,return一个链表,再将该链表通过this指针复制入新的对象。
*/
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
class PloyNode
{
public:
double coef; // 系数
signed expn; // 指数
PloyNode *next; // 下一指针
PloyNode(double c, signed e, PloyNode * p = NULL):coef(c), expn(e), next(p){}
PloyNode(PloyNode* p = NULL):next(p){}
};
class Ploy: public PloyNode
{
private:
// 指针
PloyNode * head; // 头指针
PloyNode * tail; // 尾指针
PloyNode * fence; // 阑栅
void init()
{
head = new PloyNode;
tail = fence = head;
return;
}
void removeall()
{
while(head != NULL)
{
fence = head;
head = head -> next;
delete fence;
}
return;
}
public:
// 构造
Ploy(){init();}
Ploy(const Ploy &rhs);
// 析构
~Ploy()
{
removeall();
}
// 各功能
bool inserthelp(const double & c, const signed & e);
void printall();
friend Ploy operator + (const Ploy & first, const Ploy & second);
};
//
bool Ploy::inserthelp(const double & c, const signed & e)
{
fence = head;
if ( head == tail) // 当该单项式为空时,直接在底部插入
{
if(!(fence -> next = tail = new PloyNode(c, e, tail -> next)))
return false;
return true;
}
PloyNode * temp = fence; // 用于记录本次指针
while ( fence -> next != NULL)
{
if (fence -> next -> expn == e) // 第一种情况,指数相同,搜索出是否有相同次数的指数
{
fence -> next -> coef += c; // 若搜出相同次数的指数,则系数相加
return true;
}
if (fence -> next -> expn < e) // 按指数大小排列单项式中的各项(相当于插入排序原理)
{
temp = fence -> next;
}
if (fence -> next -> expn > e)
{
break;
}
fence = fence -> next;
}
if (fence -> next == NULL) // 插入时,未能找到比它大的指数项,直接到尾部插入
{
tail -> next = new PloyNode(c, e, tail -> next);
tail = tail ->next;//<------------------------加上它
return true;
}
else // 指数大小在中间,在该结点的后面插入。
{
fence = temp;
fence -> next = new PloyNode(c, e , fence -> next);
return true;
}
}
void Ploy::printall() // 显示功能
{
for (fence = head; fence -> next != NULL; fence = fence -> next)
{
cout << fence -> next -> coef << "X(" << fence -> next -> expn << ")";
if (fence -> next -> next != NULL)
cout << " + ";
}
}
Ploy operator + (const Ploy &first, const Ploy &second)
{
Ploy third;
PloyNode * temp;
temp = first.head;
while (temp -> next != NULL)
{
third.inserthelp(temp -> next -> coef, temp -> next -> expn);
temp = temp -> next;
}
temp = second.head;
while (temp -> next != NULL)
{
third.inserthelp(temp -> next -> coef, temp -> next -> expn);
temp = temp -> next;
}
return third;
}
Ploy::Ploy(const Ploy &rhs)
{
// *this = rhs;
/*
PloyNode * temp = rhs.head;
while (temp -> next != NULL)
{
inserthelp(temp -> next -> coef, temp -> next -> expn);
temp = temp -> next;
}
*/
init(); // <- 复制时千万不要忘了这一步,现在是相当于新建一个对象,要构造
PloyNode * temp = rhs.head;
while (temp -> next != NULL)
{
(*this).inserthelp(temp -> next -> coef, temp -> next -> expn);
temp = temp -> next;
}
}
//
int main()
{
cout << "-> 现在开始一元多项式相加测试,先插入元素进第一个单项式:\n ";
Ploy test;
// 项数相同的插入
test.inserthelp(1,2);
test.inserthelp(2,2);
// 结果应为 3, 2
// 项数不同的插入
test.inserthelp(5,6);
test.inserthelp(6,1);
test.inserthelp(2,0);
test.printall();
cout << "\n-> 现在开始一元多项式相加测试,插入元素进第二单项式:\n ";
Ploy test2;
// 项数相同的插入
test2.inserthelp(1,2);
test2.inserthelp(2,2);
// 结果应为 3, 2
// 项数不同的插入
test2.inserthelp(5,1);
test2.inserthelp(6,7);
test2.inserthelp(2,3);
test2.printall();
cout << endl;
// 两项相加
cout << "\n-> 以下是一元多项式的相加的结果:\n";
Ploy test3( test + test2 );
test3.printall();
cout << endl << endl;
system("pause");
return 0;
}
运行结果:
-> 现在开始一元多项式相加测试,先插入元素进第一个单项式:
2X(0) + 6X(1) + 3X(2) + 5X(6)
-> 现在开始一元多项式相加测试,插入元素进第二单项式:
5X(1) + 3X(2) + 2X(3) + 6X(7)
-> 以下是一元多项式的相加的结果:
2X(0) + 11X(1) + 6X(2) + 2X(3) + 5X(6) + 6X(7)
请按任意键继续. . .