Decided against the start-of-file comment.
[bigint/bigint.git] / sample.cc
... / ...
CommitLineData
1/*
2 * Sample program demonstrating the most important features of the Big
3 * Integer Library
4 */
5
6// Standard libraries
7#include <string>
8#include <iostream>
9
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
16int main() {
17 try {
18 BigInteger a; // a is 0
19 int b = 535;
20
21 a = b; // From int to BigInteger...
22 b = a; // ...and back, no casts required!
23 /*
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 */
32
33 BigInteger c(a); // Copy a BigInteger.
34
35 // d is -314159265. The `int' literal is converted to a
36 // BigInteger.
37 BigInteger d(-314159265);
38
39 // This won't compile because the number is too big to be an
40 // integer literal.
41 //BigInteger e(3141592653589793238462643383279);
42
43 // Instead you can convert the number from a string.
44 std::string s("3141592653589793238462643383279");
45 BigInteger f = easyStringToBI(s);
46
47 // You can convert the other way too.
48 std::string s2 = easyBItoString(f);
49
50 // f is stringified and send to std::cout.
51 std::cout << f << std::endl;
52
53 /*
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 */
62 BigInteger g(314159), h(265);
63 // All five ``return-by-value'' arithmetic operators.
64 std::cout << (g + h) << '\n' << (g - h) << '\n' << (g * h)
65 << '\n' << (g / h) << '\n' << (g % h) << std::endl;
66
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);
73
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 }
81
82 /*
83 * If you want to experiment with the library,
84 * you can add your own test code here.
85 */
86 // std::cout << "Beginning of custom test code:" << std::endl;
87
88 } catch(char const* err) {
89 std::cout << "The library threw an exception:\n"
90 << err << std::endl;
91 }
92
93 return 0;
94}
95
96/*
97Running the sample program produces this output:
98
993141592653589793238462643383279
100314424
101313894
10283252135
1031185
104134
1050xFF
1060xFF00FFFF
1070xFF00FF00
1080x1FFFE00000
1090x3F
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
121
122 */