//static method which imports the data depending on the users input with args j private static void inputFiles(string j) { string fileName = string.Format(@"sorting_files\Year_{0}.txt", j); for (int i = 0; i < File.ReadAllLines(fileName).Length; i++) { fileName = string.Format(@"sorting_files\Year_{0}.txt", j); string year = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Month_{0}.txt", j); string month = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Day_{0}.txt", j); string day = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Time_{0}.txt", j); string time = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Magnitude_{0}.txt", j); string magnitude = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Latitude_{0}.txt", j); string latitude = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Longitude_{0}.txt", j); string longitude = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Depth_{0}.txt", j); string depth = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Region_{0}.txt", j); string region = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\IRIS_ID_{0}.txt", j); string iris = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Timestamp_{0}.txt", j); string timestamp = File.ReadLines(fileName).Skip(i).Take(1).First(); Seismic s = new Seismic(year, month, day, time, magnitude, latitude, longitude, depth, region, iris, timestamp); //creates a new Seismic object from the imported data _dataList.Add(s); //Add object to the data list } }
private static void inputFiles(string j) { string fileName = string.Format(@"sorting_files\Year_{0}.txt", j); for (int i = 0; i < File.ReadAllLines(fileName).Length; i++) { fileName = string.Format(@"sorting_files\Year_{0}.txt", j); string year = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Month_{0}.txt", j); string month = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Day_{0}.txt", j); string day = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Time_{0}.txt", j); string time = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Magnitude_{0}.txt", j); string magnitude = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Latitude_{0}.txt", j); string latitude = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Longitude_{0}.txt", j); string longitude = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Depth_{0}.txt", j); string depth = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Region_{0}.txt", j); string region = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\IRIS_ID_{0}.txt", j); string iris = File.ReadLines(fileName).Skip(i).Take(1).First(); fileName = string.Format(@"sorting_files\Timestamp_{0}.txt", j); string timestamp = File.ReadLines(fileName).Skip(i).Take(1).First(); DateTime dateStamp = new DateTime(); dateStamp = DateTime.Parse(string.Format("{0} {1} {2} {3}", day, month, year, time)); Seismic s = new Seismic(year, month, day, time, magnitude, latitude, longitude, depth, region, iris, timestamp, dateStamp); _dataList.Add(s); } }
//public method which compares an object with dateStamp, used in sorting public int CompareTo(object obj) { if (obj == null) //if the comparing object doesn't have an inherent value { return(1); } Seismic other = obj as Seismic; //Sets other to to be the argument object Seismic if (other != null) //Runs if the argument has a value { switch (Sorter.key) { case 0: return(this.dateStamp.CompareTo(other.dateStamp)); case 1: return(this.year.CompareTo(other.year)); case 2: return(this.dateStamp.Month.CompareTo(other.dateStamp.Month)); case 3: return(this.day.CompareTo(other.day)); case 4: return(this.dateStamp.TimeOfDay.CompareTo(other.dateStamp.TimeOfDay)); case 5: return(this.magnitude.CompareTo(other.magnitude)); case 6: return(this.latitude.CompareTo(other.latitude)); case 7: return(this.longitude.CompareTo(other.longitude)); case 8: return(this.depth.CompareTo(other.depth)); case 9: return(this.region.CompareTo(other.region)); case 10: return(this.iris.CompareTo(other.iris)); case 11: return(this.timestamp.CompareTo(other.timestamp)); default: return(0); } } else //returns error message if it doesn't understand what the object is { throw new ArgumentException("Object is not a Seismic"); } }
//Quicksort method public static void quickSort(List <Seismic> dataObjects, int left, int right, bool isDescending) { int i = left, j = right; Seismic pivot = dataObjects.ElementAt((left + right) / 2); while (i <= j) //while loop runs left and right are not set as each other { if (isDescending) //depends on users input for asc or desc { while (dataObjects.ElementAt(i).CompareTo(pivot) > 0) { quickSortCount++; i++; } } else { while (dataObjects.ElementAt(i).CompareTo(pivot) < 0) { quickSortCount++; i++; } } if (isDescending) //depends on users input for asc or desc { while (dataObjects.ElementAt(j).CompareTo(pivot) < 0) { quickSortCount++; //increases the operations count by 1 j--; } } else { while (dataObjects.ElementAt(j).CompareTo(pivot) > 0) { quickSortCount++; //increases the operations count by 1 j--; } } if (i <= j) { // Swap //creates temporary object tmp to swap the objects through Seismic tmp = dataObjects.ElementAt(i); dataObjects[i] = dataObjects.ElementAt(j); dataObjects[j] = tmp; i++; j--; } } //recursive methods if (left < j) { quickSort(dataObjects, left, j, isDescending); //Calls quicksort again } if (i < right) { quickSort(dataObjects, i, right, isDescending); //Calls quicksort again } }
//static method which finds the minimum and maximum of the imported data //this is done through a linear search private static void printMinMax() { //variable declaring at the top of the method string floatMax = float.MaxValue.ToString(); string intMax = int.MaxValue.ToString(); int minYear = 9999, maxYear = 1, minMonth = 12, maxMonth = 1, minDay = 31, maxDay = 1, minHours = 23, maxHours = 0, minMinutes = 59, maxMinutes = 0, minSeconds = 59, maxSeconds = 0, minIris = int.MaxValue, maxIris = 0, minTimestamp = int.MaxValue, maxTimestamp = 0; float minMagnitude = float.MaxValue, maxMagnitude = 0, minLatitude = float.MaxValue, maxLatitude = 0, minLongitude = float.MaxValue, maxLongitude = 0, minDepth = float.MaxValue, maxDepth = 0; string minRegion = "~", maxRegion = ""; int regionCompare; for (int i = 0; i < _dataList.Count(); i++) //For loop which runs for each of the entries in the data list of objects { Seismic tmp = _dataList.ElementAt(i); //tmp = Current data object if (tmp.dateStamp.Year > maxYear) { maxYear = tmp.dateStamp.Year; } if (tmp.dateStamp.Year < minYear) { minYear = tmp.dateStamp.Year; } if (tmp.dateStamp.Month > maxMonth) { maxMonth = tmp.dateStamp.Month; } if (tmp.dateStamp.Month < minMonth) { minMonth = tmp.dateStamp.Month; } if (tmp.dateStamp.Day > maxDay) { maxDay = tmp.dateStamp.Day; } if (tmp.dateStamp.Day < minDay) { minDay = tmp.dateStamp.Day; } if (tmp.dateStamp.Hour > maxHours) { maxHours = tmp.dateStamp.Hour; maxMinutes = tmp.dateStamp.Minute; maxSeconds = tmp.dateStamp.Second; } if (tmp.dateStamp.Hour == maxHours) { if (tmp.dateStamp.Minute > maxMinutes) { maxMinutes = tmp.dateStamp.Minute; maxSeconds = tmp.dateStamp.Second; } if (tmp.dateStamp.Minute == maxMinutes) { if (tmp.dateStamp.Second > maxSeconds) { maxSeconds = tmp.dateStamp.Second; } } } regionCompare = String.CompareOrdinal(tmp.region, minRegion); if (regionCompare < 0) { minRegion = tmp.region; } regionCompare = String.CompareOrdinal(tmp.region, maxRegion); if (regionCompare > 0) { maxRegion = tmp.region; } if (tmp.dateStamp.Hour < minHours) { minHours = tmp.dateStamp.Hour; minMinutes = tmp.dateStamp.Minute; minSeconds = tmp.dateStamp.Second; } if (tmp.dateStamp.Hour == minHours) { if (tmp.dateStamp.Minute < minMinutes) { minMinutes = tmp.dateStamp.Minute; minSeconds = tmp.dateStamp.Second; } if (tmp.dateStamp.Minute == minMinutes) { if (tmp.dateStamp.Second < minSeconds) { minSeconds = tmp.dateStamp.Second; } } } if (tmp.magnitude > maxMagnitude) { maxMagnitude = tmp.magnitude; } if (tmp.magnitude < minMagnitude) { minMagnitude = tmp.magnitude; } if (tmp.latitude > maxLatitude) { maxLatitude = tmp.latitude; } if (tmp.latitude < minLatitude) { minLatitude = tmp.latitude; } if (tmp.longitude > maxLongitude) { maxLongitude = tmp.longitude; } if (tmp.longitude < minLongitude) { minLongitude = tmp.longitude; } if (tmp.depth > maxDepth) { maxDepth = tmp.depth; } if (tmp.depth < minDepth) { minDepth = tmp.depth; } if (tmp.iris > maxIris) { maxIris = tmp.iris; } if (tmp.iris < minIris) { minIris = tmp.iris; } if (tmp.timestamp > maxTimestamp) { maxTimestamp = tmp.timestamp; } if (tmp.timestamp < minTimestamp) { minTimestamp = tmp.timestamp; } } //string array which will convert the DateTime output into a month string string[] months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; //Console printout for minimum and maximum values found in the linear search Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("\n\n|==================================================|Minimum Values in data set {0,-7}|================================================|", whatSet); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("|Year |Month |Day |Time |Magni |Lati |Long |Depth |Region |Iris |Timestamp |"); Console.WriteLine("|-------|------------|-------|----------|-------|---------|---------|----------|--------------------------------|-----------|----------|"); Console.WriteLine("|{0,-5} |{1,-10} |{2,-5} |{3,-5} |{4,-5} |{5,-7} |{6,-7} |{7,-8} |{8,-30} |{9,-10} |{10,-10}|", minYear, months[minMonth - 1], minDay, String.Format("{0:00}:{1:00}:{2:00}", minHours, minMinutes, minSeconds), String.Format("{0:0.000}", minMagnitude), String.Format("{0:0.000}", minLatitude), String.Format("{0:0.000}", minLongitude), String.Format("{0:0.000}", minDepth), minRegion, minIris, minTimestamp); Console.WriteLine("|______________________________________________________________________________________________________________________________________|"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("\n\n|==================================================|Maximum Values in data set {0,-7}|================================================|", whatSet); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("|Year |Month |Day |Time |Magni |Lati |Long |Depth |Region |Iris |Timestamp |"); Console.WriteLine("|-------|------------|-------|----------|-------|---------|---------|----------|--------------------------------|-----------|----------|"); Console.WriteLine("|{0,-5} |{1,-10} |{2,-5} |{3,-5} |{4,-5} |{5,-7} |{6,-7} |{7,-8} |{8,-30} |{9,-10} |{10,-10}|", maxYear, months[maxMonth - 1], maxDay, String.Format("{0:00}:{1:00}:{2:00}", maxHours, maxMinutes, maxSeconds), String.Format("{0:0.000}", maxMagnitude), String.Format("{0:0.000}", maxLatitude), String.Format("{0:0.000}", maxLongitude), String.Format("{0:0.000}", maxDepth), maxRegion, maxIris, maxTimestamp); Console.WriteLine("|______________________________________________________________________________________________________________________________________|"); }
static void Main(string[] args) { printStart(); Console.WriteLine("Please choose an Array!"); System.Threading.Thread.Sleep(100); Console.WriteLine("A : Year"); System.Threading.Thread.Sleep(100); Console.WriteLine("B : Month"); System.Threading.Thread.Sleep(100); Console.WriteLine("C : Day"); System.Threading.Thread.Sleep(100); Console.WriteLine("D : Time (UTC)"); System.Threading.Thread.Sleep(100); Console.WriteLine("E : Magnitude"); System.Threading.Thread.Sleep(100); Console.WriteLine("F : Latitude"); System.Threading.Thread.Sleep(100); Console.WriteLine("G : Longitude"); System.Threading.Thread.Sleep(100); Console.WriteLine("H : Depth"); System.Threading.Thread.Sleep(100); Console.WriteLine("I : Region"); System.Threading.Thread.Sleep(100); Console.WriteLine("J : IRIS_ID"); System.Threading.Thread.Sleep(100); Console.WriteLine("K : Timestamp\n"); System.Threading.Thread.Sleep(100); Console.Write("Array letter: "); _whichArray = Console.ReadLine().ToLower(); if (_whichArray == "a") { _filePath = @"sorting_files\Year_1.txt"; } else if (_whichArray == "b") { _filePath = @"sorting_files\Month_1.txt"; } else if (_whichArray == "c") { _filePath = @"sorting_files\Day_1.txt"; } else if (_whichArray == "d") { _filePath = @"sorting_files\Time_1.txt"; } else if (_whichArray == "e") { _filePath = @"sorting_files\Magnitude_1.txt"; } else if (_whichArray == "f") { _filePath = @"sorting_files\Latitude_1.txt"; } else if (_whichArray == "g") { _filePath = @"sorting_files\Longitude_1.txt"; } else if (_whichArray == "h") { _filePath = @"sorting_files\Depth_1.txt"; } else if (_whichArray == "i") { _filePath = @"sorting_files\Region_1.txt"; } else if (_whichArray == "j") { _filePath = @"sorting_files\IRIS_ID_1.txt"; } else if (_whichArray == "k") { _filePath = @"sorting_files\Timestamp_1.txt"; } else { Console.WriteLine("Please enter a valid Array letter"); } Console.WriteLine("\nImporting data from {0}...", _filePath); _dataArray = File.ReadAllLines(_filePath); foreach (string d in _dataArray) { Console.WriteLine(d); } Console.WriteLine("Please enter what you want to find"); string userFind = Console.ReadLine(); Seismic.load(_filePath, userFind); Seismic.printData(); }