多项式的运算)请编写程序实现多项式的加法、减法、乘法的运算。
【输入】第一行运算符c(c可能为+,-, *)
第二行,运算符c的第一个多项式。多项式每一项的系数和指数,且按照指数的降序排列,输入0 0表示结束。
第三行,运算符c的第二个多项式。多项式每一项的系数和指数,且按照指数的降序排列,输入0 0表示结束。
其中,输入多项式每一项的系数与指数均为整数,指数非负且系数不为0。
【输出】多项式运算后的结果。用^表示指数项,例如5X2的正确输出为5X^2。
例如:
【输入】
+
3 5 6 2 12 0 0 0 //表示:3X^5+6X^2+12
7 2 1 1 0 0 //表示:7X^2+X
【输出】
3X^5+13X^2+X+12
【输入】
+
7 14 2 8 -10 6 1 0 0 0 //表示:7X^14+2X^8-10X^6+1
4 18 8 14 -3 10 10 6 -1 4 0 0 //表示:4X^18+8X^14-3X^10+10X^6-X^4
【输出】
4X^18+15X^14-3X^10+2X^8-X^4+1
以下代码正确(之前print函数里漏考虑了一种特殊情况)
#include <iostream>
using namespace std;
enum Error_code { success, overflow, underflow };
struct Term {
int degree;//指数
int coefficient;//系数
Term(int exponent = 0, int scalar = 0);//构造函数
};
Term::Term(int exponent, int scalar)
{
degree = exponent;
coefficient = scalar;
}
typedef Term Node_entry;//可改队列内元素的数据类型
struct Node {
//成员
Node_entry entry;
Node* next;
//构造函数
Node();
Node(Node_entry item, Node* add_on = NULL);
};
Node::Node()
{
next = NULL;
}
Node::Node(Node_entry item, Node* add_on)
{
entry = item;
next = add_on;
}
class Polynomial {
public:
//多项式
void read();
void print()const;
int degree() const;
void add(Polynomial p, Polynomial q);//+
void sub(Polynomial p, Polynomial q);//-
void mul(Polynomial p, Polynomial q);//*
Polynomial();//构造函数
//队列
bool empty()const;
void clear();
Error_code append(const Node_entry& item);//入队
Error_code serve();//出队
Node_entry retrieve()const;//返回队首
//safety函数
~Polynomial();//析构函数
Polynomial(const Polynomial& original);//拷贝构造函数
void operator=(const Polynomial& original);//操作符重载(深拷贝)
protected:
Node* front, * rear;//队首,队尾
};
Polynomial::Polynomial()//构造函数
{
front = rear = NULL;
}
bool Polynomial::empty()const
{
return front == NULL;
}
void Polynomial::clear()
{while(!empty())
{
serve();
}
}
Error_code Polynomial::append(const Node_entry& item)//入队--从队尾
{
Node* new_rear = new Node(item);
if (new_rear == NULL)//空间分配失败--队满
return overflow;
if (rear == NULL)//空队列--front rear都要改
//空队列时入队 (0个->1个) 该元素既是队首 也是队尾
front = rear = new_rear;
else
{
rear->next = new_rear;
rear = rear->next;
}
return success;
}
Error_code Polynomial::serve()//出队--从队首
{
if (front == NULL)//队空
return underflow;
Node* old_front = front;
front = front->next;
if (front == NULL)//只有一个元素 (1个->0个)-->rear也要改
rear = NULL;
delete old_front;//释放空间
return success;
}
Node_entry Polynomial::retrieve()const//访问队首--返回的是一个结构体
{
if (front != NULL)//队非空
return front->entry;
}
Polynomial::~Polynomial()//析构函数:释放在堆内存中的数据
{
while (!empty())
serve();
}
void Polynomial::operator=(const Polynomial& original)//操作符重载(深拷贝)
{
if (this == &original)//如果两个相等,什么都不做(不加的话这个栈会被清空)
return;
while (!empty())
serve();//清空不要的链表s1
Node* new_copy, * original_node = original.front;
if (original_node == NULL)//s2为空--front rear都要改
front = rear = NULL;
else //拷贝
{
new_copy = front = new Node(original_node->entry);
while (original_node->next != NULL)
{
original_node = original_node->next;
new_copy->next = new Node(original_node->entry);
new_copy = new_copy->next;
}
rear = new_copy;
}
}
Polynomial::Polynomial(const Polynomial& original)//拷贝构造函数
{
Node* new_copy, * original_node = original.front;
if (original_node == NULL)//s1为空
front = rear = NULL;
else //拷贝
{
new_copy = front = new Node(original_node->entry);
while (original_node->next != NULL)
{
original_node = original_node->next;
new_copy->next = new Node(original_node->entry);
new_copy = new_copy->next;
}
rear = new_copy;
}
}
void Polynomial::read()
{
int degree = 0;
int coeffient = 0;
cin >> coeffient;
cin >> degree;
while (coeffient != 0 || degree != 0)
{
Term new_term(degree, coeffient);
append(new_term);
cin >> coeffient;
cin >> degree;
}
}
void Polynomial::print()const//不能改Poly-->用指针
{
Node* print_node = front;
bool first_term = true;
while (print_node != NULL) {
Term& print_term = print_node->entry;
if (first_term) {//第一项前不加+
first_term = false;
}
else if (print_term.coefficient > 0) cout << "+";
if (print_term.coefficient != 1 && print_term.coefficient != -1)
cout << print_term.coefficient;
if (print_term.coefficient == -1 && print_term.degree > 0)
cout << "-";
if (print_term.degree > 1) cout << "X^" << print_term.degree;
if (print_term.degree == 1) cout << "X";
if ((print_term.coefficient == 1 || print_term.coefficient == -1) && print_term.degree == 0)
cout << print_term.coefficient;
print_node = print_node->next;
}
if (first_term)
cout << "0"; //空
}
int Polynomial::degree() const
{
if (empty())//空--使指数=-1
return -1;
else//否则返回真正的指数
return retrieve().degree;
}
void Polynomial::add(Polynomial p, Polynomial q)//+
//传值 不会改变p,q
{
while (!p.empty()||!q.empty())//直到p,q都为空
{if(p.degree()>q.degree())
{
append(p.retrieve());
p.serve();
}
else if (p.degree() < q.degree())
{
append(q.retrieve());
q.serve();
}
else//p.degree() == q.degree()
{
//p,q系数相加=0-->直接p,q出队,不用放入r中
if (p.retrieve().coefficient + q.retrieve().coefficient != 0)
{
Term answer_term(p.degree(), p.retrieve().coefficient + q.retrieve().coefficient);
append(answer_term);
}
p.serve();
q.serve();
}
}
}
void Polynomial::sub(Polynomial p, Polynomial q)//-
{
//s=-q -->p-q=p+s
Polynomial s;
while (!q.empty())
{
Term s_term(q.retrieve().degree, -q.retrieve().coefficient);
s.append(s_term);
q.serve();
}
add(p, s);
}
void Polynomial::mul(Polynomial p, Polynomial q)//*
{
Polynomial s,t,trans;
while (!p.empty())
{
s.clear();
if (p.retrieve().coefficient != 0)
{
Polynomial qq = q;
while (!qq.empty())
{
if (qq.retrieve().coefficient != 0)
{
Term new_term(p.retrieve().degree + qq.retrieve().degree,
p.retrieve().coefficient * qq.retrieve().coefficient);
s.append(new_term);
}
qq.serve();
}
}
p.serve();
trans.clear();
//因为add是直接往trans里加(入队)t+s的结果
//而trans里还残余着之前之前一次循环的结果
//我们只需要新的结果(t+s) 所以要把trans中残留的clear掉
trans.add(t, s);
t = trans;
}
Polynomial null_poly;
add(t, null_poly);
}
int main()
{
Polynomial p,q,r;
char key;
cin >> key;
p.read();
q.read();
switch (key)
{
case '+':
r.add(p, q);
break;
case '-':
r.sub(p, q);
break;
case '*':
r.mul(p, q);
break;
}
r.print();
return 0;
}