#include #include #include #include "HugeInteger.h" using namespace std; // Test out all of the comparison operations void comparison_test(HugeInteger x, HugeInteger y) { cout << "x: " << x << endl; cout << "y: " << y << endl; cout << " == " << x.isEqualTo(y) << endl; cout << " != " << x.isNotEqualTo(y) << endl; cout << " < " << x.isLessThan(y) << endl; cout << " > " << x.isGreaterThan(y) << endl; cout << " <= " << x.isLessThanOrEqualTo(y) << endl; cout << " >= " << x.isGreaterThanOrEqualTo(y) << endl; } // Simple code to test every operation we have // Flipping them around forces us to deal with // integers of varying length, and possible negative results // although for those you only need to return zero // Also, you may get zero which means you should handle division by // zero gracefully void operation_test(HugeInteger x, HugeInteger y) { HugeInteger res; cout << "x: " << x << " y: " << y << endl; res = x.add(y); cout << "x + y: " << res << endl; res = x.subtract(y); cout << "x - y: " << res << endl; res = x.modulus(y); cout << "x % y: " << res << endl; res = x.multiply(y); cout << "x * y: " << res << endl; res = x.divide(y); cout << "x / y: " << res << endl; res = y.add(x); cout << "y + x: " << res << endl; res = y.subtract(x); cout << "y - x: " << res << endl; res = y.modulus(x); cout << "y % x: " << res << endl; res = y.multiply(x); cout << "y * x: " << res << endl; res = y.divide(x); cout << "y / x: " << res << endl; } // Generates a random HugeInteger with num_digits // random digits. Warning! This could possibly generate a 0 HugeInteger random_integer(int num_digits) { char* digit_buf = new char[num_digits+1]; for (int i = 0; i < num_digits; i++) { int digit = rand() % 10; digit_buf[i] = digit + '0'; } digit_buf[num_digits] = '\0'; return HugeInteger(digit_buf); } // This program takes a maximum number of digits to generate and a // number of trials to run. For each trial, it generates up to max_digits // and then runs all of the operations on them. Checking of the // results is currently not done, so you'll have to eyeball them // or write them yourself. int main(int argc, char* argv[]) { srand(time(0)); if (argc < 2) { cerr << "Usage: " << argv[0] << " max_digits num_trials "; exit(1); } int max_digits = atoi(argv[1]); int num_trials = atoi(argv[2]); for (int i = 0; i < num_trials; i++) { int digits1 = rand() % max_digits; int digits2 = rand() % max_digits; HugeInteger h1 = random_integer(digits1); HugeInteger h2 = random_integer(digits2); comparison_test(h1, h2); operation_test(h1, h2); } }