如何使用链接列表添加两个多项式并将答案存储在第三个列表中
问题描述:
到目前为止,我已经尝试创建一个函数来检查来自任何列表的两个元素是否相等然后添加它们但它不起作用我不知道如何插入更大的元素并分别降低
假设我正在给予以下输入
3x ^ 2 + 4x ^ 3 + 21x ^ 1
和
54x ^ 3 + 5x ^ 1
然后回答应该是
58x ^ 3 + 26x ^ 1 + 3x ^ 2
但程序没有正确添加列表,我不知道当列表的一个成员大于或小于对应时如何显示清单
我的尝试:
So far I have tried to make a function that checks if two elements from any list are equal then add them but its not working and I don't know how to insert the greater elements and lower respectively
Suppose I am giving following input
3x^2+4x^3+21x^1
and
54x^3+5x^1
then answer should be
58x^3+26x^1+3x^2
but the program is not adding lists correctly and I have no idea how to just display when one member of list is greater or smaller than corresponding list
What I have tried:
<pre lang="c++">
#include<iostream>
using namespace std;
class node{
private:
int coeff;
int exp;
node *next;
public:
void set(int coeff,int exp)
{
this->coeff=coeff;
this->exp=exp;
}
node *getnext()
{
return next;
}
void setnext(node *next)
{
this->next=next;
}
int coef()
{
return coeff;
}
int e()
{
return exp;
}
};
class list{
private:
node *head;
node *current;
int size;
public:
list()
{
head=new node();
head->setnext(NULL);
current=NULL;
size=0;
}
void create()
{
int co,ex;
node *newnode=new node();
cout<<"Enter coefficient :";
cin>>co;
cout<<"Enter exponent :";
cin>>ex;
newnode->set(co,ex);
if(current!=NULL)
{
newnode->setnext(current->getnext());
current->setnext(newnode);
current=newnode;
}
else
{
newnode->setnext(NULL);
head->setnext(newnode);
current=newnode;
}
size++;
}
int coef()
{
return current->coef();
}
int e()
{
return current->e();
}
node *cur()
{
return current;
}
int length()
{
return size;
}
void start()
{
current=head->getnext();
}
void next()
{
current=current->getnext();
}
void display()
{
if(current!=NULL)
{
cout<<current->coef()<<"x^"<<current->e();
}
}
void equal(list li2)
{
int sum;
list li3;
start();
li2.start();
int n1=length();
int n2=li2.length();
for(int i=1;i<=n1;i++)
{
for(int j=1;j<=n2;j++)
{
if(e()==li2.e())
{
sum=coef()+li2.coef();
li3.add(sum,e());
break;
}
li2.next();
}
next();
}
start();
li2.start();
for(int i=1;i<=n2;i++)
{
for(int j=1;j<=n1;j++)
{
if(li2.e()==e())
{
sum=coef()+li2.coef();
li3.add(sum,li2.e());
break;
}
next();
}
li2.next();
}
}
void add(int co,int e)
{
node *newnode=new node();
newnode->set(co,e);
if(current!=NULL)
{
newnode->setnext(current->getnext());
current->setnext(newnode);
current=newnode;
}
else
{
newnode->setnext(NULL);
head->setnext(newnode);
current=newnode;
}
size++;
}
};
int main()
{
list l1,l2,l3;
int n1,n2;
cout<<"Enter number of terms you want to enter in first polynomial :";
cin>>n1;
for(int i=1;i<=n1;i++)
{
l1.create();
}
cout<<"Enter number of terms you want to enter in second polynomial :";
cin>>n2;
for(int j=1;j<=n2;j++)
{
l2.create();
}
n1=l1.length();
n2=l2.length();
l1.start();
l2.start();
for(int i=1;i<=n1;i++)
{
l1.display();
if(i<n1)
{
cout<<"+";
}
l1.next();
}
cout<<endl;
for(int i=1;i<=n2;i++)
{
l2.display();
if(i<n2)
{
cout<<"+";
}
l2.next();
}
l1.start();
l2.start();
while(l1.cur()!=NULL && l2.cur()!=NULL)
{
l1.equal(l2);
}
cout<<endl;
int n3=l3.length();
l3.start();
for(int i=1;i<=n3;i++)
{
l3.display();
if(i<n3)
{
cout<<"+";
}
l3.next();
}
return 0;
}
答
您已在 ^ ]。请不要重新发布相同的问题。
You already posted this at Adding two polynomials by using linked list[^]. Please do not repost the same question.