• 多项式加法的链表实现


      1 /*
      2  encoding:GBK
      3  time:
      4  environment:Dev-C++ 5.11 
      5 */
      6 #include<iostream>
      7 #include<math.h>
      8 using namespace std;
      9 
     10 #define maxn 200
     11 bool sgn(float a){return fabs(a)<1e-8;}
     12 typedef struct{//存放多项式的二元组,将二元组数组起别名 
     13     float coef;
     14     int exp;
     15 }PolyArray[maxn];
     16 
     17 struct PolyNode{//结点的定义 
     18     float coef;
     19     int exp ;
     20     PolyNode* next;
     21 };
     22 
     23 class Poly{//多项式类 
     24     private:
     25         PolyNode* Head;
     26     public:
     27         Poly();//构造函数
     28         ~Poly();//析构函数,释放多项式 
     29         void PolyDisplay();//显示多项式 
     30         void CreatePoly(PolyArray a,int n);//创建多项式链表
     31         void PolySort();//有序表排序
     32         void PolyAdd(Poly LB);//多项式相加 
     33 };
     34 
     35 
     36 void Poly::CreatePoly(PolyArray a,int n)//二元表以及二元表的大小 
     37 {
     38     PolyNode *s,*r;
     39     int i;
     40     r = Head;
     41     for(int i = 0; i < n; i ++)    
     42     {
     43         s = new PolyNode;
     44         s->coef = a[i].coef;
     45         s->exp = a[i].exp;
     46         s->next = NULL;
     47         r->next = s;
     48         r = s;
     49     }
     50 }
     51 void Poly::PolySort()//表的排序 
     52 {
     53     PolyNode *p,*q,*r;
     54     p = Head->next; 
     55     if(p!=NULL)
     56     {
     57         r = p->next;
     58         p->next = NULL;
     59         p = r;
     60         while(p!=NULL)
     61         {
     62             r = p->next;
     63             q = Head;
     64             while((q->next!=NULL) && (q->next->exp < p->exp))
     65                 q = q->next;
     66                 
     67                 p->next = q->next;
     68                 q->next = p;
     69                 p = r;
     70         }
     71     }
     72 }
     73 //errors exist 
     74 void Poly::PolyAdd(Poly LB)
     75 {
     76     float sum;
     77     PolyNode *pa,*pb,*qa,*qb;
     78     pa = Head;//pa pb 指向两条链的头结点。qa qb 指向了两条链的首元 
     79     qa = pa->next;
     80     pb = LB.Head;
     81     qb = pb->next;
     82     while(qa!=NULL && qb!=NULL)//分成三种情况操作 
     83     {
     84         if(qa->exp < qb->exp)
     85         {
     86             pa = qa;
     87             qa = qa->next;
     88         }
     89         else if(qa->exp >qb->exp)
     90         {
     91             pb->next = qb->next;
     92             qb->next = qa;
     93             pa->next = qb;
     94             pa = qb;
     95             qb = pb->next;
     96         }
     97         else
     98         {
     99             sum = qa->coef+qb->coef;
    100             if(sum == 0)//系数为0的时候删除结点 ,浮点数不会严格等于零,所以需要判断 
    101             {
    102                 pa->next = qa->next;
    103                 delete qa;
    104                 qa = qa->next;
    105                 pb->next = qb->next;
    106                 delete qb;
    107                 qb = pb->next;
    108             }
    109             else
    110             {
    111                 qa->coef = sum;
    112                 pa = qa; qa = qa->next;
    113                 pb->next = qb->next;
    114                 delete qb;
    115                 qb = pb->next;
    116             }
    117         }
    118     }
    119     if(qb!=NULL)
    120     {
    121         pa->next = qb;
    122     }
    123 }
    124 void Poly::PolyDisplay()
    125 {
    126     PolyNode* q = Head;
    127     q = q->next;
    128     while(q!=NULL)
    129     {
    130         if(q->coef!=0)
    131         {
    132             cout<<"("<<q->coef<<","<<q->exp<<") "; 
    133         }
    134         q=q->next;    
    135     }
    136     cout<<endl; 
    137 }
    138 Poly::Poly(){Head = new PolyNode;}
    139 Poly::~Poly(){Head = NULL;}
    140 int main()
    141 {
    142     Poly LA,LB;
    143     PolyArray a = {{7.0,0},{3.0,1},{9.0,8},{5.0,16}};
    144     PolyArray b = {{8.0,1},{22,7},{-9.0,8}};
    145     
    146 //    PolyArray a1 = {{3.0,1},{7.0,0},{5.0,16},{9.0,8}};
    147     LA.CreatePoly(a,4);
    148     LB.CreatePoly(b,3);
    149     LA.PolyDisplay();
    150     LB.PolyDisplay();
    151     LA.PolySort();
    152     LB.PolySort();
    153     //展示合并之前的多项式 
    154     LA.PolyDisplay();
    155     LB.PolyDisplay();
    156     
    157     //多项式加法 
    158     LA.PolyAdd(LB);
    159     //展示合并后的多项式 
    160     LA.PolyDisplay(); 
    161 }
  • 相关阅读:
    python连接redis
    python3进行md5加密
    python操作mysql数据库
    python3操作excle
    memcache与redis的存储类型
    模块
    函数
    json与字典相互转换
    常用的Random函数
    字符串常用方法
  • 原文地址:https://www.cnblogs.com/randy-lo/p/12844249.html
Copyright © 2020-2023  润新知