c++实现输入两个稀疏一元多项式表达式,要求是指数有序的规范形式,然后对两个表达式求解,写出完整代码
时间: 2023-10-07 21:09:45 浏览: 93
下面是一个基于链表的 C++ 实现,可以输入两个稀疏一元多项式表达式,对它们进行相加或相乘操作:
```cpp
#include <iostream>
using namespace std;
// 定义节点结构体
struct Node
{
int coefficient; // 系数
int exponent; // 指数
Node* next; // 指向下一个节点的指针
};
// 定义链表类
class Polynomial
{
public:
Polynomial(); // 构造函数
~Polynomial(); // 析构函数
void input(); // 输入多项式
void output(); // 输出多项式
friend Polynomial operator+(const Polynomial& a, const Polynomial& b); // 多项式相加
friend Polynomial operator*(const Polynomial& a, const Polynomial& b); // 多项式相乘
private:
Node* head; // 头节点指针
};
Polynomial::Polynomial()
{
head = new Node(); // 头节点不存储数据
head->next = nullptr;
}
Polynomial::~Polynomial()
{
Node* p = head->next;
while (p)
{
Node* q = p;
p = p->next;
delete q;
}
delete head;
}
void Polynomial::input()
{
int n;
cout << "请输入多项式项数:";
cin >> n;
Node* p = head;
for (int i = 0; i < n; ++i)
{
int c, e;
cout << "请输入第" << i + 1 << "项系数和指数:";
cin >> c >> e;
// 查找插入位置
while (p->next && p->next->exponent > e)
{
p = p->next;
}
// 如果指数相同,则系数相加
if (p->next && p->next->exponent == e)
{
p->next->coefficient += c;
}
else
{
Node* q = new Node();
q->coefficient = c;
q->exponent = e;
q->next = p->next;
p->next = q;
}
}
}
void Polynomial::output()
{
Node* p = head->next;
while (p)
{
cout << p->coefficient << "x^" << p->exponent;
if (p->next)
{
cout << " + ";
}
p = p->next;
}
cout << endl;
}
Polynomial operator+(const Polynomial& a, const Polynomial& b)
{
Polynomial c;
Node* p = a.head->next;
Node* q = b.head->next;
Node* r = c.head;
// 指数有序,从高到低逐项相加
while (p && q)
{
if (p->exponent > q->exponent)
{
Node* s = new Node();
s->coefficient = p->coefficient;
s->exponent = p->exponent;
r->next = s;
r = s;
p = p->next;
}
else if (p->exponent < q->exponent)
{
Node* s = new Node();
s->coefficient = q->coefficient;
s->exponent = q->exponent;
r->next = s;
r = s;
q = q->next;
}
else
{
Node* s = new Node();
s->coefficient = p->coefficient + q->coefficient;
s->exponent = p->exponent;
r->next = s;
r = s;
p = p->next;
q = q->next;
}
}
// 将未处理完的项插入到 c 中
while (p)
{
Node* s = new Node();
s->coefficient = p->coefficient;
s->exponent = p->exponent;
r->next = s;
r = s;
p = p->next;
}
while (q)
{
Node* s = new Node();
s->coefficient = q->coefficient;
s->exponent = q->exponent;
r->next = s;
r = s;
q = q->next;
}
return c;
}
Polynomial operator*(const Polynomial& a, const Polynomial& b)
{
Polynomial c;
// 逐项相乘
Node* p = a.head->next;
while (p)
{
Node* q = b.head->next;
while (q)
{
Node* r = new Node();
r->coefficient = p->coefficient * q->coefficient;
r->exponent = p->exponent + q->exponent;
// 从高到低插入
Node* s = c.head;
while (s->next && s->next->exponent > r->exponent)
{
s = s->next;
}
// 如果指数相同,则系数相加
if (s->next && s->next->exponent == r->exponent)
{
s->next->coefficient += r->coefficient;
}
else
{
r->next = s->next;
s->next = r;
}
q = q->next;
}
p = p->next;
}
return c;
}
int main()
{
Polynomial a, b;
cout << "请输入第一个多项式:" << endl;
a.input();
cout << "请输入第二个多项式:" << endl;
b.input();
cout << "第一个多项式为:";
a.output();
cout << "第二个多项式为:";
b.output();
Polynomial c = a + b;
cout << "它们的和为:";
c.output();
Polynomial d = a * b;
cout << "它们的积为:";
d.output();
return 0;
}
```
注意,这里使用了 operator+ 和 operator* 函数重载,可以直接对两个多项式进行相加和相乘操作。
阅读全文