//每行第一个数表示多项式项数,后面表示系数和指数。data.txt 内容如下:
//4 3 4 -5 2 6 1 -2 0
//3 5 20 -7 4 3 1
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define useIostream 0
struct ListNode
{
double coef;
int exp;
ListNode *next;
ListNode() :coef(0), exp(1), next(nullptr) { }
ListNode(double c, int e) :coef(c), exp(e), next(nullptr) { }
};
class Poly
{
public:
Poly() { head = new ListNode; tail = head; name = ""; }
Poly(string s) { head = new ListNode; tail = head; name = s; cout << "--------construct: " << name << "--------------" << endl;}
Poly(Poly &p); //复制构造函数,深复制
~Poly();
void insertTerm(double c, int e); //末尾插入
void displayPoly();
void mergePoly(); //合并同类项
void sortPoly(); //降阶排序
Poly operator* (Poly &p2);
Poly* operator+ (Poly &p2);
private:
ListNode *head;
ListNode *tail;
string name;
};
//复制构造函数 深复制
Poly::Poly(Poly &p)
{
if (p.head == nullptr)
return;
head = new ListNode;
tail = head;
name = "copy of " + p.name;
ListNode *cur = p.head->next;
while (cur)
{
insertTerm(cur->coef, cur->exp);
cur = cur->next;
}
cout << "--------construct: " << name << "--------------" << endl;
}
Poly::~Poly()
{
ListNode *cur = head;
ListNode *next = nullptr;
while (cur)
{
next = cur->next;
delete cur;
cur = next;
}
cout << "--------deconstruct: " << name << "--------------" << endl;
}
void Poly::insertTerm(double coef, int exp)
{
ListNode *node = new ListNode(coef, exp);
tail->next = node;
tail = tail->next;
}
void Poly::displayPoly()
{
if (head == nullptr)
return;
ListNode *cur = head->next;
cout << name << " = ";
while (cur)
{
cout << cur->coef << "x^" << cur->exp;
if (cur->next != nullptr)
cout << " + ";
cur = cur->next;
}
cout << endl << endl;
}
void Poly::mergePoly()
{
if (head == nullptr)
return;
ListNode *cur1 = head->next;
ListNode *cur2 = nullptr;
ListNode *pre1 = head;
ListNode *pre2 = nullptr;
while (cur1)
{
ListNode *tmp = nullptr;
pre2 = cur1;
cur2 = cur1->next;
while (cur2)
{
if (cur1->exp == cur2->exp) //需要删除cur2, pre2不变
{
cur1->coef += cur2->coef;
cur2->coef = 0;
cur2->exp = 1; //防止出现0的0次方
pre2->next = cur2->next;
tmp = cur2;
}
else
pre2 = cur2;
cur2 = cur2->next;
if (tmp != nullptr) { delete tmp; tmp = nullptr; }
}
if (cur1->coef == 0) //需要删除cur1, pre1不用变
{
pre1->next = cur1->next;
tmp = cur1;
}
else
pre1 = cur1;
cur1 = cur1->next;
if (tmp != nullptr) { delete tmp; tmp = nullptr; }
}
//或者:最后重新遍历一遍删除系数为0的节点
}
void Poly::sortPoly(void)
{
if (head == nullptr)
return;
ListNode *n1 = head->next, *n2 = nullptr;
while (n1)
{
n2 = n1->next; //不能遗漏
while (n2)
{
if (n1->exp < n2->exp) //交换系数和指数
{
swap(n1->coef, n2->coef);
swap(n1->exp, n2->exp);
}
n2 = n2->next;
}
n1 = n1->next;
}
}
Poly* Poly::operator+ (Poly &p2)
{
/*-------------------方法1 需要先对两个实参排序,返回值不用排序--------------------------------*/
if (this->head == nullptr || p2.head == nullptr)
return nullptr;
Poly *sum = new Poly("sum");
ListNode *cur1 = this->head->next;
ListNode *cur2 = p2.head->next;
this->sortPoly();
p2.sortPoly();
while (cur1 && cur2)
{
if (cur1->exp > cur2->exp)
{
sum->insertTerm(cur1->coef, cur1->exp);
cur1 = cur1->next;
}
else if (cur1->exp < cur2->exp)
{
sum->insertTerm(cur2->coef, cur2->exp);
cur2 = cur2->next;
}
else
{
int c = cur1->coef + cur2->coef;
if (c != 0)
{
sum->insertTerm(c, cur1->exp);
}
cur1 = cur1->next;
cur2 = cur2->next;
}
}
while (cur1)
{
sum->insertTerm(cur1->coef, cur1->exp);
cur1 = cur1->next;
}
while (cur2)
{
sum->insertTerm(cur2->coef, cur2->exp);
cur2 = cur2->next;
}
return sum; //函数返回后最终需要delete指针
/*-------------------方法2 把p2和this拼接到一起,然后合并同类型、排序--------------------------------*/
//Poly *sum = new Poly("sum");
//ListNode *cur1 = this->head->next, *cur2 = p2.head->next;
//while (cur1)
//{
// sum->insertTerm(cur1->coef, cur1->exp);
// cur1 = cur1->next;
//}
//while (cur2)
//{
// sum->insertTerm(cur2->coef, cur2->exp);
// cur2 = cur2->next;
//}
//sum->mergePoly();
//sum->sortPoly();
//return sum; //函数返回后最终需要delete指针
}
//返回值不能使用引用,因为调用结束后product会消亡
Poly Poly::operator* (Poly &p2)
{
Poly product("product");
ListNode *cur1 = this->head->next;
ListNode *cur2 = p2.head->next;
while (cur1)
{
while (cur2)
{
product.insertTerm(cur1->coef * cur2->coef, cur1->exp + cur2->exp);
cur2 = cur2->next;
}
cur1 = cur1->next;
cur2 = p2.head->next;
}
product.mergePoly();
product.sortPoly();
return product; //返回时,调用复制构造函数,然后product消亡
}
int main()
{
Poly poly1("poly1"), poly2("poly2");
int length1 = 0;
int length2 = 0;
//------------输入多项式1-----------------------
#if useIostream
cin >> length1;
#else
fstream f("data.txt");
f >> length1;
#endif
for (int i = 0; i < length1; i++)
{
double coef = 0, exp = 1;
#if useIostream
cin >> coef >> exp;
#else
f >> coef >> exp;
#endif
poly1.insertTerm(coef, exp);
}
//------------输入多项式2-----------------------
#if useIostream
cin >> length2;
#else
f >> length2;
#endif
for (int i = 0; i < length2; i++)
{
double coef = 0, exp = 1;
#if useIostream
cin >> coef >> exp;
#else
f >> coef >> exp;
#endif
poly2.insertTerm(coef, exp);
}
poly1.displayPoly();
poly2.displayPoly();
Poly product = poly1 * poly2; //为保证正常返回,需要定义复制构造函数
product.displayPoly();
Poly *sum = poly1 + poly2; //返回对象指针,不需要使用复制构造函数,但需要delete指针
sum->displayPoly();
delete sum;
system("pause");
return 0;
}
DS-链表实现多项式运算
最新推荐文章于 2023-04-03 19:06:57 发布