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

📄 多项式 之.cpp

📁 此为求多项式相加减乘除的源程序
💻 CPP
字号:
#include<iostream.h>
#include<malloc.h>
struct Term
{
   double coefficient;//系数
   int exponent;//指数
   struct Term *next;//下一个指针
};
 Term * creat()
{  //用头插法生成一个多项式,系数和指数输入0时退出输入
   int m;
   double n;
   Term *head,*rear,*s;
   //head为头指针,rear和s为临时指针
   head=(Term *)malloc(sizeof(Term));
   rear=head;
   //指向头
   cout<<"input coefficient(系数):";//输入系数
   cin>>n;
   cout<<"input exponent(指数-输入负数时结束):";//输入指数
   cin>>m;
   while(m>=0)//输入0就退出
   {
     s=(Term *)malloc(sizeof(Term));
     s->coefficient=n;
     s->exponent=m;
     s->next=NULL;
     rear->next=s;//头插法
     rear=s;
     cout<<"input coefficient(系数):";//输入系数
     cin>>n;
     cout<<"input exponent(指数-输入负数时结束):";//输入指数
     cin>>m;
    }
    head=head->next;//第一个头没有用到
  return head;
}
void swap(Term *head)      //交换位置的函数
{
   Term *p,*q,*t;
    int temp;
    p=head;
    while(p!=NULL)
    {
      q=p;
      t=q->next;
      while(t!=NULL)
       {
        if(t->exponent>q->exponent)
        q=t;
        t=t->next;
       }
       temp=p->coefficient;
       p->coefficient=q->coefficient;
       q->coefficient=temp;
       temp=p->exponent;
       p->exponent=q->exponent;
       q->exponent=temp;
       p=p->next;
    }

}
void print(Term *head)//显示一个多项式
{
   Term *p;
   double x;
   int one_time=1; //标志多项式的第一项
   p=head;
   while(p!=NULL)//不为空的话
   {
     if(p->coefficient==0) //如果系数为0
        cout<<"";
     if(one_time==1) //如果是多项式的第一项,则前面不用加+
      {
        if(p->exponent==0)//如果指数为0的话,直接输出系数
        cout<<p->coefficient; //如果系数是正的话前面就要加+号
        else if(p->coefficient>0)//如果系数是大于0的话就输出+系数x^指数的形式
             {
               if(p->exponent==1)
               cout<<p->coefficient<<"x";
               if(p->coefficient==1)
               cout<<"x^"<<p->exponent;//如果系数是1的话就直接输出+x号
                else  cout<<p->coefficient<<"x^"<<p->exponent;
             }
        else if(p->coefficient<0)//如果系数是小于0的话就输出系数x^指数的形式
             {
               if(p->coefficient==-1)
               cout<<"-"<<"x^"<<p->exponent;//如果系数是-1的话就直接输出-x号
               if(p->exponent==1)
               cout<<p->coefficient<<"x";
              else cout<<p->coefficient<<"x^"<<p->exponent;
            }
        one_time=0;
      }
     else
        {
            if(p->exponent==0)//如果指数为0的话,直接输出系数

             {
               if(p->coefficient>0)
                cout<<"+"<<p->coefficient; //如果系数是正的话前面就要加+号
             }
            else if(p->coefficient>0)//如果系数是大于0的话就输出+系数x^指数的形式
                  {
                    if(p->exponent==1)
                     cout<<p->coefficient<<"x";
                    if(p->coefficient==1)
                     cout<<"+"<<"x^"<<p->exponent;
                    else cout<<"+"<<p->coefficient<<"x^"<<p->exponent;
                  }
            else if(p->coefficient<0)//如果系数是小于0的话就输出系数x^指数的形式
                  {
                    if(p->coefficient==-1)
                     cout<<"-"<<"x^"<<p->exponent;//如果系数是1的话就直接输出-x号
                    if(p->exponent==1)
                     cout<<p->coefficient<<"x";
                    else cout<<p->coefficient<<"x^"<<p->exponent;
                  }
       }
       p=p->next;//指向下一个指针
   }
     cout<<'\n';
}
Term * equals_sum(Term *heada,Term *headb)
{ //2个多项式的加法运算
  Term *headc,*p,*q,*s,*r;
  //headc为头指针,r,s为临时指针,p指向第1个多项式并向右移动,q指向第2个多项式并并向右移动
  double x;
  //x为系数的求和
  p=heada;
  //指向第一个多项式的头
  q=headb;
  //指向第二个多项式的头
  headc=(Term *)malloc(sizeof(Term));
  r=headc;
  //开辟空间
  while(p!=NULL&&q!=NULL)
  //2个多项式的某一项都不为空时
   {
     if(p->exponent==q->exponent)//指数相等的话
      {
        x=p->coefficient+q->coefficient;//系数就应该相加
        if(x!=0)//相加的和不为0的话
         {
           s=(Term *)malloc(sizeof(Term));//用头插法建立一个新的节点
           s->coefficient=x;
           s->exponent=p->exponent;
           r->next=s;
           r=s;
         }
         q=q->next;
         p=p->next;
         //2个多项式都向右移
      }
      else if(p->exponent<q->exponent)//p的系数小于q的系数的话,就应该复制q接点到多项式中
       {
         s=(Term *)malloc(sizeof(Term));
         s->coefficient=q->coefficient;
         s->exponent=q->exponent;
         r->next=s;
         r=s;
         q=q->next;//q向右移动
       }
       else//p的系数大于q的系数的话,就应该复制p接点到多项式中
        {
          s=(Term *)malloc(sizeof(Term));
          s->coefficient=p->coefficient;
          s->exponent=p->exponent;
          r->next=s;
          r=s;
          p=p->next;//p向右移动
        }
    }
    //当第2个多项式空,第1个数不为空时,将第一个数剩下的全用新节点产生
    while(p!=NULL)
     {
       s=(Term *)malloc(sizeof(Term));
       s->coefficient=p->coefficient;
       s->exponent=p->exponent;
       r->next=s;
       r=s;
       p=p->next;
     }
     //当第1个多项式空,第2个数不为空时,将第2个数剩下的全用新节点产生
     while(q!=NULL)
     {
      s=(Term *)malloc(sizeof(Term));
      s->coefficient=q->coefficient;
      s->exponent=q->exponent;
      r->next=s;
      r=s;
      q=q->next;
     }
     r->next=NULL;
    //最后指向空
     headc=headc->next;//第一个头没有用到
     return headc;//返回头接点
}
Term *subtration(Term *heada,Term *headb)
{ //2个多项式的减法运算,和加法类似,不同的地方已经注释
  Term *headc,*p,*q,*s,*r;
  double x;
  p=heada;
  q=headb;
  headc=(Term *)malloc(sizeof(Term));
  r=headc;
  while(p!=NULL&&q!=NULL)
   {
    if(p->exponent==q->exponent)
     {
       x=p->coefficient-q->coefficient;//系数相减
       if(x!=0)
        {
          s=(Term *)malloc(sizeof(Term));
          s->coefficient=x;
          s->exponent=p->exponent;
          r->next=s;
          r=s;
        }
       q=q->next;p=p->next;
    }
    else if(p->exponent<q->exponent)//p的指数小于q的指数的话
     {
       s=(Term *)malloc(sizeof(Term));
       s->coefficient=-q->coefficient;//建立的接点的系数为原来的相反数
       s->exponent=q->exponent;
       r->next=s;
       r=s;
       q=q->next;
     }
     else
      {
        s=(Term *)malloc(sizeof(Term));
        s->coefficient=p->coefficient;
        s->exponent=p->exponent;
        r->next=s;
        r=s;
        p=p->next;
      }
   }
   while(p!=NULL)
    {
      s=(Term *)malloc(sizeof(Term));
      s->coefficient=p->coefficient;
      s->exponent=p->exponent;
      r->next=s;
      r=s;
      p=p->next;
    }
    while(q!=NULL)
     {
       s=(Term *)malloc(sizeof(Term));
       s->coefficient=-q->coefficient;//建立的接点的系数为原来的相反数
       s->exponent=q->exponent;
       r->next=s;
       r=s;
       q=q->next;
      }
     r->next=NULL;
     headc=headc->next;
  return headc;
}
Term * sub_mul(Term *heada,double coefficient,int exponent)
{  //一个多项式乘于coef x^exp后得到的值,如(5x^4+4x^3+x^1)*2x^2
   Term *head,*rear,*s,*p;
   p=heada;
   head=(Term *)malloc(sizeof(Term));
   rear=head;
   while(p!=NULL)
    {
      s=(Term *)malloc(sizeof(Term));
      s->coefficient=p->coefficient*coefficient;//系数为原来的系数乘于coef
      s->exponent=p->exponent+exponent;//指数为原来的指数加exp
      rear->next=s;
      rear=s;//头插法
      p=p->next;
    }
    rear->next=NULL;
    head=head->next;
  return head;
}
//2个多项式的相乘,将第一个多项式去乘于第2个多项式的每一项,得到一些新多项式,再相加就是最后结果
Term * multiplicative(Term *heada,Term *headb)
{
  Term * headc ,*q;
  int one_time=1;
  q=headb;
  while(q!=NULL)
   {
     if(one_time==1)
      {
        headc=sub_mul(heada,q->coefficient,q->exponent);
        one_time=0;
      }
        //第一次headc就是第一个多项式去乘于第2个多项式的第一项
     else
      headc=equals_sum(headc,sub_mul(heada,q->coefficient,q->exponent));
      //以后headc就是headc加上第一个多项式去乘于第2个多项式的后面的数
      q=q->next;
  }
 return headc;
}

void equals_sum_main()
{
  Term * a,*b,*c;
  cout<<"==============input the first=================\n";
  a=creat();//生成第一个多项式
  swap(a);
  cout<<"==============input the second=================\n";
  b=creat();//生成第二个多项式
  swap(b);
  c=equals_sum(a,b);//加法
  cout<<"===============the first========================\n";
  print(a);
  cout<<"===============The second========================\n";
  print(b);
  cout<<"=====================sum is=====================\n";
  print(c);
}
void subtration_main()
{
   Term * a,*b,*c;
   cout<<"==================input the first==============\n";
   a=creat();//生成第一个多项式
   swap(a);
   cout<<"==================input the second=============\n";
   b=creat();//生成第二个多项式
   swap(b);
   c=subtration(a,b);//减法
   cout<<"==================the first===================\n";
   print(a);
   cout<<"==================the second==================\n";
   print(b);
   cout<<"==================subtration is===============\n";
   print(c);
}
void multiplicative_main()
{
  Term * a,*b,*c;
  cout<<"==============input the first========================\n";
  a=creat();//生成第一个多项式
  swap(a);
  cout<<"==============input the second=======================\n";
  b=creat();//生成第二个多项式
  swap(b);
  c=multiplicative(a,b);//乘法
  cout<<"===============the first=============================\n";
  print(a);
  cout<<"===============the second============================\n";
  print(b);
  cout<<"===============multiplicative is=====================\n";
  print(c);
}
void main()
{
   int choose;
   cout<<"1:equals_sum(相加)2:subtration(相减)3:multiplicative(相乘)4:exit(结束)\n";
   //输入1为两个多项式相加,2为两个多项式相减,3为两个多项式相乘,4为结束。
   cin>>choose;
   while(choose!=4)
   {
    switch(choose)
    {
       case 1:equals_sum_main();break;//加法
       case 2:subtration_main();break;//减法
       case 3:multiplicative_main();break;//乘法
    }
    cout<<"\n1:equals_sum 2:subtration 3:multiplicative 4:exit\n";
     //输入1为两个多项式相加,2为两个多项式相减,3为两个多项式相乘,4为结束。
    cin>>choose;
  }
}

⌨️ 快捷键说明

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