Commit | Line | Data |
---|---|---|
05780f4b MM |
1 | /* |
2 | * Matt McCutchen's Big Integer Library | |
3 | * http://mysite.verizon.net/mccutchen/bigint/ | |
4 | */ | |
5 | ||
6 | /* | |
7 | * This sample file demonstrates the most important features of the Big Integer Library. | |
8 | * | |
9 | * To get started quickly with the library, imitate the code in `main' below. | |
10 | * | |
11 | * If you want more detail or more speed or can't find a feature here, | |
12 | * look in the appropriate source file. This file shows only the more ``user-friendly'' features; | |
13 | * the other features are messier but worth learning eventually. | |
14 | * | |
15 | * GO FORTH and play with many-digit numbers! (c.f. The TeXbook.) | |
16 | */ | |
17 | ||
b3fe29df | 18 | // Standard libraries |
05780f4b MM |
19 | #include <string> |
20 | #include <iostream> | |
21 | ||
b3fe29df MM |
22 | // For the BigInteger class itself. |
23 | #include "BigInteger.hh" | |
24 | ||
25 | // For the 4 routines `easy BI/BU <=> string' and `iostream' integration. | |
26 | #include "BigIntegerUtils.hh" | |
27 | ||
05780f4b MM |
28 | int main() { |
29 | try { | |
30 | BigInteger a; // a is 0 | |
31 | int b = 535; | |
32 | ||
b3fe29df | 33 | a = b; // From int to BigInteger... |
05780f4b MM |
34 | b = a; // ...and back, no casts required! |
35 | /* | |
36 | * If a were too big for an int you'd get a runtime exception. The Big Integer Library | |
37 | * throws C-strings (that is, `const char *'s) when something goes wrong. It's a good | |
b3fe29df MM |
38 | * idea to catch them; the `try/catch' construct wrapping all this code is an example |
39 | * of how to do this. Some C++ compilers need a special command-line option to compile | |
40 | * code that uses exceptions. | |
05780f4b MM |
41 | */ |
42 | ||
b3fe29df | 43 | BigInteger c(a); // Copy a BigInteger. |
05780f4b | 44 | |
e257a1b2 MM |
45 | std::cout << "here 0" << std::endl; |
46 | ||
05780f4b MM |
47 | BigInteger d(-314159265); // c is -314159265. The `int' literal is converted to a BigInteger. |
48 | ||
49 | // Ahem: that's too big to be an `int' literal (or even a `long' literal)! | |
50 | // Disillusion yourself now -- this won't compile. | |
51 | //BigInteger e(3141592653589793238462643383279); | |
52 | ||
e257a1b2 MM |
53 | std::cout << "here 1" << std::endl; |
54 | ||
05780f4b MM |
55 | std::string s("3141592653589793238462643383279"); |
56 | BigInteger f = easyStringToBI(s); | |
57 | // Ah. The string is converted to a BigInteger, and strings can be as long as you want. | |
58 | ||
e257a1b2 MM |
59 | std::cout << "here 2" << std::endl; |
60 | ||
05780f4b MM |
61 | std::string s2 = easyBItoString(f); // You can convert the other way too. |
62 | ||
e257a1b2 MM |
63 | std::cout << "here 3" << std::endl; |
64 | ||
05780f4b MM |
65 | std::cout << f << std::endl; // f is stringified and send to std::cout. |
66 | ||
e257a1b2 MM |
67 | std::cout << "here 4" << std::endl; |
68 | ||
05780f4b MM |
69 | /* |
70 | * Let's do some math! | |
71 | * | |
72 | * The Big Integer Library provides three kinds of operators: | |
73 | * | |
74 | * (1) Overloaded ``value'' operators: +, -, *, /, %, unary -. | |
75 | * Big-integer code using these operators looks identical to | |
76 | * code using the primitive integer types. The operator takes | |
77 | * one or two BigInteger inputs and returns a BigInteger result, | |
78 | * which can then be assigned to a BigInteger variable or used | |
79 | * in an expression. | |
80 | * | |
81 | * (2) Overloaded assignment operators: +=, -=, *=, /=, %=, | |
82 | * ++, --, flipSign. | |
83 | * Again, these are used on BigIntegers just like on ints. | |
84 | * They take one writable BigInteger that both provides an | |
85 | * operand and receives a result. The first five also take | |
86 | * a second read-only operand. | |
87 | * | |
88 | * (3) ``Put-here'' operations: `add', `subtract', etc. | |
89 | * Use these if and only if you are concerned about performance. | |
90 | * They require fewer BigInteger copy-constructions and assignments | |
91 | * than do operators in (1) or (2). Most take two read-only operands | |
92 | * and save the result in the invoked object `*this', whose previous | |
93 | * value is irrelevant. `divideWithRemainder' is an exception. | |
94 | * <<< NOTE >>>: Put-here operations do not return a value: they don't need to!! | |
95 | */ | |
96 | ||
97 | BigInteger g(314159), h(265); | |
98 | // All five ``value'' operators | |
99 | std::cout << (g + h) << '\n' << (g - h) << '\n' << (g * h) | |
100 | << '\n' << (g / h) << '\n' << (g % h) << std::endl; | |
101 | ||
e257a1b2 MM |
102 | std::cout << "here 5" << std::endl; |
103 | ||
05780f4b MM |
104 | BigInteger i(5), j(10), k; |
105 | // These two lines do the same thing: k is set to a BigInteger containing 15. | |
106 | k = i + j; | |
107 | k.add(i, j); | |
108 | ||
e257a1b2 MM |
109 | std::cout << "here 6" << std::endl; |
110 | ||
05780f4b MM |
111 | // Let's do some heavy lifting. |
112 | std::cout << "Powers of 3" << std::endl; | |
113 | std::cout << "How many do you want?" << std::endl; | |
114 | int maxPower; | |
115 | std::cin >> maxPower; | |
116 | ||
117 | BigUnsigned x(1), three(3); | |
118 | for (int power = 0; power <= maxPower; power++) { | |
119 | std::cout << "3^" << power << " = " << x << std::endl; | |
120 | x *= three; // A BigInteger assignment operator | |
121 | } | |
122 | ||
123 | std::cout << "There you go. Goodbye." << std::endl; | |
124 | ||
b3fe29df | 125 | } catch(char const* err) { |
05780f4b MM |
126 | std::cout << "Sorry, the library threw an exception:\n" |
127 | << err << std::endl; | |
128 | } | |
b3fe29df | 129 | |
05780f4b MM |
130 | return 0; |
131 | } | |
132 | ||
133 | /* | |
134 | * Here is the output of a sample run of this sample program: | |
135 | ||
136 | 3141592653589793238462643383279 | |
137 | 314424 | |
138 | 313894 | |
139 | 83252135 | |
140 | 1185 | |
141 | 134 | |
142 | Powers of 3 | |
143 | How many do you want? | |
144 | 2 | |
145 | 3^0 = 1 | |
146 | 3^1 = 3 | |
147 | 3^2 = 9 | |
148 | There you go. Goodbye. | |
149 | ||
150 | */ |