多项式程序 [英] Program for Polynomials

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

问题描述

想编写C ++程序来添加,减去和分配两个多项式.

多项式可以具有任意数量的项.

我需要加法和减法,但是在将一个多项式分配给另一个多项式时遇到了困难.

加减法程序

Want to write C++ program to add , subtract and assign two polynomials.

Polynomials may have any number of terms.

I have got for addition and subtraction but getting difficulties while assigning one polynomial to another.

program to add and subtract

#include<conio.h>   	
#include<ostream.h> 
#include<process.h>  
//   Creating a NODE Structure
struct node
{
   int coe,exp;        // data
   struct node *next;  // link to next node and previous node
};
 
// Creating a class Polynomial
class polynomial
{
   struct node *start,*ptrn,*ptrp;
   public:
     void get_poly(); // to get a polynomial
     void show();     // show
     void add(polynomial p1,polynomial p2); // Add two polynomials
     void subtract(polynomial p1,polynomial p2); //Subtract2 polynomials
};
 
void polynomial::get_poly()      //  Get Polynomial
{
   char c=''y'';
   ptrn=ptrp=start=NULL;
   while(c==''y'' || c==''Y'')
   {
      ptrn=new node;
      ptrp->next=ptrn;
      if(start==NULL)
	start=ptrn;
      ptrp=ptrn;
      cout<<"\nEnter the coefficient: ";
      cin>>ptrn->coe;
      cout<<"Enter the exponent: ";
      cin>>ptrn->exp;
      ptrn->next=NULL;
      cout<<"Enter y to add more nodes: ";
      cin>>c;
   }
   return;
}
 
 
void polynomial::show()  // Show Polynomial
{
    struct node *ptr;
    ptr=start;
    while(ptr!=NULL)
    {
       cout<<ptr->coe<<"X^"<<ptr->exp<<" + ";
       ptr=ptr->next;
    }
    cout<<"\b\b ";
}
 
void polynomial::add(polynomial p1,polynomial p2)  // Add Polynomials
{
   struct node *p1ptr,*p2ptr;
   int coe,exp;
   ptrn=ptrp=start=NULL;
   p1ptr=p1.start;
   p2ptr=p2.start;
   while(p1ptr!=NULL && p2ptr!=NULL)
   {
      if(p1ptr->exp==p2ptr->exp) // If coefficients are equal
      {
	 coe=p1ptr->coe+p2ptr->coe;
	 exp=p1ptr->exp;
	 p1ptr=p1ptr->next;
	 p2ptr=p2ptr->next;
      }
      else if(p1ptr->exp>p2ptr->exp)
      {
	 coe=p1ptr->coe;
	 exp=p1ptr->exp;
	 p1ptr=p1ptr->next;
      }
      else if(p1ptr->exp<p2ptr->exp)
      {
	 coe=p2ptr->coe;
	 exp=p2ptr->exp;
	 p2ptr=p2ptr->next;
      }
      ptrn=new node;
      if(start==NULL)
	 start=ptrn;
      ptrn->coe=coe;
      ptrn->exp=exp;
      ptrn->next=NULL;
      ptrp->next=ptrn;
      ptrp=ptrn;
   } // End of While
   if(p1ptr==NULL)
   {
      while(p2ptr!=NULL)
      {
	 coe=p2ptr->coe;
	 exp=p2ptr->exp;
	 p2ptr=p2ptr->next;
	 ptrn=new node;
	 if(start==NULL)
	    start=ptrn;
	 ptrn->coe=coe;
	 ptrn->exp=exp;
	 ptrn->next=NULL;
	 ptrp->next=ptrn;
	 ptrp=ptrn;
      }
   }
   else if(p2ptr==NULL)
   {
      while(p1ptr!=NULL)
      {
	 coe=p1ptr->coe;
	 exp=p1ptr->exp;
	 p1ptr=p1ptr->next;
	 ptrn=new node;
	 if(start==NULL)
	    start=ptrn;
	 ptrn->coe=coe;
	 ptrn->exp=exp;
	 ptrn->next=NULL;
	 ptrp->next=ptrn;
	 ptrp=ptrn;
      }
   }
}  // End of addition
 
//  Subtract two polynomials
void polynomial::subtract(polynomial p1,polynomial p2)  // Subtract
{
   struct node *p1ptr,*p2ptr;
   int coe,exp;
   ptrn=ptrp=start=NULL;
   p1ptr=p1.start;
   p2ptr=p2.start;
   while(p1ptr!=NULL && p2ptr!=NULL)
   {
      if(p1ptr->exp==p2ptr->exp) // If coefficients are equal
      {
	 coe=p1ptr->coe-p2ptr->coe;
	 exp=p1ptr->exp;
	 p1ptr=p1ptr->next;
	 p2ptr=p2ptr->next;
      }
      else if(p1ptr->exp>p2ptr->exp)
      {
	 coe=p1ptr->coe;
	 exp=p1ptr->exp;
	 p1ptr=p1ptr->next;
      }
      else if(p1ptr->exp<p2ptr->exp)
      {
	 coe=0-p2ptr->coe;
	 exp=p2ptr->exp;
	 p2ptr=p2ptr->next;
      }
      ptrn=new node;
      if(start==NULL)
	 start=ptrn;
      ptrn->coe=coe;
      ptrn->exp=exp;
      ptrn->next=NULL;
      ptrp->next=ptrn;
      ptrp=ptrn;
   } // End of While
   if(p1ptr==NULL)
   {
      while(p2ptr!=NULL)
      {
	 coe=0-p2ptr->coe;
	 exp=p2ptr->exp;
	 p2ptr=p2ptr->next;
	 ptrn=new node;
	 if(start==NULL)
	    start=ptrn;
	 ptrn->coe=coe;
	 ptrn->exp=exp;
	 ptrn->next=NULL;
	 ptrp->next=ptrn;
	 ptrp=ptrn;
      }
   }
   else if(p2ptr==NULL)
   {
      while(p1ptr!=NULL)
      {
	 coe=p1ptr->coe;
	 exp=p1ptr->exp;
	 p1ptr=p1ptr->next;
	 ptrn=new node;
	 if(start==NULL)
	    start=ptrn;
	 ptrn->coe=coe;
	 ptrn->exp=exp;
	 ptrn->next=NULL;
	 ptrp->next=ptrn;
	 ptrp=ptrn;
      }
   }
}  // End of subtraction
 
 
int main()
{
   clrscr();
   polynomial p1,p2,sum,diff;
   cout<<"First Polynomial.\n";
   p1.get_poly();
   cout<<"\nSecond polynomial.\n";
   p2.get_poly();
   clrscr();
   cout<<"\nThe First polynomial is: ";
   p1.show();
   cout<<"\nThe second polynomial is: ";
   p2.show();
   cout<<"\n\nThe sum of two polynomials is: ";
   sum.add(p1,p2);
   sum.show();
   cout<<"\n\nThe difference of two polynomials is: ";
   diff.subtract(p1,p2);
   diff.show();
   getch();
   return 0;
}

推荐答案

我认为在get_poly()函数的此行ptrp->next=ptrn;中存在问题.您将ptrp分配为NULL并且正在访问ptrp->next .
I think there is a Problem in this line ptrp->next=ptrn; in get_poly() function.you assigned ptrp to NULL and you are accessing ptrp->next.


void polynomial::get_poly()      //  Get Polynomial
{
   char c='y';
   ptrn=ptrp=start=NULL;
   while(c=='y' || c=='Y')
   {
      ptrn=new node;
      ptrp=ptrn;
      if(start==NULL)
	start=ptrn;
      cout<<"\nEnter the coefficient: ";
      cin>>ptrn->coe;
      cout<<"Enter the exponent: ";
      cin>>ptrn->exp;
      ptrn=ptrn->next;
      cout<<"Enter y to add more nodes: ";
      cin>>c;
   }
   return;
}



自从我上次创建链接列表以来,已经很老了.



Wild guess, it has been ages since I last made linked lists.


这篇关于多项式程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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