/*******************************************************************************
* Purpose of this program:  Program to simulate stations ordering based on Mok
* and Wards binary countdown
* protocol.  The program only iterates once so there is no dynamic host
* numbering involved.  The protocol accepts up to 20 station numbers as 3 digit
* intergers. No decimals please.
********************************************************************************
* Functions:
*  	 order:  the heart of the protocol,  performs the ordering.
*
*		 get_stations:  gets the station numbers.  This is a function so that
*      					different methods can be easily interchanged.
*
*		 resize: Resize the taable to throw out any unwanted values, namely
*              those values who do not have the current bit set.
*              Please note that this function will only return if the bithigh
*              value is false,  meaning tat noone has this bit set
*
*		 check: Checks the bit in question and sets bit high to true or false.
*      		 By checks the bit in question meaning does anyone use this bit.
*
*		 error: If there is an invalid input..eg. a station number that is
*             four digits or negative,  other than -1 which is used for
*             controlling the input.
*
*		 main:  is the main body of the program which calls the above functions.
********************************************************************************
*By:  Chuck Hall,  for Dr. K.Kowalski,  CSC451,  Computer networks
*10-97 last update
*******************************************************************************/
//c++ source file.

#include           //include the standard io package.
#include             //We need this for exit();

#define true        1
#define false       0
#define quit        -1
#define firstbit    0x200      //the convention here is firstbit = msb
#define secondbit   0x100      //tenthbit = lsb
#define thirdbit    0x80
#define fourthbit   0x40
#define fifthbit    0x20      //define masks for each bit in the 3 digit number
#define sixthbit    0x10      //a 3 digit int has 10 binary digits.
#define seventhbit  0x8
#define eigthbit    0x4
#define ninthbit    0x2
#define tenthbit    0x1
int count=1;                   //Ok 1 global to maintain the Global count.
int MasterList[20];

typedef int* station_list;    //define types for the station list
typedef short flag;           //and the high bit flag...the bit is set

void get_stations(int &size, station_list &list);
void order(int &size, station_list &list);
void error(){cout<<"\n\nERROR: Values must be between 0 and 999\n\n";}
flag check(station_list templist, int size, int bitnum);
void resize(station_list &templist,int &size, int bitnum, flag bithigh);
void output(int MList[]);

int main()
{
   int size=0;
   station_list slist;


	cout << "\n\nThis program performs stations ordering based on"
        << "\nMok and Wards Binary Countdown Protocol."<> ioBuff;
       if(ioBuff==-1){break;}        //read into the buffer
       if((ioBuff <-1)||(ioBuff > 999)){error();exit(1);}
   	 temp[size] = ioBuff;
       size++;
   }
   if(size==0){return;}
   list = new int[size];
   for(i=0;i<=size;i++)
   {
   	list[i] = temp [i];
   }
}

void order(int &size, station_list &list)
{
	int i;
   flag bithigh=false;
   station_list templist;
   int tempsize, temp;

   if(size == 0){return;}
   else if(size == 1)
   {
   	cout<

    Source: geocities.com/siliconvalley/way/5970

               ( geocities.com/siliconvalley/way)                   ( geocities.com/siliconvalley)