/*利用链表实现多项式的相乘以及相加
两个多项式
多项式1:3X^4 + 5X^2 +6X - 2
多项式2:5X^20 -7X^4 +3X
输入数据: 4 3 4 5 2 6 1 -2 0 输入数据对于每一个多项式而言,先指定多项式的项数,然后
····· 3 5 20 -7 4 3 1 输入每一项的系数与指数
输出数据:与上述类型一样,对多项式的系数与指数进行输出
程序的框架如下:
1、主函数
读入多项式1
读入多项式2
进行乘法计算
进行加法计算
2、读入多项式 4 3 4 -5 2 6 1 -2 0 3 5 20 -7 4 3 1
输入数据是由一个项数 + 系数加指数的形式出现,在读入数据的时候要注意结点的申请
3、加法计算
相同指数项进行合并操作
不同项将指数大的那一项进行连接
小的那一项保持不动
4、乘法计算
以一个多项式为基准,使得此多项式的每一个结点(项)对另一个多项式的每一项进行乘法计算
然后将其于最终的多项式进行加法计算
5、输出
*/
#include<iostream>
using namespace std;
class Node{ //定义多项式每一项的节点结构
public:
int exp;
int coefficient;
Node * next;
};
class polynomial{ //定义多项式类
public:
polynomial(); //构造函数--需要对头节点进行初始化,分配空间
~polynomial(){}; //析构函数--暂时不考虑
void print(); //将多项式以系数+指数的方式进行输出
void append(int coef,int expo); //将多项式进行连接--也就是链表的尾插工作
Node * getHead(); //返回头节点
Node * getTail(); //返回尾节点
private:
Node * head; //将头节点与尾节点声明为私有
Node * tail;
};
Node *polynomial::getHead(){
return head;
}
Node * polynomial::getTail(){
return tail;
}
polynomial::polynomial(){
head = new Node();
head->exp = NULL; //如何记录一个结点的初始状态? 此处有待商榷,会出现警告
tail = head;
}
void polynomial::print(){
Node * current = head; //注意将head节点进行相应的托管
while(current != NULL){
cout<<current->coefficient<<" "<<current->exp<<" ";
current = current->next;
}
cout<<endl;
}
void polynomial::append(int coef,int expo){
if(head->exp == NULL){ //判定是否是初始化状态
head->coefficient = coef;
head->exp = expo;
head->next =NULL;
tail = head;
}else{
Node *temp = new Node();
temp->coefficient = coef;
temp->exp = expo;
temp->next = NULL;
tail->next = temp;
tail = temp;
}
}
void initPoly(int n,polynomial poly){
while(n--){
int coef,exp;
cin>>coef>>exp;
poly.append(coef,exp);
}
}
polynomial add(polynomial poly1,polynomial poly2){
polynomial result;
Node * temp1 =poly1.getHead();
Node * temp2 =poly2.getHead();
while(temp1 != NULL && temp2 != NULL){
if(temp1->exp == temp2->exp){ //判定多项式中的前后关系
if(temp1->coefficient + temp2->coefficient == 0)
; //在合并同类项的时候如果出现了相互抵消的状况,也就是两者有一方的系数为0,部将其纳入结果中
else
result.append(temp1->coefficient + temp2->coefficient,
temp1->exp); //相同的进行同类项的合并
temp1 = temp1->next;
temp2 = temp2->next;
}
else if(temp1->exp > temp2->exp){
result.append(temp1->coefficient,temp1->exp);
temp1 = temp1->next;
}
else{
result.append(temp2->coefficient,temp2->exp);
temp2 = temp2->next;
}
}
if(temp1 == NULL){ //当多项式中的一方遍历完成,则另一个多项式之后的所有都加入到结果之中
result.getTail()->next = temp2;
}
else{
result.getTail()->next = temp1;
}
return result;
}
polynomial multiply(polynomial poly1,polynomial poly2){
polynomial result;
Node * temp1 =poly1.getHead();
while(temp1 != NULL ){
polynomial result1;
Node * temp2 =poly2.getHead(); //由于要遍历多次,所以要多次进行声明,由于局部变量是存放在栈中,执行过后将会自动进行清空,所以并不需要将其释放,如果是利用malloc在堆上进行声明的变量要进行手动的释放空间
while (temp2 != NULL){
result1.append(temp1->coefficient * temp2->coefficient,
temp1->exp + temp2->exp);
temp2 = temp2->next;
}
if(temp1 == poly1.getHead()){
result = result1;
}
else{
result = add(result,result1);
}
temp1 = temp1->next;
}
return result;
}
int main(){
polynomial poly1,poly2,result1,result2;
int n1,n2;
cin>>n1;
initPoly(n1,poly1);
cin>>n2;
initPoly(n2,poly2);
cout<<"多项式1初始化为:"<<endl;
poly1.print();
cout<<"多项式2初始化为:"<<endl;
poly2.print();
result1 = add(poly1,poly2);
cout<<"多项式1 + 多项式2的结果为:"<<endl;
result1.print();
result2 =multiply(poly1,poly2);
cout<<"多项式1 * 多项式2的结果为:"<<endl;
result2.print();
return 0;
}
利用链表实现多项式的相乘以及相加
最新推荐文章于 2024-05-08 21:34:13 发布