using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp7 { public partial class Form1 : Form { public static int TooLargeCounter = 0; public static Boolean replace = false; public static Boolean resetExpression = false; public static Nullable leftOp = null; public static Int64 rightOp; public static Nullable operation = null; public Form1() { InitializeComponent(); DivideOp_Button.Text = "\u00F7"; } public Int64 ConvertOutputToLong() { while (OutputText.Text.Contains(',')) { OutputText.Text = OutputText.Text.Remove(OutputText.Text.IndexOf(','), 1); } return Convert.ToInt64(OutputText.Text); } public void NumericKey_Click(object sender, EventArgs args) { Button alpha = sender as Button; if (alpha == null) // The typecast failed return; // This... probably shouldn't ever happen anyway if (OutputText.Text.Length > 14) { TooLargeCounter++; if (TooLargeCounter % 3 == 0) { MessageBox.Show("Sorry, this value is too large to be stored in memory."); } return; } if (replace) { OutputText.Text = ""; replace = false; } if (OutputText.Text.CompareTo("0") == 0) { OutputText.Text = ""; } if (resetExpression) { OutputText.Text = ""; CurrentExpression.Text = ""; leftOp = null; resetExpression = false; } OutputText.AppendText(alpha.Text); if (OutputText.Text.Length > 3) // n,nnn and beyond { Int64 slacker = ConvertOutputToLong(); OutputText.Text = String.Format("{0:#,0}", slacker); } } public void ArithmeticButton_Click(object sender, EventArgs args) { Button alpha = sender as Button; if (alpha == null) // The typecast failed return; // This... probably shouldn't ever happen anyway //CurrentExpression.AppendText(alpha.Text + " " + OutputText.Text); if (!leftOp.HasValue) { leftOp = ConvertOutputToLong(); operation = alpha.Text[0]; replace = true; } else { rightOp = ConvertOutputToLong(); switch (operation) { case ('+'): leftOp += rightOp; replace = true; break; case ('-'): leftOp -= rightOp; replace = true; break; case ('*'): leftOp *= rightOp; replace = true; break; case ('\u00F7'): leftOp /= rightOp; replace = true; break; case ('='): CurrentExpression.Text = ""; resetExpression = false; break; default: break; } if (alpha.Text[0] == '=') { resetExpression = true; operation = alpha.Text[0]; } else { operation = alpha.Text[0]; } } CurrentExpression.Text = " " + alpha.Text + " " + OutputText.Text + " " + CurrentExpression.Text; OutputText.Text = String.Format("{0:#,0}", leftOp); } private void Clear_Button_Click(object sender, EventArgs e) { CurrentExpression.Text = ""; leftOp = null; } private void ClearExpression_Button_Click(object sender, EventArgs e) { OutputText.Text = "0"; } private void Negate_Button_Click(object sender, EventArgs e) { /* An exercise for you to complete, if you like if (OutputText.Text[OutputText.Text.Length - 1] != '-') OutputText.Text = OutputText.Text.Insert(OutputText.Text.Length - 1, "-"); else OutputText.Text = OutputText.Text.Remove(OutputText.Text.Length - 1, 1); */ } } }