| 1 | /* |
| 2 | * Matt McCutchen's Big Integer Library |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * This sample program demonstrates the most important features of the Big Integer Library. |
| 7 | * To get started quickly, read the code and explanations below. Then try the program out. |
| 8 | * |
| 9 | * If you want more detail or more speed or can't find a feature here, look in the |
| 10 | * appropriate source file. This file shows only the more ``user-friendly'' features; |
| 11 | * the other features are messier but worth learning eventually. |
| 12 | * |
| 13 | * GO FORTH and play with many-digit numbers! (c.f. The TeXbook.) |
| 14 | */ |
| 15 | |
| 16 | // Standard libraries |
| 17 | #include <string> |
| 18 | #include <iostream> |
| 19 | |
| 20 | // For the BigInteger class itself. |
| 21 | #include "BigInteger.hh" |
| 22 | |
| 23 | // For the 4 routines `easy BI/BU <=> string' and `iostream' integration. |
| 24 | #include "BigIntegerUtils.hh" |
| 25 | |
| 26 | int main() { |
| 27 | try { |
| 28 | std::cout << "=====\nBig Integer Library Demonstration" << std::endl; |
| 29 | |
| 30 | BigInteger a; // a is 0 |
| 31 | int b = 535; |
| 32 | |
| 33 | a = b; // From int to BigInteger... |
| 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 |
| 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. |
| 41 | */ |
| 42 | |
| 43 | BigInteger c(a); // Copy a BigInteger. |
| 44 | |
| 45 | BigInteger d(-314159265); // c is -314159265. The `int' literal is converted to a BigInteger. |
| 46 | |
| 47 | // Ahem: that's too big to be an `int' literal (or even a `long' literal)! |
| 48 | // Disillusion yourself now -- this won't compile. |
| 49 | //BigInteger e(3141592653589793238462643383279); |
| 50 | |
| 51 | std::string s("3141592653589793238462643383279"); |
| 52 | BigInteger f = easyStringToBI(s); |
| 53 | // Ah. The string is converted to a BigInteger, and strings can be as long as you want. |
| 54 | |
| 55 | std::string s2 = easyBItoString(f); // You can convert the other way too. |
| 56 | |
| 57 | std::cout << f << std::endl; // f is stringified and send to std::cout. |
| 58 | |
| 59 | /* |
| 60 | * Let's do some math! |
| 61 | * |
| 62 | * The Big Integer Library provides lots of overloaded operators |
| 63 | * and corresponding assignment operators. So you can do `a + b' |
| 64 | * with big integers just as with normal integers. The named |
| 65 | * methods `add', `divideWithRemainder', etc. are more advanced |
| 66 | * ``put-here operations''; see `BigUnsigned.hh' for details. |
| 67 | */ |
| 68 | BigInteger g(314159), h(265); |
| 69 | // All five ``return-by-value'' operators. |
| 70 | std::cout << (g + h) << '\n' << (g - h) << '\n' << (g * h) |
| 71 | << '\n' << (g / h) << '\n' << (g % h) << std::endl; |
| 72 | |
| 73 | std::cout << "=====\nTest code" << std::endl; |
| 74 | |
| 75 | /* |
| 76 | * If you want to experiment with the library, |
| 77 | * put your own test code here. |
| 78 | */ |
| 79 | |
| 80 | /* |
| 81 | * (End of test code) |
| 82 | */ |
| 83 | |
| 84 | // Let's do some heavy lifting. |
| 85 | std::cout << "=====\nPowers of 3" << std::endl; |
| 86 | std::cout << "How many do you want?" << std::endl; |
| 87 | int maxPower; |
| 88 | std::cin >> maxPower; |
| 89 | |
| 90 | BigUnsigned x(1), three(3); |
| 91 | for (int power = 0; power <= maxPower; power++) { |
| 92 | std::cout << "3^" << power << " = " << x << std::endl; |
| 93 | x *= three; // A BigInteger assignment operator |
| 94 | } |
| 95 | |
| 96 | std::cout << "There you go. Goodbye.\n=====" << std::endl; |
| 97 | |
| 98 | } catch(char const* err) { |
| 99 | std::cout << "=====\nSorry, the library threw an exception:\n" |
| 100 | << err << std::endl; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Here is the output of a sample run of this sample program: |
| 108 | |
| 109 | 3141592653589793238462643383279 |
| 110 | 314424 |
| 111 | 313894 |
| 112 | 83252135 |
| 113 | 1185 |
| 114 | 134 |
| 115 | Powers of 3 |
| 116 | How many do you want? |
| 117 | 2 |
| 118 | 3^0 = 1 |
| 119 | 3^1 = 3 |
| 120 | 3^2 = 9 |
| 121 | There you go. Goodbye. |
| 122 | |
| 123 | */ |