polygf2.cpp
上传用户:jtjnyq9001
上传日期:2014-11-21
资源大小:3974k
文件大小:7k
源码类别:

3G开发

开发平台:

Visual C++

  1. // polygf2.cpp
  2. //
  3. #include "iostream.h"
  4. #include "polygf2.h"
  5. PolyOvrGF2::PolyOvrGF2(void)
  6. {
  7.   
  8.   /*
  9.   Degree=7;
  10.   Coeff = new int[Degree+1];
  11.   Coeff[0] = 1;
  12.   Coeff[1] = 1;
  13.   Coeff[2] = 0;
  14.   Coeff[3] = 0;
  15.   Coeff[4] = 0;
  16.   Coeff[5] = 0;
  17.   Coeff[6] = 0;
  18.   Coeff[7] = 1;
  19.   */
  20.   
  21.   /*
  22.   Degree=2;  // P(x)= x**2 + x + 2
  23.   Coeff = new int[Degree+1];
  24.   Coeff[0] = 2;
  25.   Coeff[1] = 1;
  26.   Coeff[2] = 1;
  27.   */
  28.   
  29.   /*Degree=2;  // P(x)= x**2 + 2x + 2
  30.   Coeff = new int[Degree+1];
  31.   Coeff[0] = 2;
  32.   Coeff[1] = 2;
  33.   Coeff[2] = 1;
  34. */
  35.   Degree=0;
  36.   Coeff = new int[Degree+1];
  37.   Coeff[0] = 0;
  38.   
  39.   /*
  40.   Degree=3;
  41.   Coeff = new int[Degree+1];
  42.   Coeff[0] = 1;
  43.   Coeff[1] = 0;
  44.   Coeff[2] = 2;
  45.   Coeff[3] = 1;
  46.   */
  47.   cout << "Hello from PolyOvrGF2 ctor" << endl;
  48.   Rem_Coeff = NULL;
  49. }
  50. //======================================
  51. PolyOvrGF2::PolyOvrGF2(int degree)
  52. {
  53.   int i;
  54.   Degree = degree;
  55.   Coeff = new int[Degree+1];
  56.   for(i=0; i<=Degree; i++)
  57.       Coeff[i] = 0;
  58.   Rem_Degree = 0;
  59.   Rem_Coeff = new int[Rem_Degree+1];
  60.   for(i=0; i<=Rem_Degree; i++)
  61.       Rem_Coeff[i] = 0;
  62. }
  63. //======================================
  64. PolyOvrGF2::PolyOvrGF2(int degree, int coeff)
  65. {
  66.   int i;
  67.   Degree = degree;
  68.   Coeff = new int[Degree+1];
  69.   for(i=0; i<=Degree; i++)
  70.       Coeff[i] = 0;
  71.   Coeff[degree] = coeff;
  72.   Coeff[0] = 1;
  73.   Rem_Degree = 0;
  74.   Rem_Coeff = new int[Rem_Degree+1];
  75. }
  76. int PolyOvrGF2::MaxDegree(void)
  77. {
  78.   return Degree;
  79. }
  80. int PolyOvrGF2::Coefficient(int degree)
  81. {
  82.   return Coeff[degree];
  83. }
  84. //========================================================
  85. void PolyOvrGF2::SetRemainder( PolyOvrGF2* poly )
  86. {
  87.   if(Rem_Coeff != NULL) delete[] Rem_Coeff;
  88.   Rem_Degree = poly->Degree;
  89.   Rem_Coeff = new int[Rem_Degree+1];
  90.   for(int i=0; i<=Rem_Degree; i++)
  91.     {
  92.     Rem_Coeff[i] = poly->Coeff[i];
  93.     }
  94. }
  95. //========================================================
  96. void PolyOvrGF2::SetRemainder( int degree, int* coeff )
  97. {
  98.   if(Rem_Coeff != NULL) delete[] Rem_Coeff;
  99.   Rem_Degree = degree;
  100.   Rem_Coeff = new int[Rem_Degree+1];
  101.   for(int i=0; i<=Rem_Degree; i++)
  102.     {
  103.     Rem_Coeff[i] = coeff[i];
  104.     }
  105. }
  106. //===========================================================
  107. PolyOvrGF2& PolyOvrGF2::operator* (const PolyOvrGF2 &right)
  108. {
  109.   PolyOvrGF2 *result;
  110.   result = new PolyOvrGF2( Degree + right.Degree );
  111.   //--------------------------------
  112.   // perform multiplication
  113.   for(int rgt_idx=0; rgt_idx<= right.Degree; rgt_idx++)
  114.     {
  115.     for( int this_idx=0; this_idx <=Degree; this_idx++)
  116.       {
  117.       result->Coeff[this_idx+rgt_idx] +=
  118.                 (Coeff[this_idx] * right.Coeff[rgt_idx]);
  119.       }
  120.     }
  121.   return *result;
  122. }
  123. //===========================================================
  124. PolyOvrGF2& PolyOvrGF2::operator*= (const PolyOvrGF2 &right)
  125. {
  126.   //---------------------------------------------------
  127.   // save pointer to original coefficient array so that
  128.   // this array can be deleted once no longer needed
  129.   int *orig_coeff = Coeff;
  130.   int orig_degree = Degree;
  131.   int i;
  132.   //------------------------------------------------------
  133.   // create new longer array to hold the new coefficients
  134.   Degree += right.Degree;
  135.   Coeff = new int[Degree+1];
  136.   for( i=0; i<=Degree; i++)
  137.       Coeff[i] = 0;
  138.   //--------------------------------
  139.   // perform multiplication
  140.   for(int rgt_idx=0; rgt_idx<= right.Degree; rgt_idx++)
  141.     {
  142.     for( int orig_idx=0; orig_idx <=orig_degree; orig_idx++)
  143.       {
  144.       Coeff[orig_idx+rgt_idx] +=
  145.                 (orig_coeff[orig_idx] * right.Coeff[rgt_idx]);
  146.       }
  147.     }
  148.   //delete []orig_coeff;
  149.   return *this;
  150. }
  151. //===========================================================
  152. PolyOvrGF2& PolyOvrGF2::operator/ (const PolyOvrGF2 &divisor)
  153. {
  154.   int dividend_deg, divisor_deg, j, k;
  155.   PolyOvrGF2 *result;
  156.   //------------------------------------------
  157.   //  allocate new polynomial for result
  158.  // PolyOvrGF2 *result = new PolyOvrGF2();
  159.   dividend_deg = Degree;
  160.   divisor_deg = divisor.Degree;
  161.   int *work = new int[dividend_deg+1];
  162.   if(dividend_deg < divisor_deg)
  163.     {
  164.     // division does not take place.
  165.     // remainder is equal to dividend
  166.     PolyOvrGF2 *result = new PolyOvrGF2(0);
  167.     result->SetRemainder(this);
  168.     }
  169.   else
  170.     {    
  171.     //--------------------------------
  172.     // perform division
  173.     result = new PolyOvrGF2(dividend_deg-divisor_deg);
  174.     for(j=0; j<= dividend_deg; j++)
  175.       {
  176.       work[j] = Coeff[j];
  177.       }
  178.     for(k=dividend_deg-divisor_deg; k>=0; k--)
  179.       {
  180.       result->Coeff[k] = work[divisor_deg+k]/divisor.Coeff[divisor_deg];
  181.       work[divisor_deg+k]=0;
  182.       for(j=divisor_deg+k-1; j>=k; j--)
  183.         work[j] = work[j] - (result->Coeff[k]) * divisor.Coeff[j-k];
  184.       }
  185.     int rem_deg = 0;
  186.     for(j=0; j=dividend_deg ; j++)
  187.       {
  188.       if(work[j] != 0) rem_deg = j;
  189.       }
  190.     result->SetRemainder(rem_deg, work);
  191.     }
  192.   return *result;
  193. }
  194. //============================================================
  195. PolyOvrGF2& PolyOvrGF2::operator/= (const PolyOvrGF2 &divisor)
  196. {
  197.   int dividend_deg, divisor_deg, j, k;
  198.   //------------------------------------------
  199.   //
  200.   if(Rem_Coeff != NULL) delete[] Rem_Coeff;
  201.   dividend_deg = Degree;
  202.   divisor_deg = divisor.Degree;
  203.   int *work = new int[dividend_deg+1];
  204.   if(dividend_deg < divisor_deg)
  205.     {
  206.     // division does not take place.
  207.     // remainder is equal to dividend
  208.     Rem_Degree = Degree;
  209.     Rem_Coeff = new int[Rem_Degree+1];
  210.     }
  211.   else
  212.     {    
  213.     //--------------------------------
  214.     // perform division
  215.     for(j=0; j<= dividend_deg; j++)
  216.       {
  217.       work[j] = Coeff[j];
  218.       Coeff[j] = 0;
  219.       }
  220.     for(k=dividend_deg-divisor_deg; k>=0; k--)
  221.       {
  222.       Coeff[k] = work[divisor_deg+k]/divisor.Coeff[divisor_deg];
  223.       work[divisor_deg+k]=0;
  224.       for(j=divisor_deg+k-1; j>=k; j--)
  225.         work[j] = work[j] - Coeff[k] * divisor.Coeff[j-k];
  226.       }
  227.     int rem_deg = 0;
  228.     for(j=0; j<=dividend_deg ; j++)
  229.       {
  230.       if(work[j] != 0) rem_deg = j;
  231.       }
  232.     Rem_Degree = rem_deg;
  233.     Rem_Coeff = new int[Rem_Degree+1];
  234.     for(j=0; j=rem_deg; j++)
  235.       Rem_Coeff[j] = work[j];
  236.     }
  237.   return *this;
  238. }
  239. //=========================================================
  240. //  dump polynomial to an output stream
  241. void PolyOvrGF2::DumpToStream( ostream* output_stream)
  242. {
  243.  (*output_stream) << "Degree = " << Degree << endl;
  244.  
  245.  for(int i=Degree; i>=0; i--)
  246.    {
  247.     (*output_stream) << "Coeff[" << i << "] = " 
  248.                      << Coeff[i] << endl;
  249.    }
  250.  return;
  251. }