Commit | Line | Data |
---|---|---|
0afe80d5 | 1 | // Sample program demonstrating the use of the Big Integer Library. |
05780f4b | 2 | |
b3fe29df | 3 | // Standard libraries |
05780f4b MM |
4 | #include <string> |
5 | #include <iostream> | |
6 | ||
0afe80d5 MM |
7 | // `BigIntegerLibrary.hh' includes all of the library headers. |
8 | #include "BigIntegerLibrary.hh" | |
b3fe29df | 9 | |
05780f4b | 10 | int main() { |
0afe80d5 MM |
11 | /* The library throws `const char *' error messages when things go |
12 | * wrong. It's a good idea to catch them using a `try' block like this | |
13 | * one. Your C++ compiler might need a command-line option to compile | |
14 | * code that uses exceptions. */ | |
05780f4b MM |
15 | try { |
16 | BigInteger a; // a is 0 | |
17 | int b = 535; | |
5ff40cf5 | 18 | |
0afe80d5 MM |
19 | /* Any primitive integer can be converted implicitly to a |
20 | * BigInteger. */ | |
21 | a = b; | |
22 | ||
23 | /* The reverse conversion requires a method call (implicit | |
24 | * conversions were previously supported but caused trouble). | |
25 | * If a were too big for an int, the library would throw an | |
26 | * exception. */ | |
27 | b = a.toInt(); | |
5ff40cf5 | 28 | |
b3fe29df | 29 | BigInteger c(a); // Copy a BigInteger. |
5ff40cf5 | 30 | |
0afe80d5 | 31 | // The int literal is converted to a BigInteger. |
00c6448a | 32 | BigInteger d(-314159265); |
5ff40cf5 | 33 | |
0afe80d5 MM |
34 | /* This won't compile (at least on 32-bit machines) because the |
35 | * number is too big to be a primitive integer literal, and | |
36 | * there's no such thing as a BigInteger literal. */ | |
05780f4b | 37 | //BigInteger e(3141592653589793238462643383279); |
5ff40cf5 | 38 | |
00c6448a | 39 | // Instead you can convert the number from a string. |
05780f4b | 40 | std::string s("3141592653589793238462643383279"); |
0afe80d5 | 41 | BigInteger f = stringToBigInteger(s); |
5ff40cf5 | 42 | |
00c6448a | 43 | // You can convert the other way too. |
0afe80d5 | 44 | std::string s2 = bigIntegerToString(f); |
5ff40cf5 | 45 | |
0afe80d5 | 46 | // f is implicitly stringified and sent to std::cout. |
00c6448a | 47 | std::cout << f << std::endl; |
5ff40cf5 | 48 | |
0afe80d5 MM |
49 | /* Let's do some math! The library overloads most of the |
50 | * mathematical operators (including assignment operators) to | |
51 | * work on BigIntegers. There are also ``copy-less'' | |
52 | * operations; see `BigUnsigned.hh' for details. */ | |
53 | ||
54 | // Arithmetic operators | |
05780f4b | 55 | BigInteger g(314159), h(265); |
0afe80d5 MM |
56 | std::cout << (g + h) << '\n' |
57 | << (g - h) << '\n' | |
58 | << (g * h) << '\n' | |
59 | << (g / h) << '\n' | |
60 | << (g % h) << std::endl; | |
5ff40cf5 | 61 | |
0afe80d5 | 62 | // Bitwise operators |
ef2b7c59 | 63 | BigUnsigned i(0xFF0000FF), j(0x0000FFFF); |
0afe80d5 | 64 | // The library's << operator recognizes base flags. |
ef2b7c59 | 65 | std::cout.flags(std::ios::hex | std::ios::showbase); |
0afe80d5 MM |
66 | std::cout << (i & j) << '\n' |
67 | << (i | j) << '\n' | |
68 | << (i ^ j) << '\n' | |
69 | // Shift distances are ordinary unsigned ints. | |
70 | << (j << 21) << '\n' | |
71 | << (j >> 10) << '\n'; | |
ef2b7c59 | 72 | std::cout.flags(std::ios::dec); |
5ff40cf5 | 73 | |
00c6448a MM |
74 | // Let's do some heavy lifting and calculate powers of 314. |
75 | int maxPower = 10; | |
76 | BigUnsigned x(1), big314(314); | |
77 | for (int power = 0; power <= maxPower; power++) { | |
78 | std::cout << "314^" << power << " = " << x << std::endl; | |
79 | x *= big314; // A BigInteger assignment operator | |
80 | } | |
5ff40cf5 | 81 | |
0afe80d5 MM |
82 | // Some big-integer algorithms (albeit on small integers). |
83 | std::cout << gcd(BigUnsigned(60), 72) << '\n' | |
84 | << modinv(BigUnsigned(7), 11) << '\n' | |
85 | << modexp(BigUnsigned(314), 159, 2653) << std::endl; | |
5ff40cf5 | 86 | |
0afe80d5 | 87 | // Add your own code here to experiment with the library. |
b3fe29df | 88 | } catch(char const* err) { |
00c6448a | 89 | std::cout << "The library threw an exception:\n" |
05780f4b MM |
90 | << err << std::endl; |
91 | } | |
5ff40cf5 | 92 | |
05780f4b MM |
93 | return 0; |
94 | } | |
95 | ||
96 | /* | |
0afe80d5 | 97 | The original sample program produces this output: |
05780f4b MM |
98 | |
99 | 3141592653589793238462643383279 | |
100 | 314424 | |
101 | 313894 | |
102 | 83252135 | |
103 | 1185 | |
104 | 134 | |
0551c03b MM |
105 | 0xFF |
106 | 0xFF00FFFF | |
107 | 0xFF00FF00 | |
108 | 0x1FFFE00000 | |
109 | 0x3F | |
00c6448a MM |
110 | 314^0 = 1 |
111 | 314^1 = 314 | |
112 | 314^2 = 98596 | |
113 | 314^3 = 30959144 | |
114 | 314^4 = 9721171216 | |
115 | 314^5 = 3052447761824 | |
116 | 314^6 = 958468597212736 | |
117 | 314^7 = 300959139524799104 | |
118 | 314^8 = 94501169810786918656 | |
119 | 314^9 = 29673367320587092457984 | |
120 | 314^10 = 9317437338664347031806976 | |
0afe80d5 MM |
121 | 12 |
122 | 8 | |
123 | 1931 | |
05780f4b | 124 | |
3e132790 | 125 | */ |