Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python

C++


JAVA
PHP
SQL
Alice

Factorial


INTEGER.h

//
// INTEGER.h
// Chuck Cusack, May 1999.  Revised November 1999
//
// This is a class that allows tracking the number of certain operations
// performed.   Useful for comparing the efficiency of algorithms.
//
//------------------------------------------------------------------------
#ifndef INTEGER_H
#define INTEGER_H
//------------------------------------------------------------------------
#include <fstream>
#include <iostream>
using namespace std;
//------------------------------------------------------------------------
class INTEGER {
protected:
int value;             // The value stored

static int cop_c;      // Copy Constructor
static int cre1_c;      // Default Creation 
static int cre2_c;      // Other Creation 
static int com_c;      // Compare ( < , > , == , etc ) 
static int asg_c;      // Copy and assignnment 
static int bit_c;      // bitwise operators 
static int add_c;      // Addition and Increment 
static int sub_c;      // Subtraction and decrement 
static int mul_c;      // Multiplication 
static int div_c;      // Division 
static int mod_c;      // modulo 
static int out_c;      // output 
static int inp_c;      // input 
static int tot_c;      // total 

public:

// 
//
INTEGER() : value(0) {cre1_c++;};                    // Default constructor
INTEGER(int x) : value(x) { cre2_c++; };          // Another constructor

// Well, I WILL count them, right or wrong.
//
// //Don't count these operations.  The copy constructor seems to be
// //called quite a bit, and it's not clear how it should be counted, if at all.
//
// The statement 
// INTEGER X=Y;  
// does NOT call the assign, but the copy constructor.
// Thus, it can be done for "free"
//
INTEGER(const INTEGER& x) { 
      value = x.value; 
      cop_c++;
      };   // Copy constructor 
 
                   // Various operations overloaded so we can keep track 
                   // of the number of times they are performed.

        // Used to cast an INTEGER to an int.
unsigned int operator()() const { asg_c++; return value; };

         // Assignment 
INTEGER& operator=(INTEGER x);
INTEGER& operator=(int x);

        // The [] overloaded
//INTEGER *&operator[](INTEGER I);
//const INTEGER *&operator[](INTEGER I) const;

      // Comparisons
friend bool operator<(const INTEGER x,const INTEGER y);
friend bool operator>(const INTEGER x,const INTEGER y);
friend bool operator!=(const INTEGER x,const INTEGER y);
friend bool operator==(const INTEGER x,const INTEGER y);
friend bool operator>=(const INTEGER x,const INTEGER y);
friend bool operator<=(const INTEGER x,const INTEGER y);


       // Arithmetic 
friend INTEGER operator+(const INTEGER x,const INTEGER y);
friend INTEGER operator-(const INTEGER x,const INTEGER y);
friend INTEGER operator*(const INTEGER x,const INTEGER y);
friend INTEGER operator/(const INTEGER x,const INTEGER y);

       // Modulo
friend INTEGER operator%(const INTEGER x,const INTEGER y);

        // bitwise operators
friend INTEGER operator<<(const INTEGER x,const INTEGER y);
friend INTEGER operator>>(const INTEGER x,const INTEGER y);
friend INTEGER operator&(const INTEGER x,const INTEGER y);
friend INTEGER operator|(const INTEGER x,const INTEGER y);
friend INTEGER operator^(const INTEGER x,const INTEGER y);
INTEGER operator~();

        // Unary operators
INTEGER operator++();
INTEGER operator++(int);
INTEGER operator--();
INTEGER operator--(int);
//INTEGER operator[](int);

         // Input/Output
friend istream &operator>>(istream &thefile,INTEGER &x);
friend ostream &operator<<(ostream &thefile,INTEGER x);

static void print_stats();     // A function to output the various counts.
static int get_total();        // A function to output the total count.
static int get_comps();        // A function to output the compare count.
static void reset_counters();    // reset the counters all to 0.
};
//-------------------------------------------------------------------------
void Swap(INTEGER &x, INTEGER &y);
//-------------------------------------------------------------------------
#endif