public void SortReceiptBundle() { saveReceiptBundle(); string name = receiptBundleNameInput.value; string receipts = receiptBundles[receiptBundleNameInput.value]; ReceiptBundle rb = ConvertToReceiptBundle(name, receipts); if (rb == null) { Debug.Log("Sort failed. Try it again."); } else { float startTime = Time.realtimeSinceStartup; Sort(ref rb); Debug.Log("Time to sort: " + (Time.realtimeSinceStartup - startTime)); sortSound.Stop(); sortSound.Play(); string rbStr = ConvertReceiptBundleToString(rb); receiptBundleInput.value = rbStr; } }
public ReceiptBundle ConvertToReceiptBundle(string name, string receipts) { string[] splitInput = receipts.Split(new char[] { '\n' }, System.StringSplitOptions.RemoveEmptyEntries); ReceiptBundle rb = new ReceiptBundle(name, new List <Receipt>()); try { for (int i = 0; i < splitInput.Length; i++) { StringBuilder sb = new StringBuilder(); bool spaceFound = false; for (int j = 0; j < splitInput[i].Length; j++) { if (splitInput[i][j] == ' ') { if (!spaceFound) { sb.Append(splitInput[i][j]); } spaceFound = true; } else { spaceFound = false; } if (!spaceFound) { sb.Append(splitInput[i][j]); } } if (sb[0] == ' ') { sb = sb.Remove(0, 1); } //Debug.Log("sb is: ." + sb + "."); //string[] splitLine = Regex.Split(sb.ToString(), @" "); string[] splitLine = sb.ToString().Split(' '); string tName = splitLine[0]; int tMonth = int.Parse(splitLine[1]); int tDay = int.Parse(splitLine[2]); float tPrice = float.Parse(splitLine[3]); Receipt r = new Receipt(tName, tMonth, tDay, tPrice); rb.AddReceipt(r); } } catch { //Debug.Log("at index: " + i); //splitLine.print(); //Debug.Log("error sb is: ." + sb + "."); Debug.Log("error sorting!"); rb = null; } return(rb); }
public static void printJustMonths(this ReceiptBundle rb) { for (int i = 0; i < rb.receipts.Count; i++) { Debug.Log("Start Month:"); Debug.Log("Month: " + rb.receipts[i].month); Debug.Log("End Month:"); } }
public static void print(this ReceiptBundle rb) { for (int i = 0; i < rb.receipts.Count; i++) { Debug.Log("Start Receipt:"); Debug.Log("Name: " + rb.receipts[i].name); Debug.Log("Month: " + rb.receipts[i].month); Debug.Log("Day: " + rb.receipts[i].day); Debug.Log("Price: " + rb.receipts[i].price); Debug.Log("End Receipt:"); } }
/* * public int[] MergeSort(int[] numbers) * { * * if (numbers.Length > 1) * { * int[] firsthalf = new int[numbers.Length / 2]; * for (int i = 0; i < firsthalf.Length; i++) * { * firsthalf[i] = numbers[i]; * } * * int[] secondhalf = new int[numbers.Length - firsthalf.Length]; * for (int i = 0; i < secondhalf.Length; i++) * { * secondhalf[i] = numbers[firsthalf.Length + i]; * } * * * firsthalf = MergeSort(firsthalf); * secondhalf = MergeSort(secondhalf); * numbers = Merge(firsthalf, secondhalf); * } * return numbers; * } * * public static int[] Merge(int[] first, int[] second) * { * * int totalLength = first.Length + second.Length; * int[] answer = new int[totalLength]; * int firstcounter = 0; * int secondcounter = 0; * * for (int i = 0; i < answer.Length; i++) * { * * // Deciding whether to take next smallest number from * // first array or second. * if ((secondcounter == second.Length) || ((firstcounter < first.Length) && (first[firstcounter] < second[secondcounter])) ) * { * answer[i] = first[firstcounter]; * firstcounter++; * } * else * { * answer[i] = second[secondcounter]; * secondcounter++; * } * } * return answer; * }*/ public string ConvertReceiptBundleToString(ReceiptBundle rb) { StringBuilder sb = new StringBuilder(); int max = rb.receipts.Count; for (int i = 0; i < max; i++) { sb.Append(rb.receipts[i].toString()); } return(sb.ToString()); }
public void Sort(ref ReceiptBundle rb) { for (int i = 0; i < rb.receipts.Count; i++) { for (int j = 0; j < rb.receipts.Count - 1; j++) { if (rb.receipts[j].month > rb.receipts[j + 1].month) { rb.SwapReceipts(j); } else if (rb.receipts[j].month == rb.receipts[j + 1].month) { if (rb.receipts[j].day > rb.receipts[j + 1].day) { rb.SwapReceipts(j); } } } } }