prog8.cpp

Published in DevCode 2001. Original code and wording are preserved.

// Program 8
// April 24, 2003
#include<iostream>
#include<vector>
#include<queue>
#include<string>
#include<fstream>
using namespace std;


int Find_ith_Digit(int number, int i) // Function finds the ith Digit
{
    int Return_Value;
    switch(i)
    {
    case 1:
        Return_Value = number%10;
        return Return_Value;
        break;
    case 2:
        Return_Value = (number%100)/10;
        return Return_Value;
        break;
    case 3:
        Return_Value = (number%1000)/100;
        return Return_Value;
        break;
    case 4:
        Return_Value = (number%10000)/1000;
        return Return_Value;
        break;
    case 5:
        Return_Value = (number%100000)/10000;
        return Return_Value;
        break;
    };

}

void main()
{


    queue <int> masterlist;
    vector <queue<int> > Array_Queue (10);
    int number, number2, digit;

    string filename = "numbers.txt";
    ifstream input(filename.c_str());

    while(input >> number)
        masterlist.push(number);    // inputs each number into the masterlist queue

    for(int i = 1; i <= 5; i++)
    {
        while(masterlist.empty()!=true) // If masterlist is not empty
        {
            number=masterlist.front();  // number = the first value in the queue masterlist
            masterlist.pop(); // pops of the first value
            digit=Find_ith_Digit(number, i); // returns the ith number to 'digit'
            Array_Queue[digit].push(number); //pushes the value 'number' in the array of queues at the 'digit' position
        }
        for(int j = 0; j<=9; j++) // This loop goes once the values in the array are sorted
        {
            while(Array_Queue[j].empty()!=true) //while the queue in the array at position j is not empty
            {
                number2 = Array_Queue[j].front(); // number2 is assigned the value of the first value in the queue in the array at position j
                Array_Queue[j].pop(); // gets rid of the first number
                masterlist.push(number2); //pushes the number into the the orginal queue sorted
            }
        }
    }

    while(masterlist.empty()!=true) //prints out the queue
    {
        cout <<masterlist.front() << endl;
        masterlist.pop();
    }
}