static void Main(string[] args) { /*for (int i = 0; i < args.Length; i++) * { * Console.WriteLine($"args[{i}]={args[i]}"); * } * foreach (var arg in args) * { * Console.WriteLine($""); * }*/ if (args.Length != 1) { throw new Exception("Please only input one number"); } int usrGuess; if (Int32.TryParse(args[0], out usrGuess)) { var hail = new Hailstone(usrGuess); } else { throw new Exception("Please input a number, not whatever you just gave me"); } }
private void btnCalc_Click(object sender, EventArgs e) { // Depending on the number range selected, this could be a long process // so set the cursor to wait this.Cursor = Cursors.WaitCursor; // Set the starting values and objects int currentNumber, startRange, endRange, currentMax; hailstones = new List<Hailstone>(); startRange = int.Parse(tbxStart.Text); endRange = int.Parse(tbxEnd.Text); if (startRange > endRange) { // For fun, use an XOR swap startRange ^= endRange; endRange ^= startRange; startRange ^= endRange; tbxStart.Text = startRange.ToString(); tbxEnd.Text = endRange.ToString(); } Hailstone hailstone; for (currentNumber = startRange; currentNumber <= endRange; currentNumber++) { // Create a new Hailstone and set its starting values hailstone = new Hailstone(); hailstone.number = currentNumber; hailstone.max = currentNumber; hailstone.trips = 1; currentMax = currentNumber; do { // Process the rules (even: n / 2, odd: n * 3 + 1) currentMax = (currentMax % 2 == 0) ? currentMax/2 : 3 * currentMax + 1; // Update maximum reached value hailstone.max = (currentMax > hailstone.max) ? currentMax : hailstone.max; // Increase trip count until 1 is reached hailstone.trips++; } while (currentMax > 1); // Add the new Hailstone to the collection hailstones.Add(hailstone); // Update the progress lblProgress.Text = String.Format("Processing: {0}", currentNumber); Application.DoEvents(); } // Update the graph with the new values lblProgress.Text = "Building Graph..."; Application.DoEvents(); updateGraph(); Application.DoEvents(); // Output the values to a textbox, tab delimited StringBuilder output = new StringBuilder(); foreach (Hailstone item in hailstones) { lblProgress.Text = String.Format("Adding {0} to output...", item.number); Application.DoEvents(); output.Append(String.Format("Number:{0}\t\tTrips:{1}\t\tMax:{2}\r\n", item.number, item.trips, item.max)); } tbxOut.Text = output.ToString(); lblProgress.Text = ""; this.Cursor = Cursors.Default; }