Exemplo n.º 1
0
    public void Bubblesort(double[] data, string[] yearLines, string[] monthLines, int menuChoiceTime, string month, string year, int menuChoiceDo, int menuChoiceOutput)
    {
        //Initalise the temporary variables
        double temp        = 0;
        string tmpYear     = "";
        string tmpMonth    = "";
        int    stepCounter = 0;

        //Initialise object
        output outputObject = new output();

        //Do swapping
        //Sorts 3 lists at once based upon the comparison
        for (int write = 0; write < data.Length; write++)
        {
            for (int sort = 0; sort < data.Length - 1; sort++)
            {
                if (data[sort] > data[sort + 1])
                {
                    //Assign the temporary variables
                    temp     = data[sort + 1];
                    tmpYear  = yearLines[sort + 1];
                    tmpMonth = monthLines[sort + 1];

                    //Swap the data
                    data[sort + 1]       = data[sort];
                    yearLines[sort + 1]  = yearLines[sort];
                    monthLines[sort + 1] = monthLines[sort];

                    //Swap the temp variables back in
                    data[sort]       = temp;
                    yearLines[sort]  = tmpYear;
                    monthLines[sort] = tmpMonth;

                    //Increases the step counter
                    stepCounter++;
                }
            }
        }

        //Reverse the arrays if viewing as descending
        if (menuChoiceDo == 2)
        {
            Array.Reverse(data);
            Array.Reverse(monthLines);
            Array.Reverse(yearLines);
        }

        //Output the steps the algorithm took
        Console.WriteLine("------------------------------------------");
        Console.WriteLine("This Algorithm took {0} steps", stepCounter);
        Console.WriteLine("------------------------------------------");

        //Calls the output class and the output method to output the data
        outputObject.doOutput(menuChoiceOutput, data, menuChoiceTime, year, month, yearLines, monthLines);
    }
Exemplo n.º 2
0
    public void InsertionSort(double[] data, string[] yearLines, string[] monthLines, int menuChoiceTime, string month, string year, int menuChoiceDo, int menuChoiceOutput)
    {
        //Initalise the count variables
        int numSorted = 1;
        int index;
        int stepCounter = 0;

        //Initialise object
        output outputObject = new output();

        while (numSorted < data.Length)
        {
            //Assign the temporary data
            double temp     = data[numSorted];
            string tmpYear  = yearLines[numSorted];
            string tmpMonth = monthLines[numSorted];
            //Do the swapping
            for (index = numSorted; index > 0; index--)
            {
                if (temp < data[index - 1])
                {
                    data[index]       = data[index - 1];
                    yearLines[index]  = yearLines[index - 1];
                    monthLines[index] = monthLines[index - 1];
                    stepCounter++;
                }
                else
                {
                    break;
                }
            }
            //Swap the temp elements back in
            data[index]       = temp;
            yearLines[index]  = tmpYear;
            monthLines[index] = tmpMonth;
            numSorted++;
        }

        //Reverse the array if viewing descending
        if (menuChoiceDo == 2)
        {
            Array.Reverse(data);
            Array.Reverse(monthLines);
            Array.Reverse(yearLines);
        }

        Console.WriteLine("------------------------------------------");
        Console.WriteLine("This Algorithm took {0} steps", stepCounter);
        Console.WriteLine("------------------------------------------");

        //Call the output method to output the data
        outputObject.doOutput(menuChoiceOutput, data, menuChoiceTime, year, month, yearLines, monthLines);
    }
Exemplo n.º 3
0
    public static void Main(string[] args)
    {
        //Creates objects to call functions and methods from
        menuSystem menuObject      = new menuSystem();
        textFiles  textFilesObject = new textFiles();
        sort       algorithmObject = new sort();
        output     outputObject    = new output();

        //Uses the menu class to get the input options from the user
        string weatherStation   = menuObject.getWeatherStation();
        string whatToView       = menuObject.getWhatToView();
        int    menuChoiceTime   = menuObject.getWhatTime();
        int    menuChoiceDo     = menuObject.getWhatToDo();
        int    menuChoiceOutput = menuObject.getOutputType();

        //Initialise  variables
        string year  = "";
        string month = "";

        //Reads in the year and months
        string[] yearLines  = System.IO.File.ReadAllLines(@"CMP1124M_Weather_Data/Year.txt");
        string[] monthLines = System.IO.File.ReadAllLines(@"CMP1124M_Weather_Data/Month.txt");

        //Gets the year and month from the user and validates it
        if (menuChoiceTime == 1)
        {
            year = menuObject.getYear(yearLines);
        }
        else if (menuChoiceTime == 2)
        {
            year  = menuObject.getYear(yearLines);
            month = menuObject.getMonth(year, yearLines, monthLines, menuChoiceTime);
        }
        else if (menuChoiceTime == 4)
        {
            month = menuObject.getMonth(year, yearLines, monthLines, menuChoiceTime);
        }

        //Assigns an array the correct text file based on what the user wants to view
        double[] data = textFilesObject.loadInData(weatherStation, whatToView);

        //Initliases variables
        int menuAlgorithmChoice;

        object[] returnArrays;

        int stepCounter;

        //Switch for doing what the user has specified
        switch (menuChoiceDo)
        {
        //If user chosen to sort
        case 1:
        case 2:
            //Get which sorting algorithm the user wants to use
            menuAlgorithmChoice = menuObject.getSortingAlgorithm();

            //Selects what algorithm to use via a nested switch statement
            switch (menuAlgorithmChoice)
            {
            case 1:     //Use Insertion Sort
                algorithmObject.InsertionSort(data, yearLines, monthLines, menuChoiceTime, month, year, menuChoiceDo, menuChoiceOutput);
                break;

            case 2:     //Use QuickSort

                //Initialise the step counter
                stepCounter = 0;

                returnArrays = algorithmObject.Quicksort(data, 0, data.Length - 1, yearLines, monthLines, menuChoiceTime, month, year, menuChoiceDo, stepCounter);

                //Retrieve the arrays from the object array created by the quicksort function
                data        = (double[])returnArrays[0];
                yearLines   = (string[])returnArrays[1];
                monthLines  = (string[])returnArrays[2];
                stepCounter = (int)returnArrays[3];

                //If the user wants to view the data backwards then reverse the array
                if (menuChoiceDo == 2)
                {
                    Array.Reverse(data);
                    Array.Reverse(monthLines);
                    Array.Reverse(yearLines);
                }

                //output the steps the algorithm took
                Console.WriteLine("This Algorithm took {0} steps", stepCounter);

                //Call the output method
                outputObject.doOutput(menuChoiceOutput, data, menuChoiceTime, year, month, yearLines, monthLines);

                break;

            case 3:     //Use Bubblesort
                algorithmObject.Bubblesort(data, yearLines, monthLines, menuChoiceTime, month, year, menuChoiceDo, menuChoiceOutput);
                break;

            //default class
            default:
                Console.WriteLine("Error");
                break;
            }

            break; //End of outside case 1 and 2

        case 3:    //Find the Median value

            //Initialise the step counter
            stepCounter = 0;

            //Gets the sorted data
            returnArrays = algorithmObject.Quicksort(data, 0, data.Length - 1, yearLines, monthLines, menuChoiceTime, month, year, menuChoiceDo, stepCounter);

            //Retrieves the data from the object as needed to return 3 vairbales
            data        = (double[])returnArrays[0];
            yearLines   = (string[])returnArrays[1];
            monthLines  = (string[])returnArrays[2];
            stepCounter = (int)returnArrays[3];

            //output the steps the algorithm took
            Console.WriteLine("This Algorithm took {0} steps", stepCounter);

            //Call the output method to output the median values
            outputObject.doOutputMedian(menuChoiceOutput, data, menuChoiceTime, year, month, yearLines, monthLines);

            break;

        //Max and Min
        case 4:

            //Initialise the step counter
            stepCounter = 0;

            //Retrieve sorted array
            returnArrays = algorithmObject.Quicksort(data, 0, data.Length - 1, yearLines, monthLines, menuChoiceTime, month, year, menuChoiceDo, stepCounter);

            //Retrieves and assigns individual lists
            data        = (double[])returnArrays[0];
            yearLines   = (string[])returnArrays[1];
            monthLines  = (string[])returnArrays[2];
            stepCounter = (int)returnArrays[3];

            //output the steps the algorithm took
            Console.WriteLine("This Algorithm took {0} steps", stepCounter);

            //Call the output method to output the data
            outputObject.doOutputMinMax(menuChoiceOutput, data, menuChoiceTime, year, month, yearLines, monthLines);

            break;

        case 5: //Sort Chronoligcally
            //Calls the output method to output the data
            outputObject.doOutput(menuChoiceOutput, data, menuChoiceTime, year, month, yearLines, monthLines);
            break;

        case 6: //Sort Chronoligcally Backwards

            //Reveres the arrays to display them from old to new
            Array.Reverse(data);
            Array.Reverse(yearLines);
            Array.Reverse(monthLines);

            //use the output class and the user selected output to output the data
            outputObject.doOutput(menuChoiceOutput, data, menuChoiceTime, year, month, yearLines, monthLines);
            break;

        default:
            Console.WriteLine("Error");
            break;
        }
    }