使用链表添加两个多项式 [英] Adding two polynomials by using linked list

查看:82
本文介绍了使用链表添加两个多项式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发一个程序,它从用户那里获取两个多项式,然后添加它们,其余的条目就像我有困难一样写。

例如

如果我提供输入

2x ^ 3 + 8x ^ 4 + 5x ^ 6



3x ^ 4 + 4x ^ 6

然后输出应该是

2x ^ 3 + 11x ^ 4 + 9x ^ 6

但是我收到的都是错误< br $> b $ b

我的尝试:



I have tried to develop a program that takes two polynomials from user and then add them also the remaining entries are written just like that I have difficulty as how to do that.
For example
if I give input
2x^3+8x^4+5x^6
and
3x^4+4x^6
then output should be
2x^3+11x^4+9x^6
but all I am receiving is error

What I have tried:

#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 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++;
		}
		void cc(node *current)
		{
			this->current=current;
		}
};
int main()
{
	list l1,l2,l3;
    int n1,n2;
    cout<<"Please enter elements in ordered form "<<endl;
    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();
    }
    int n,sum;
    if(n1>n2)
    {
    	n=n1;
    }
    else
    {
    	n=n2;
    }
    node *temp1;
    node *temp2;
    l1.start();
    l2.start();
    for(int i=1;i<=n;i++)
    {
    	temp1=l1.cur();
    	temp2=l2.cur();
    	for(int i=1;i<=n1;i++)
    	{
    		for(int i=1;i<=n2;i++)
    		{
    			if(l1.e()==l2.e())
    			{
    				sum=l1.coef()+l2.coef();
    				l3.add(sum,l1.e());
    			}
    			l2.next();
    		}
    		l1.next();
    	}
    	l1.cc(temp1);
    	l2.cc(temp2);
    	if(l1.e()>l2.e())
    	{
    		l3.add(l1.coef(),l1.e());
    	}
    	else
    	{
    		l3.add(l2.coef(),l2.e());
    	}
    }
    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;
}

推荐答案

Quote:

但我收到的只是错误



什么错误信息?用哪个输入?



我看到代码中有一个主流:

你的代码没有强制执行存储列表中的coeffs顺序。通过在存储列表中强制执行coeffs的顺序,它将简化以后的使用。

您知道系数是有序的,添加2个多项式并不是很复杂。

拿一张纸和一支铅笔。注意存储在列表中的coeffs,并在第三个列表中添加2个列表。你是怎么进行的?

这是你的算法;你的代码也必须这样做。


What error message? with which input?

I see a major flow in your code:
Your code do not enforce the order of coeffs in the storage list. By enforcing the order of coeffs when you put in storage list, it will simplify later usage.
One you know the coeffs are in order, adding the 2 polynomials is not really complicated.
Take a sheet of paper and a pencil. note the coeffs as stored in lists and add the 2 lists in a third. How did you proceed ?
This is your algorithm; your code will have to do the same.


这篇关于使用链表添加两个多项式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆