Advertisement
c4tf1sh

INTL.cpp

Oct 22nd, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.22 KB | None | 0 0
  1. #include "INTL.h"
  2.  
  3. using namespace intl::operators;
  4.  
  5. /*operators**********************************************************/
  6.  
  7. void intl::operators::operator << ( const ostream &, INTL &z )
  8. {
  9.     z.output();
  10. }
  11. /********************************************************************/
  12.  
  13. void intl::operators::operator >> ( const istream &, INTL &z )
  14. {
  15.     z.input();
  16. }
  17. /********************************************************************/
  18.  
  19. intl::INTL intl::operators::operator +( const intl::INTL &a, const intl::INTL &b )
  20. {
  21.     intl::INTL  rslt;
  22.     int         z = 0;
  23.  
  24.     rslt.size = max( a.size, b.size );
  25.  
  26.     for( int i = 0; i < rslt.size | z; i++ )
  27.     {
  28.         rslt.digits[i] = a.digits[i] + b.digits[i] + z;
  29.         if( rslt.digits[i] >= rslt.osn )
  30.         {
  31.             rslt.digits[i] -= rslt.osn;
  32.             z = 1;
  33.         }
  34.         else z = 0;
  35.     }
  36.     if( rslt.digits[rslt.size] )
  37.         rslt.size++;
  38.  
  39.     return rslt;
  40. }
  41. /********************************************************************/
  42.  
  43. void intl::operators::operator ++( intl::INTL &a )
  44. {
  45.     a = a + intl::int2intl(1);
  46. }
  47. /********************************************************************/
  48.  
  49. void intl::operators::operator +=( intl::INTL &a, const intl::INTL &b )
  50. {
  51.     a = a + b;
  52. }
  53. /********************************************************************/
  54.  
  55. void intl::operators::operator --( intl::INTL &a )
  56. {
  57.     a = a - intl::int2intl(1);
  58. }
  59. /********************************************************************/
  60.  
  61. void intl::operators::operator -=( intl::INTL &a, const intl::INTL &b )
  62. {
  63.     a = a - b;
  64. }
  65. /********************************************************************/
  66.  
  67. intl::INTL sub( const intl::INTL &a, const intl::INTL &b )
  68. {
  69.     intl::INTL rslt = a;
  70.     int z = 0;
  71.  
  72.     for( int i = 0; i < rslt.size; i++ )
  73.     {
  74.         rslt.digits[i] -= b.digits[i] + z;
  75.  
  76.         if( rslt.digits[i] < 0 )
  77.         {
  78.             rslt.digits[i] += rslt.osn;
  79.             rslt.digits[i+1]--;
  80.         }
  81.     }
  82.  
  83.     int pos = rslt.size;
  84.  
  85.     while( pos && !rslt.digits[pos] )
  86.         pos--;
  87.  
  88.     rslt.size = pos + 1;
  89.  
  90.     return rslt;
  91. }
  92. /********************************************************************/
  93.  
  94. intl::INTL intl::operators::operator - ( const intl::INTL &a, const intl::INTL &b )
  95. {
  96.     intl::INTL c;
  97.  
  98.     if( a < b )
  99.     {
  100.         c = sub( b, a );
  101.         c.negative = true;
  102.     }
  103.     else
  104.     {
  105.         c = sub( a, b );
  106.         c.negative = false;
  107.     }
  108.  
  109.     return c;
  110. }
  111. /********************************************************************/
  112.  
  113. intl::INTL intl::operators::operator * ( const intl::INTL &a, const intl::INTL &b )
  114. {
  115.     intl::INTL rslt;
  116.     for( int i = 0; i < a.size; i++ )
  117.     {
  118.         int z = 0;
  119.         for( int j = 0; j < b.size | z; j++ )
  120.         {
  121.             rslt.digits[i+j] += a.digits[i] * b.digits[j] + z;
  122.             z = rslt.digits[i+j] / rslt.osn;
  123.             rslt.digits[i+j] -= z * rslt.osn;
  124.         }
  125.     }
  126.  
  127.     int pos = a.size + b.size;
  128.  
  129.     while( pos > 0 && !rslt.digits[pos] )
  130.         pos--;
  131.  
  132.     rslt.size = pos + 1;
  133.  
  134.     return rslt;
  135. }
  136. /********************************************************************/
  137.  
  138. void intl::operators::operator *= ( intl::INTL &a, const intl::INTL &b )
  139. {
  140.     a = a * b;
  141. }
  142. /********************************************************************/
  143.  
  144. void next( intl::INTL &z )
  145. {
  146.     for( int i = z.size; i >= 1; i-- )
  147.         z.digits[i] = z.digits[i-1];
  148.     if( z.digits[z.size] )
  149.         z.size++;
  150. }
  151. /********************************************************************/
  152.  
  153. intl::INTL intl::operators::operator / (const intl::INTL &a, const intl::INTL &b)
  154. {
  155.     intl::INTL rslt;
  156.     intl::INTL tmp;
  157.  
  158.     for( int i = a.size - 1; i >= 0; i-- )
  159.     {
  160.         next( tmp );
  161.         tmp.digits[0] = a.digits[i];
  162.  
  163.         int x = 0;
  164.         int y = 0, z = a.osn;
  165.  
  166.         while (y <= z)
  167.         {
  168.             int m = (y + z) >> 1;
  169.             intl::INTL cur = b * m;
  170.  
  171.             if( cur <= tmp )
  172.             {
  173.                 x = m;
  174.                 y = m + 1;
  175.             }
  176.             else
  177.                 z = m - 1;
  178.         }
  179.  
  180.         rslt.digits[i] = x;
  181.         tmp = tmp - b * x;
  182.     }
  183.  
  184.     int pos = a.size;
  185.  
  186.     while( pos >= 0 && !rslt.digits[pos] )
  187.         pos--;
  188.  
  189.     rslt.size = pos + 1;
  190.  
  191.   return rslt;
  192. }
  193. /********************************************************************/
  194.  
  195. void intl::operators::operator /= ( intl::INTL &a, const intl::INTL &b )
  196. {
  197.     a = a / b;
  198. }
  199. /********************************************************************/
  200.  
  201. intl::INTL intl::operators::operator % ( const intl::INTL &a, const intl::INTL &b )
  202. {
  203.     intl::INTL rslt;
  204.     intl::INTL temp;
  205.  
  206.     for( int i = a.size - 1; i >= 0; i-- )
  207.     {
  208.         next( temp );
  209.         temp.digits[0] = a.digits[i];
  210.  
  211.         int x = 0;
  212.         int y = 0, z = a.osn;
  213.  
  214.         while( y <= z )
  215.         {
  216.             int m = (y + z) >> 1;
  217.             intl::INTL cur = b * m;
  218.  
  219.             if( cur <= temp )
  220.             {
  221.                 x = m;
  222.                 y = m + 1;
  223.             }
  224.             else
  225.                 z = m - 1;
  226.         }
  227.  
  228.         rslt.digits[i] = x;
  229.         temp = temp - b * x;
  230.     }
  231.  
  232.     return temp;
  233. }
  234. /********************************************************************/
  235.  
  236. void intl::operators::operator %= ( intl::INTL &a, const intl::INTL &b )
  237. {
  238.     a = a % b;
  239. }
  240. /********************************************************************/
  241.  
  242. bool intl::operators::operator < ( const intl::INTL &a, const intl::INTL &b )
  243. {
  244.     if( a.size != b.size )
  245.         return a.size < b.size;
  246.  
  247.     for( int i = a.size - 1; i >= 0; i-- )
  248.     {
  249.         if( a.digits[i] != b.digits[i] )
  250.             return a.digits[i] < b.digits[i];
  251.     }
  252.     return false;
  253. }
  254. /********************************************************************/
  255.  
  256. bool intl::operators::operator > ( const intl::INTL &a, const intl::INTL &b )
  257. {
  258.     if( a.size != b.size )
  259.         return a.size > b.size;
  260.  
  261.     for( int i = a.size - 1; i >= 0; i-- )
  262.     {
  263.         if( a.digits[i] != b.digits[i] )
  264.             return a.digits[i] > b.digits[i];
  265.     }
  266.     return false;
  267. }
  268. /********************************************************************/
  269.  
  270. bool intl::operators::operator <= ( const intl::INTL &a, const intl::INTL &b )
  271. {
  272.     if( a.size != b.size )
  273.         return a.size <= b.size;
  274.  
  275.     for( int i = a.size - 1; i >= 0; i-- )
  276.         if( a.digits[i] != b.digits[i] )
  277.             return a.digits[i] <= b.digits[i];
  278.  
  279.     return true;
  280. }
  281. /********************************************************************/
  282.  
  283. bool intl::operators::operator >= ( const intl::INTL &a, const intl::INTL &b )
  284. {
  285.     if( a.size != b.size )
  286.         return a.size >= b.size;
  287.  
  288.     for( int i = a.size - 1; i >= 0; i-- )
  289.         if( a.digits[i] != b.digits[i] )
  290.             return a.digits[i] >= b.digits[i];
  291.  
  292.     return true;
  293. }
  294. /********************************************************************/
  295.  
  296. bool intl::operators::operator == ( const intl::INTL &a, const intl::INTL &b )
  297. {
  298.     if( a.size != b.size )
  299.         return false;
  300.  
  301.     for( int i = 0; i < a.size; i++ )
  302.     {
  303.         if( a.digits[i] != b.digits[i] )
  304.             return false;
  305.     }
  306.  
  307.     return true;
  308. }
  309. /********************************************************************/
  310.  
  311. intl::INTL intl::pow( const intl::INTL &a, const int &N )
  312. {
  313.     intl::INTL rslt( intl::int2str(1) );
  314.     intl::INTL tmp = a;
  315.     int z = N;
  316.  
  317.     while( z )
  318.     {
  319.         if( z & 1 )
  320.             rslt = rslt * tmp;
  321.  
  322.         tmp = tmp * tmp;
  323.  
  324.         z >>= 1;
  325.     }
  326.  
  327.     return rslt;
  328. }
  329. /********************************************************************/
  330.  
  331. intl::INTL intl::sqrt( intl::INTL rslt )
  332. {
  333.  
  334. }
  335. /********************************************************************/
  336.  
  337. intl::INTL intl::factorial( intl::INTL n )
  338. {
  339.     intl::INTL rslt(1);
  340.  
  341.     try
  342.     {
  343.         for( intl::INTL i(2); i <= n; ++i )
  344.             rslt = rslt * i;
  345.     }
  346.     catch( exception &e )
  347.     {
  348.         rslt.setmemory( e.what() );
  349.     }
  350.  
  351.     return rslt;
  352. }
  353. /********************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement