Commit | Line | Data |
---|---|---|
05780f4b MM |
1 | /* |
2 | * Matt McCutchen's Big Integer Library | |
05780f4b | 3 | * |
00c6448a MM |
4 | * Sample program demonstrating the most important features of the Big |
5 | * Integer Library | |
05780f4b MM |
6 | */ |
7 | ||
b3fe29df | 8 | // Standard libraries |
05780f4b MM |
9 | #include <string> |
10 | #include <iostream> | |
11 | ||
b3fe29df MM |
12 | // For the BigInteger class itself. |
13 | #include "BigInteger.hh" | |
14 | ||
15 | // For the 4 routines `easy BI/BU <=> string' and `iostream' integration. | |
16 | #include "BigIntegerUtils.hh" | |
17 | ||
05780f4b MM |
18 | int main() { |
19 | try { | |
20 | BigInteger a; // a is 0 | |
21 | int b = 535; | |
5ff40cf5 | 22 | |
b3fe29df | 23 | a = b; // From int to BigInteger... |
05780f4b MM |
24 | b = a; // ...and back, no casts required! |
25 | /* | |
00c6448a MM |
26 | * If a were too big for an int you'd get a runtime exception. |
27 | * The Big Integer Library throws C-strings (that is, | |
28 | * `const char *'s) when something goes wrong. It's a good idea | |
29 | * to catch them; the `try/catch' construct wrapping all this | |
30 | * code is an example of how to do this. Some C++ compilers need | |
31 | * a special command-line option to compile code that uses | |
32 | * exceptions. | |
05780f4b | 33 | */ |
5ff40cf5 | 34 | |
b3fe29df | 35 | BigInteger c(a); // Copy a BigInteger. |
5ff40cf5 | 36 | |
00c6448a MM |
37 | // d is -314159265. The `int' literal is converted to a |
38 | // BigInteger. | |
39 | BigInteger d(-314159265); | |
5ff40cf5 | 40 | |
00c6448a MM |
41 | // This won't compile because the number is too big to be an |
42 | // integer literal. | |
05780f4b | 43 | //BigInteger e(3141592653589793238462643383279); |
5ff40cf5 | 44 | |
00c6448a | 45 | // Instead you can convert the number from a string. |
05780f4b MM |
46 | std::string s("3141592653589793238462643383279"); |
47 | BigInteger f = easyStringToBI(s); | |
5ff40cf5 | 48 | |
00c6448a MM |
49 | // You can convert the other way too. |
50 | std::string s2 = easyBItoString(f); | |
5ff40cf5 | 51 | |
00c6448a MM |
52 | // f is stringified and send to std::cout. |
53 | std::cout << f << std::endl; | |
5ff40cf5 | 54 | |
05780f4b MM |
55 | /* |
56 | * Let's do some math! | |
57 | * | |
a8b42b68 MM |
58 | * The Big Integer Library provides lots of overloaded operators |
59 | * and corresponding assignment operators. So you can do `a + b' | |
00c6448a | 60 | * with BigIntegers just as with normal integers. The named |
a8b42b68 MM |
61 | * methods `add', `divideWithRemainder', etc. are more advanced |
62 | * ``put-here operations''; see `BigUnsigned.hh' for details. | |
05780f4b | 63 | */ |
05780f4b | 64 | BigInteger g(314159), h(265); |
ef2b7c59 | 65 | // All five ``return-by-value'' arithmetic operators. |
05780f4b MM |
66 | std::cout << (g + h) << '\n' << (g - h) << '\n' << (g * h) |
67 | << '\n' << (g / h) << '\n' << (g % h) << std::endl; | |
5ff40cf5 | 68 | |
ef2b7c59 MM |
69 | BigUnsigned i(0xFF0000FF), j(0x0000FFFF); |
70 | // All five ``return-by-value'' bitwise operators. | |
71 | std::cout.flags(std::ios::hex | std::ios::showbase); | |
72 | std::cout << (i & j) << '\n' << (i | j) << '\n' << (i ^ j) << '\n' | |
73 | << (j << 21) << '\n' << (j >> 10) << '\n'; | |
74 | std::cout.flags(std::ios::dec); | |
5ff40cf5 | 75 | |
00c6448a MM |
76 | // Let's do some heavy lifting and calculate powers of 314. |
77 | int maxPower = 10; | |
78 | BigUnsigned x(1), big314(314); | |
79 | for (int power = 0; power <= maxPower; power++) { | |
80 | std::cout << "314^" << power << " = " << x << std::endl; | |
81 | x *= big314; // A BigInteger assignment operator | |
82 | } | |
5ff40cf5 | 83 | |
a8b42b68 MM |
84 | /* |
85 | * If you want to experiment with the library, | |
00c6448a | 86 | * you can add your own test code here. |
a8b42b68 | 87 | */ |
e515a220 | 88 | // std::cout << "Beginning of custom test code:" << std::endl; |
5ff40cf5 | 89 | |
b3fe29df | 90 | } catch(char const* err) { |
00c6448a | 91 | std::cout << "The library threw an exception:\n" |
05780f4b MM |
92 | << err << std::endl; |
93 | } | |
5ff40cf5 | 94 | |
05780f4b MM |
95 | return 0; |
96 | } | |
97 | ||
98 | /* | |
00c6448a | 99 | Running the sample program produces this output: |
05780f4b MM |
100 | |
101 | 3141592653589793238462643383279 | |
102 | 314424 | |
103 | 313894 | |
104 | 83252135 | |
105 | 1185 | |
106 | 134 | |
0551c03b MM |
107 | 0xFF |
108 | 0xFF00FFFF | |
109 | 0xFF00FF00 | |
110 | 0x1FFFE00000 | |
111 | 0x3F | |
00c6448a MM |
112 | 314^0 = 1 |
113 | 314^1 = 314 | |
114 | 314^2 = 98596 | |
115 | 314^3 = 30959144 | |
116 | 314^4 = 9721171216 | |
117 | 314^5 = 3052447761824 | |
118 | 314^6 = 958468597212736 | |
119 | 314^7 = 300959139524799104 | |
120 | 314^8 = 94501169810786918656 | |
121 | 314^9 = 29673367320587092457984 | |
122 | 314^10 = 9317437338664347031806976 | |
05780f4b MM |
123 | |
124 | */ |