Programming Resources For Fun and Learning

# Factorial

INTEGER.cpp

```//
// INTEGER.cc
// Chuck Cusack, May 1999
//
// This is a class that allows tracking the number of certain operations
// performed.   Useful for comparing the efficiency of algorithms.
//
//------------------------------------------------------------------------
#include "INTEGER.h"
//------------------------------------------------------------------------
// The counters must be allocated and initiaized.
int INTEGER::cop_c = 0;
int INTEGER::cre1_c = 0;
int INTEGER::cre2_c = 0;
int INTEGER::com_c = 0;
int INTEGER::asg_c = 0;
int INTEGER::bit_c = 0;
int INTEGER::sub_c = 0;
int INTEGER::mul_c = 0;
int INTEGER::div_c = 0;
int INTEGER::mod_c = 0;
int INTEGER::inp_c = 0;
int INTEGER::out_c = 0;
int INTEGER::tot_c = 0;
//------------------------------------------------------------------------
// Various operations overloaded so we can keep track
// of the number of times they are performed.
// Assignment
INTEGER& INTEGER::operator=(INTEGER x) {
++INTEGER::asg_c; value = x.value; return *this; };
INTEGER& INTEGER::operator=(int x) {
++INTEGER::asg_c; value = x; return *this;};

//INTEGER*& INTEGER::operator[](INTEGER I) {
//   return *this;
//   }
//const INTEGER*& INTEGER::operator[](INTEGER I) const {
//   return (this+I.value);
//   }

// Comparisions
bool operator<(const INTEGER x,const INTEGER y) {
++INTEGER::com_c; return (x.value < y.value); }
bool operator>(const INTEGER x,const INTEGER y) {
++INTEGER::com_c; return (x.value > y.value); }
bool operator!=(const INTEGER x,const INTEGER y) {
++INTEGER::com_c; return (x.value != y.value); };
bool operator==(const INTEGER x,const INTEGER y) {
++INTEGER::com_c; return (x.value == y.value); };
bool operator>=(const INTEGER x,const INTEGER y) {
++INTEGER::com_c; return (x.value >= y.value); };
bool operator<=(const INTEGER x,const INTEGER y) {
++INTEGER::com_c; return (x.value <= y.value); };

// Binary arithmetic operators
INTEGER operator+(const INTEGER x,const INTEGER y) {
++INTEGER::add_c; return (x.value + y.value); };
INTEGER operator-(const INTEGER x,const INTEGER y) {
++INTEGER::sub_c; return (x.value - y.value); };
INTEGER operator*(const INTEGER x,const INTEGER y) {
++INTEGER::mul_c; return (x.value * y.value); };
INTEGER operator/(const INTEGER x,const INTEGER y) {
++INTEGER::div_c; return (x.value / y.value); };

// Modulo
INTEGER operator%(const INTEGER x,const INTEGER y) {
++INTEGER::mod_c; return (x.value % y.value); };

// Bitwise operators
INTEGER operator<<(const INTEGER x,const INTEGER y) {
++INTEGER::bit_c; return (x.value << y.value); };
INTEGER operator>>(const INTEGER x,const INTEGER y) {
++INTEGER::bit_c; return (x.value >> y.value); };
INTEGER operator&(const INTEGER x,const INTEGER y) {
++INTEGER::bit_c; return (x.value & y.value); };
INTEGER operator^(const INTEGER x,const INTEGER y) {
++INTEGER::bit_c; return (x.value ^ y.value); };
INTEGER operator|(const INTEGER x,const INTEGER y) {
++INTEGER::bit_c; return (x.value | y.value); };
INTEGER INTEGER::operator~() {
++INTEGER::bit_c; return ~value; };

// Unary operators
INTEGER INTEGER::operator++() {
INTEGER INTEGER::operator++(int) {
INTEGER INTEGER::operator--() {
++INTEGER::sub_c; return --value; };
INTEGER INTEGER::operator--(int) {
++INTEGER::sub_c; return value--; };
//INTEGER INTEGER::operator[]() {
//       ++INTEGER::sub_c; return value--; };

// Input/Output
istream &operator>>(istream &thefile,INTEGER &x) {
++INTEGER::inp_c;  thefile>>x.value;
return thefile; };
ostream &operator<<(ostream &thefile,INTEGER x) {
++INTEGER::out_c;  thefile<<x.value;
return thefile; };

//-------------------------------------------------------------------------
// A function to output the various counts.
void INTEGER::print_stats() {
cout << " Copy Constr count = " << INTEGER::cop_c << "\n";
cout << "Deflt Create count = " << INTEGER::cre1_c << "\n";
cout << "Other Create count = " << INTEGER::cre2_c << "\n";
cout << "      Assign count = " << INTEGER::asg_c << "\n";
cout << "        Comp count = " << INTEGER::com_c << "\n";
cout << "     Bitwise count = " << INTEGER::bit_c << "\n";
cout << "         Sub count = " << INTEGER::sub_c << "\n";
cout << "     Muliply count = " << INTEGER::mul_c << "\n";
cout << "    Division count = " << INTEGER::div_c << "\n";
cout << "      Output count = " << INTEGER::out_c<< "\n";
cout << "       Input count = " << INTEGER::inp_c << "\n";
cout << "      Modulo count = " << INTEGER::mod_c << "\n";
cout << "       Total count = " << get_total() << "\n";
cout <<"\n";
}
int INTEGER::get_total() {
INTEGER::tot_c =
INTEGER::cop_c +
INTEGER::cre1_c + INTEGER::cre2_c + INTEGER::asg_c +
INTEGER::com_c + INTEGER::bit_c + INTEGER::add_c +
INTEGER::sub_c + INTEGER::mul_c + INTEGER::div_c +
INTEGER::out_c + INTEGER::inp_c + INTEGER::mod_c;
return INTEGER::tot_c;
}
int INTEGER::get_comps() {
return INTEGER::com_c;
}
void INTEGER::reset_counters() {
INTEGER::cop_c = 0;
INTEGER::cre1_c = 0;
INTEGER::cre2_c = 0;
INTEGER::com_c = 0;
INTEGER::asg_c = 0;
INTEGER::bit_c = 0;