Append _H to anti-multiple-inclusion macros.
[bigint/bigint.git] / sample.cc
CommitLineData
05780f4b 1/*
6e1e0f2f
MM
2 * Sample program demonstrating the most important features of the Big
3 * Integer Library
4 */
05780f4b 5
b3fe29df 6// Standard libraries
05780f4b
MM
7#include <string>
8#include <iostream>
9
b3fe29df
MM
10// For the BigInteger class itself.
11#include "BigInteger.hh"
12
13// For the 4 routines `easy BI/BU <=> string' and `iostream' integration.
14#include "BigIntegerUtils.hh"
15
05780f4b
MM
16int main() {
17 try {
18 BigInteger a; // a is 0
19 int b = 535;
5ff40cf5 20
b3fe29df 21 a = b; // From int to BigInteger...
05780f4b
MM
22 b = a; // ...and back, no casts required!
23 /*
6e1e0f2f
MM
24 * If a were too big for an int you'd get a runtime exception.
25 * The Big Integer Library throws C-strings (that is,
26 * `const char *'s) when something goes wrong. It's a good idea
27 * to catch them; the `try/catch' construct wrapping all this
28 * code is an example of how to do this. Some C++ compilers need
29 * a special command-line option to compile code that uses
30 * exceptions.
31 */
5ff40cf5 32
b3fe29df 33 BigInteger c(a); // Copy a BigInteger.
5ff40cf5 34
00c6448a
MM
35 // d is -314159265. The `int' literal is converted to a
36 // BigInteger.
37 BigInteger d(-314159265);
5ff40cf5 38
00c6448a
MM
39 // This won't compile because the number is too big to be an
40 // integer literal.
05780f4b 41 //BigInteger e(3141592653589793238462643383279);
5ff40cf5 42
00c6448a 43 // Instead you can convert the number from a string.
05780f4b
MM
44 std::string s("3141592653589793238462643383279");
45 BigInteger f = easyStringToBI(s);
5ff40cf5 46
00c6448a
MM
47 // You can convert the other way too.
48 std::string s2 = easyBItoString(f);
5ff40cf5 49
00c6448a
MM
50 // f is stringified and send to std::cout.
51 std::cout << f << std::endl;
5ff40cf5 52
05780f4b 53 /*
6e1e0f2f
MM
54 * Let's do some math!
55 *
56 * The Big Integer Library provides lots of overloaded operators
57 * and corresponding assignment operators. So you can do `a + b'
58 * with BigIntegers just as with normal integers. The named
59 * methods `add', `divideWithRemainder', etc. are more advanced
60 * ``put-here operations''; see `BigUnsigned.hh' for details.
61 */
05780f4b 62 BigInteger g(314159), h(265);
ef2b7c59 63 // All five ``return-by-value'' arithmetic operators.
05780f4b
MM
64 std::cout << (g + h) << '\n' << (g - h) << '\n' << (g * h)
65 << '\n' << (g / h) << '\n' << (g % h) << std::endl;
5ff40cf5 66
ef2b7c59
MM
67 BigUnsigned i(0xFF0000FF), j(0x0000FFFF);
68 // All five ``return-by-value'' bitwise operators.
69 std::cout.flags(std::ios::hex | std::ios::showbase);
70 std::cout << (i & j) << '\n' << (i | j) << '\n' << (i ^ j) << '\n'
71 << (j << 21) << '\n' << (j >> 10) << '\n';
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
a8b42b68 82 /*
6e1e0f2f
MM
83 * If you want to experiment with the library,
84 * you can add your own test code here.
85 */
e515a220 86 // std::cout << "Beginning of custom test code:" << std::endl;
5ff40cf5 87
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/*
00c6448a 97Running the sample program produces this output:
05780f4b
MM
98
993141592653589793238462643383279
100314424
101313894
10283252135
1031185
104134
0551c03b
MM
1050xFF
1060xFF00FFFF
1070xFF00FF00
1080x1FFFE00000
1090x3F
00c6448a
MM
110314^0 = 1
111314^1 = 314
112314^2 = 98596
113314^3 = 30959144
114314^4 = 9721171216
115314^5 = 3052447761824
116314^6 = 958468597212736
117314^7 = 300959139524799104
118314^8 = 94501169810786918656
119314^9 = 29673367320587092457984
120314^10 = 9317437338664347031806976
05780f4b 121
6e1e0f2f 122 */