⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addpoly(new).cpp

📁 数据结构课程程序
💻 CPP
字号:
/******Don't forget to download*****
*****GRAPHICAL DATA FILE STRUCTURE*****
*****A approach to learn DFS Graphically*****
Only @  http://www.vivekpatel.cjb.net        */

//Addition of Two Polynomial

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

struct poly{
    int coeff;
    int pow;
    poly *next;
};

class add2poly
{
   poly *poly1, *poly2, *poly3;
   public:
   add2poly(){poly1=poly2=poly3=NULL;}
   void addpoly();
   void display();
};

void add2poly :: addpoly(){
      int i,p;
      poly *newl=NULL,*end=NULL;
      cout<<"Enter highest power for x\n";
      cin>>p;
    //Read first poly
      cout<<"\nFirst Polynomial\n";
      for(i=p;i>=0;i--)
      {
	  newl=new poly;
	  newl->pow=p;
	  cout<<"Enter Co-efficient for degree"<<i<<"::  ";
	  cin>>newl->coeff;
	  newl->next=NULL;
	  if(poly1==NULL)
	     poly1=newl;
	  else
	     end->next=newl;
	  end=newl;
      }

    //Read Second poly
      cout<<"\n\nSecond Polynomial\n";
      end=NULL;
      for(i=p;i>=0;i--)
      {
	  newl=new poly;
	  newl->pow=p;
	  cout<<"Enter Co-efficient for degree"<<i<<"::  ";
	  cin>>newl->coeff;
	  newl->next=NULL;
	  if(poly2==NULL)
	     poly2=newl;
	  else
	     end->next=newl;
	  end=newl;
      }

      //Addition Logic
      poly *p1=poly1,*p2=poly2;
      end=NULL;
      while(p1 !=NULL && p2!=NULL){
		if(p1->pow == p2->pow){
			newl=new poly;
			newl->pow=p--;
			newl->coeff=p1->coeff + p2->coeff;
			newl->next=NULL;
			if(poly3==NULL)
			   poly3=newl;
			else
			   end->next=newl;
			end=newl;
		}
		p1=p1->next;
		p2=p2->next;
      }
}

void add2poly :: display(){
   poly *t=poly3;
   cout<<"\n\nAnswer after addition is : ";
   while(t!=NULL){
      cout.setf(ios::showpos);
      cout<<t->coeff;
      cout.unsetf(ios::showpos);
      cout<<"X"<<t->pow;
      t=t->next;
   }
}


void main(){
	clrscr();
	add2poly obj;
	obj.addpoly();
	obj.display();
	getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -