Old snapshot `BigIntegerLibrary-2005.01.18'; see the ChangeLog file.
[bigint/bigint.git] / sample.cc
CommitLineData
05780f4b
MM
1/*
2* Matt McCutchen's Big Integer Library
3* http://mysite.verizon.net/mccutchen/bigint/
4*/
5
6/*
a8b42b68
MM
7* This sample program demonstrates the most important features of the Big Integer Library.
8* To get started quickly, read the code and explanations below. Then try the program out.
05780f4b 9*
a8b42b68
MM
10* If you want more detail or more speed or can't find a feature here, look in the
11* appropriate source file. This file shows only the more ``user-friendly'' features;
05780f4b
MM
12* the other features are messier but worth learning eventually.
13*
14* GO FORTH and play with many-digit numbers! (c.f. The TeXbook.)
15*/
16
b3fe29df 17// Standard libraries
05780f4b
MM
18#include <string>
19#include <iostream>
20
b3fe29df
MM
21// For the BigInteger class itself.
22#include "BigInteger.hh"
23
24// For the 4 routines `easy BI/BU <=> string' and `iostream' integration.
25#include "BigIntegerUtils.hh"
26
05780f4b
MM
27int main() {
28 try {
a8b42b68
MM
29 std::cout << "=====\nBig Integer Library Demonstration" << std::endl;
30
05780f4b
MM
31 BigInteger a; // a is 0
32 int b = 535;
33
b3fe29df 34 a = b; // From int to BigInteger...
05780f4b
MM
35 b = a; // ...and back, no casts required!
36 /*
37 * If a were too big for an int you'd get a runtime exception. The Big Integer Library
38 * throws C-strings (that is, `const char *'s) when something goes wrong. It's a good
b3fe29df
MM
39 * idea to catch them; the `try/catch' construct wrapping all this code is an example
40 * of how to do this. Some C++ compilers need a special command-line option to compile
41 * code that uses exceptions.
05780f4b
MM
42 */
43
b3fe29df 44 BigInteger c(a); // Copy a BigInteger.
05780f4b
MM
45
46 BigInteger d(-314159265); // c is -314159265. The `int' literal is converted to a BigInteger.
47
48 // Ahem: that's too big to be an `int' literal (or even a `long' literal)!
49 // Disillusion yourself now -- this won't compile.
50 //BigInteger e(3141592653589793238462643383279);
51
52 std::string s("3141592653589793238462643383279");
53 BigInteger f = easyStringToBI(s);
54 // Ah. The string is converted to a BigInteger, and strings can be as long as you want.
55
56 std::string s2 = easyBItoString(f); // You can convert the other way too.
57
58 std::cout << f << std::endl; // f is stringified and send to std::cout.
59
60 /*
61 * Let's do some math!
62 *
a8b42b68
MM
63 * The Big Integer Library provides lots of overloaded operators
64 * and corresponding assignment operators. So you can do `a + b'
65 * with big integers just as with normal integers. The named
66 * methods `add', `divideWithRemainder', etc. are more advanced
67 * ``put-here operations''; see `BigUnsigned.hh' for details.
05780f4b 68 */
05780f4b 69 BigInteger g(314159), h(265);
a8b42b68 70 // All five ``return-by-value'' operators.
05780f4b
MM
71 std::cout << (g + h) << '\n' << (g - h) << '\n' << (g * h)
72 << '\n' << (g / h) << '\n' << (g % h) << std::endl;
73
a8b42b68 74 std::cout << "=====\nTest code" << std::endl;
e257a1b2 75
a8b42b68
MM
76 /*
77 * If you want to experiment with the library,
78 * put your own test code here.
79 */
05780f4b 80
a8b42b68
MM
81 /*
82 * (End of test code)
83 */
e257a1b2 84
05780f4b 85 // Let's do some heavy lifting.
a8b42b68 86 std::cout << "=====\nPowers of 3" << std::endl;
05780f4b
MM
87 std::cout << "How many do you want?" << std::endl;
88 int maxPower;
89 std::cin >> maxPower;
90
91 BigUnsigned x(1), three(3);
92 for (int power = 0; power <= maxPower; power++) {
93 std::cout << "3^" << power << " = " << x << std::endl;
94 x *= three; // A BigInteger assignment operator
95 }
96
a8b42b68 97 std::cout << "There you go. Goodbye.\n=====" << std::endl;
05780f4b 98
b3fe29df 99 } catch(char const* err) {
a8b42b68 100 std::cout << "=====\nSorry, the library threw an exception:\n"
05780f4b
MM
101 << err << std::endl;
102 }
b3fe29df 103
05780f4b
MM
104 return 0;
105}
106
107/*
108* Here is the output of a sample run of this sample program:
109
1103141592653589793238462643383279
111314424
112313894
11383252135
1141185
115134
116Powers of 3
117How many do you want?
1182
1193^0 = 1
1203^1 = 3
1213^2 = 9
122There you go. Goodbye.
123
124*/