C # calculator (WinForm ),

Source: Internet
Author: User

C # calculator (WinForm ),

1. Open Microsoft Visual Studio 2013 and create a project named [Calculator.

2. Place appropriate controls on the Form1 form for layout (PS: Good naming rules are recommended ).

3. Form1 code is written as follows:

1 public partial class frmMain: Form 2 {3 private string number; 4 public frmMain () 5 {6 InitializeComponent (); 7} 8 // <summary> 9 // enter 1 ~ 9, + ,-,*,/,(,) assign the variable number and the text box txtDisplay10 /// </summary> 11 /// <param name = "sender"> </param> 12 /// <param name =" e "> </param> 13 private void InputHandler (object sender, eventArgs e) 14 {15 number + = (Button) sender ). text; 16 this.txt Display. text + = (Button) sender ). text; 17} 18 /// <summary> 19 /// = 20 /// </summary> 21 /// <param name = "sender"> </param> 22/ // <param name = "e"> </param> 23 private void btnQu Al_Click (object sender, EventArgs e) 24 {25 // if the txtDisplay Text box is not empty, calculate 26 if (txtDisplay. Text! = String. empty) 27 {28 number = this.txt Display. text; 29 this.txt Display. text + = "="; 30 this.txt Display. text + = Calculator. dealWith (number ). toString (); 31} 32} 33 // <summary> 34 // CE35 // </summary> 36 // <param name = "sender"> </param> 37 /// <param name = "e"> </param> 38 private void btClear_Click (object sender, eventArgs e) 39 {40 number = string. empty; 41 txtDisplay. text = string. empty; 42} 43 // <sum Mary> 44 // enter 45 characters in txtDisplay /// </summary> 46 /// <param name = "sender"> </param> 47 // <param name = "e"> </param> 48 private void txtDisplay_KeyPress (object sender, keyPressEventArgs e) 49 {50 // if the input is not a numeric category, nor is it a enter key, Backspace key, +-*/(), txtDisplay_KeyPress cancels the input 51 if (! (Char. IsNumber (e. KeyChar) & e. KeyChar! = (Char) 13 & e. KeyChar! = (Char) 8 & e. KeyChar! = (Char) 40 & e. KeyChar! = (Char) 41 & e. KeyChar! = (Char) 42 & e. KeyChar! = (Char) 43 & e. KeyChar! = (Char) 45 & e. KeyChar! = (Char) 47) 52 {53 e. Handled = true; 54} 55} 56}

4. We noticed that there is a method Calculator. dealWith () in the btnQual_Click event. Similarly, we write the Calculator Code as follows:

1 public static class Calculator 2 {3 public static float dealWith (string number) 4 {5 string operand1 = "", opreand2 = ""; 6 float result = 0; 7 char opera = '', operandOrOera =''; 8 string [,] opreandArray = new string [50, 2]; 9 Queue numberQueue = new Queue (); 10 11 // All characters in the loop string are assigned to the numberQueue queue 12 foreach (char c in number) 13 {14 15 numberQueue. enqueue (c); 16} 17 18 // split the characters in the queue to form a number with "+" or "- And put them into the two-dimensional array opreandArray 19 while (numberQueue. Count! = 0) 20 {21 operandOrOera = Convert. toChar (numberQueue. peek (); 22 if (operandOrOera = '(') 23 {24 numberQueue. dequeue (); 25 string inside = null; 26 while (Convert. toChar (numberQueue. peek ())! = ') 27 {28 inside + = (numberQueue. dequeue ()). toString (); 29} 30 numberQueue. dequeue (); 31 operand1 = dealWith (inside ). toString (); 32} 33 while (Convert. toInt32 (operandOrOera)> 47 & Convert. toInt32 (operandOrOera) <58) // ASCII48-57 corresponds to 0-9 34 {35 numberQueue. dequeue (); 36 operand1 + = operandOrOera. toString (); 37 if (numberQueue. count! = 0) 38 {39 operandOrOera = Convert. toChar (numberQueue. peek (); 40} 41 else 42 {43 break; 44} 45} 46 int j = 0; 47 if (operandOrOera = '+' | operandOrOera = '-' | operandOrOera = '*' | operandOrOera = '/') 48 {49 numberQueue. dequeue (); 50 opera = operandOrOera; 51 // if it is "+" or "-" 52 if (opera = '+' | opera = '-') 53 {54 opreandArray [j, 0] = operand1; 55 opreandArray [j, 1] = opera. toSt Ring (); 56 j ++; 57 operand1 = null; 58} 59 // if it is "*" or "/" 60 else 61 {62 char n = Convert. toChar (numberQueue. peek (); 63 if (n = '(') 64 {65 66 numberQueue. dequeue (); 67 string inside = null; 68 while (Convert. toChar (numberQueue. peek ())! = ') 69 {70 inside + = (numberQueue. dequeue ()). toString (); 71} 72 numberQueue. dequeue (); 73 opreand2 = dealWith (inside ). toString (); 74} 75 while (Convert. toInt32 (n)> 47 & Convert. toInt32 (n) <58) 76 {77 opreand2 + = n. toString (); 78 numberQueue. dequeue (); 79 if (numberQueue. count! = 0) 80 {81 n = Convert. toChar (numberQueue. peek (); 82} 83 else 84 {85 break; 86} 87} 88 89 switch (opera) 90 {91 case ('*'): 92 {93 operand1 = (Convert. toInt32 (operand1) * Convert. toInt32 (opreand2 )). toString (); 94 break; 95} 96 case ('/'): 97 {98 try 99 {100 operand1 = (Convert. toInt32 (operand1)/Convert. toInt32 (opreand2 )). toString (); 101} 102 catch (Exception) {103 104} 105 break; 106} 10 7 108} 109 opreand2 = null; 110} 111} 112 113 114 // calculate the number in the two-dimensional array and assign the value result116 int count = 0; 117 for (int I = 0; opreandArray [I, 0]! = Null; I ++) 118 {119 count ++; 120} 121 for (int I = 0; I <count; I ++) 122 {123 if (I = 0) 124 {125 result + = Convert. toInt32 (opreandArray [I, 0]); 126 127} 128 else129 {130 if (opreandArray [I-1, 1] = "+ ") 131 {132 result + = Convert. toInt32 (opreandArray [I, 0]); 133} 134 else135 {136 result-= Convert. toInt32 (opreandArray [I, 0]); 137} 138} 139 140 141 142 143 // add or remove 144 if (count! = 0) 145 {146 if (opreandArray [count-1, 1] = "+") 147 {148 return result + Convert. toInt32 (operand1); 149} 150 else151 {152 return result-Convert. toInt32 (operand1); 153} 154} 155 else156 {157 return Convert. toInt32 (operand1); 158} 159} 160}

5. O (∩ _ ∩) O Haha ~, The basic code is written.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.