Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Examples  

bigarith.h

Go to the documentation of this file.
00001 //  N.Yu.Zolotykh 1999, 2000
00002 //  University of Nizhni Novgorod, Russia
00003 //  Arageli 1.02
00004 
00005 
00016 
00017 #include<iostream.h>
00018 #include<stdlib.h>
00019 #include<string.h>
00020 #include<ctype.h>
00021 #include<strstrea.h>
00022 #include "arageli.h"
00023 
00024 #ifndef __cplusplus
00025 #  error Must use C++ for the type big_int.
00026 #endif
00027 
00028 #ifndef BIGARITH_H_
00029 #define BIGARITH_H_
00030 
00031 #ifdef USE_ASM
00032 #  if defined(__WIN32__) || defined (__WATCOMC__)
00033      typedef unsigned int digit; // 4 bytes
00034      const digit max_digit = 0xFFFFFFFF;
00035      const int bits_per_digit = 32;
00036 #    include"bigar32.h"
00037 #  else
00038      typedef unsigned int digit;
00039      const digit max_digit = 0xFFFF;
00040      const int bits_per_digit = 16;
00041 #    include"bigarbc.h"
00042 #  endif
00043 #else
00044      typedef unsigned int digit;
00045      const digit max_digit = 0xFFFF;
00046      const int bits_per_digit = 16;
00047 #    include"bigar.h"
00048 #endif
00049 
00050 
00054 
00055 class big_int
00056 {
00057 
00064 
00065  public:
00066 
00067   big_int();                    
00068   big_int(char *str);           
00069   big_int(const big_int & b);   
00070   big_int(int b);               
00071   ~big_int();                   
00072 
00073   big_int & operator = (const big_int & b);     
00074 
00076   size_t length();
00077 
00078   int operator [] (size_t k);
00079 
00080   // Converts a string to a big_int
00081   // friend void string_to_num(char *s, big_int & b);
00082   // надо бы убрать вообще эту функцию,
00083   // а где-нибудь в powerest создать шаблон b=s
00084 
00086   // void to_string(char *string, size_t size, digit radix = 10) const;
00088   friend ostream & operator << (ostream & s, const big_int & x);
00090   friend istream & operator >> (istream & s, big_int & x);
00091 
00093 
00099   friend int cmp(const big_int & a, const big_int & b);
00100 
00102   friend int operator == (const big_int & a, const big_int & b);
00103 
00105   friend int operator != (const big_int & a, const big_int & b);
00106 
00108   friend int operator > (const big_int & a, const big_int & b);
00109 
00111   friend int operator >= (const big_int & a, const big_int & b);
00112 
00114   friend int operator < (const big_int & a, const big_int & b);
00115 
00117   friend int operator <= (const big_int & a, const big_int & b);
00118 
00119   friend big_int operator + (const big_int & a);       
00120   friend big_int operator - (const big_int & a);       
00121 
00123   friend big_int operator + (const big_int & b, const big_int & c);
00125   friend big_int operator - (const big_int & b, const big_int & c);
00127   friend big_int operator *(const big_int & b, const big_int & c);
00129   friend big_int operator / (const big_int & b, const big_int & c);
00131   friend big_int operator % (const big_int & b, const big_int & c);
00133   friend big_int & operator += (big_int & b, const big_int & c);
00135   friend big_int & operator -= (big_int & b, const big_int & c);
00137   friend big_int & operator *= (big_int & b, const big_int & c);
00139   friend big_int & operator /= (big_int & b, const big_int & c);
00141   friend big_int & operator %= (big_int & b, const big_int & c);
00142 
00144 
00145   friend void divide(big_int & a, const big_int & b, const big_int & c,
00146       big_int & res);
00147 
00149   friend big_int random_number(size_t length);
00150   friend big_int random_number1(size_t length); //bits
00151 
00152 
00153  private:
00154 
00155   // the following type is used inside the big_arith unit and implements
00156   // the storage for a Big Integer Number
00157 
00158   struct big_struct
00159   {
00160     int sign;                   // the sign: 0, 1 or -1
00161     digit *data;                // the storage for digits
00162     size_t len;                 // the number of digits
00163     int refs;                   // the number of points to this number
00164   } *number;
00165 
00166   // number allocation routines
00167   void alloc_number(int new_sign, digit * new_mem, size_t new_len);
00168   void free_number();
00169   void free_mem_and_alloc_number(int new_sign, digit * new_data, size_t new_len);
00170   void alloc_zero();
00171   void free_mem_and_alloc_zero();
00172 
00173 };
00174 #endif BIGARITH_H_

Generated at Tue Jan 22 20:37:04 2002 for Arageli by doxygen1.2.9.1 written by Dimitri van Heesch, © 1997-2001