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

📄 polylink.cpp

📁 a simple program for link lists in C++
💻 CPP
字号:
//aso shojaie		    /*****IN THE NAME OF GOD******/
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
#include<ctype.h>
#include<string.h>
#include <iostream.h>
#include<math.h>


enum boolean{true=1,false=0};
struct term{
	     int coef;
	     int exp;
	     void init(int c=0,int e=0){coef=c;exp=e;}
	   };
//**********************************************************************
template<class type>class list;
template<class type>class listiterator;
template<class type>
 class node{

	  friend class list<type>;
	  friend class listiterator<type>;
	 private:
	 type  data;
	 node *link;
	 public:
	 node(type k )
	 {link=0;
	  data=k;
	   }

	  };
//***************************************
template<class type>
class list
  {  friend class listiterator<type>;
     private:
     node<type> *first;
     node<type> *last;
     public:
 //   int   conut(const list<type> &);
      void attach(type k);
     list(){first=0;}
     boolean isempty();
  };
//***********************************************
template<class type>
  void list<type>::attach(type k)
{
    node<type> *newnode=new node<type>(k);
  if(first==0)
  first=last=newnode;
  else
  {
   last->link=newnode;
   last=newnode;
  }
}
//*************************************************

template<class type>
class listiterator
{
public:
listiterator(const list<type> &l):List(l),current(l.first){};
listiterator(const polynomial<type> &p):List(p.poly),current(p.poly.first){};
boolean notnull();
boolean nextnotnull();
void firstpoly();
type *First();
type *next();
int count(const list<type> &);
private :
const list<type> &List;
node<type>  *current;
};
//******************************
template<class type>
void listiterator<type>::firstpoly()
{
 if(current!=List.first)
    current=List.first;
}

//******************************
template<class type>
boolean listiterator<type>::notnull()
{if (current)return true;
 else
 return false;
 }
//**************************************
template<class type>
boolean listiterator<type>::nextnotnull()
{
  if (current && current->link)return true;
 else return false;
}
//**************************************************
template<class type>
type * listiterator<type>::First()
{if (List.first)
return &List.first->data;
else
return 0;

}
//*********************************************
template<class type>
type* listiterator<type>::next()
{if (current)
  {
current=current->link;
return&current->data;
  }
else
return 0;
}
//*********************************************
template <class type>
class polynomial{
friend class listiterator<type>;
friend polynomial operator+(const polynomial &,const polynomial &);
friend polynomial operator*(const polynomial &,const polynomial &);
public:
void display(const polynomial&);
polynomial change1(char s[100]);
polynomial getpolynomial();
private:
list<term> poly;
} ;
//************************************************************
//**************************************************
template<class type>
void polynomial<type>::display(const polynomial<type>& p)
{ term  *retvalue;
listiterator<type> li1(p.poly);
 retvalue=li1.First();
  cout<<retvalue->coef<<"x^"<<retvalue->exp;
	 if(li1.nextnotnull())
	 cout<<" + ";
	while(li1.nextnotnull())
    {  retvalue=li1.next();
   cout<<retvalue->coef<<"x^"<<retvalue->exp;
	 if(li1.nextnotnull())
	 cout<<" + ";


}
  getch();

  }
//*****************************************************

//*****************************************************
char compare(int x,int y)
{if(x>y) return'>';
 else if(x<y)
  return '<';
  else
  return'=';

}
//************************get polynomial**********************
template<class type>
polynomial<type> polynomial<type>::getpolynomial()
{
 polynomial<type> p;
  char s[100]={0};
  cout<<"     enter a polynomial ::  ";
  cin>>s;
  p=change1(s);
  return p;  }
//**********************************************************
template<class type>
polynomial<type> operator*(const polynomial<type> &a,const polynomial<type> &b)
{ term *p,*q,temp;
 listiterator<term>aiter(a.poly);
 listiterator<term>biter(b.poly);
 polynomial<type>c;
 listiterator<term>citer(c.poly);
int i,j,coef,exp;
 p=aiter.First();
 q=biter.First();
 int bcount=biter.count(b.poly) ;
 int acount=aiter.count(a.poly);
 if(bcount>acount)

 {
 for(i=0;i<acount;i++)
 {    biter.firstpoly();
      for(j=0;j<bcount;j++)
       {
      coef=p->coef*q->coef;
      exp=p->exp+q->exp;
      temp.init(coef,exp);
      if(coef)
	 c.poly.attach(temp);
	 q=biter.next();
	 }
    {
     p=aiter.next();
     q=biter.First();
    }
  }
 }
if(bcount<=acount)
     {
 for(j=0;j<bcount;j++)
 {    aiter.firstpoly();
      for(i=0;i<acount;i++)
       {
      coef=p->coef*q->coef;
      exp=p->exp+q->exp;
      temp.init(coef,exp);
      if(coef)
	 c.poly.attach(temp);
	 p=aiter.next();
	 }
    {
     q=biter.next();
     p=aiter.First();
    }
  }
 }

      return c;
}
//}
//******************************************************
template<class type>
polynomial<type> operator+(const polynomial<type> &a,const polynomial<type> &b)
{
 term *p,*q,temp;
 listiterator<term>aiter(a.poly);
 listiterator<term>biter(b.poly);
 polynomial<type> c;
 p=aiter.First();
 q=biter.First();
 while(aiter.notnull()&& biter.notnull())
{
  switch(compare(p->exp,q->exp))
  {
  case '=':
  int x=p->coef+q->coef;
  temp.init(x,q->exp);
  if (x) c.poly.attach(temp);
  p=aiter.next();
  q=biter.next();
  break;
  case'<':
  temp.init(q->coef,q->exp);
  c.poly.attach(temp);
  q=biter.next();
  break;
  case'>':
  temp.init(p->coef,p->exp);
  c.poly.attach(temp);
  p=biter.next();
  break;
   }
}
while(aiter.notnull())
    { temp.init(p->coef,p->exp);
     c.poly.attach(temp);
     p=aiter.next();
     }
while(biter.notnull())
    { temp.init(q->coef,q->exp);
     c.poly.attach(temp);
     q=biter.next();
     }
return c;
}

//************************** change string to polynomial**********
template <class type>
polynomial<type> polynomial<type>::change1(char s[100])
{ int j=0,i=0;
  polynomial<type> c;
  term temp;
  char* cc={0};
  char *ee={0};
  char* cc1={0};
  char *ee1={0};
  int coef,e;
    while(s[i]!='\0')
  {   j=0;
     for(;isdigit(s[i]);i++)
       {
       cc[j]=s[i];
       j++;

       }
      coef=atoi(cc);
      i++;
      i++;
      j=0;
      while(isdigit(s[i]))
      {
      ee[j]=s[i];
      j++;

       }

      e=atoi(ee);
      temp.init(coef,e);
      c.poly.attach(temp);
       strcpy(cc,cc1);
       strcpy(ee,ee1);
       if(s[i]!='\0')
       i++;
    }
 return c;
}

//******************************************************
template<class type>
int listiterator<type>::count(const list<type> &l)
{int countt=0;
term *retvalue;
listiterator li(l);
 if (!li.notnull())
 return 0;
else
{retvalue=li.First();
  countt++;}

 while(li.nextnotnull())
 {  retvalue=li.next();
   countt++ ; }
   return(countt);
}

//*************************************************************
int menu()
 {clrscr();
  int answer;
   cout<<endl;
   cout<<"1)  add two polynomial "<<endl;
   cout<<"2)  Multyply two polynomial "<<endl;
   cout<<"3)  exit   "<<endl;
   cout<<endl;
   cout<<"you select a choice:(1-3)";
   cin>>answer;

   return answer;
   }
//********************************************

main()

{    clrscr();
     int c;
     int answer;
     polynomial<term> p1,p2,p3;
     for(;;)
    {
      c=menu();
      switch(c)
      {
      case 1:
      {
      p1=p1.getpolynomial();
      p2=p2.getpolynomial();
      p3=p1+p2;
      cout<<"add two polynomial ::  ";
      p3.display(p3);
       break;
       }
       case 2:
       {
       p1=p1.getpolynomial();
       p2=p2.getpolynomial();
       p3=p1*p2;
       cout<<"Multyply two polynomial ::  ";
       p3.display(p3);
       break;

	     }
       case 3:exit(0);
	     }

   }
}
 //*************************

⌨️ 快捷键说明

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