王朝网络
分享
 
 
 

C#计算器(WinForm版)

王朝学院·作者佚名  2016-05-20  
宽屏版  字体: |||超大  

C#计算器(WinForm版)

1.打开Microsoft Visual Studio 2013, 新建名字为【Calculator】的项目。

2.在Form1窗体上放置合适的控件,进行布局,(PS:建议遵循好的命名规范)。

3.Form1的代码书写如下:

1 public partial class frmMain : Form 2 { 3 PRivate string number; 4 public frmMain() 5 { 6 InitializeComponent(); 7 } 8 /// <summary> 9 /// 将计算器可输入字符1~9,+,-,*,/,(,)同时赋值给变量number和文本框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.txtDisplay.Text += ((Button)sender).Text;17 }18 /// <summary>19 /// =20 /// </summary>21 /// <param name="sender"></param>22 /// <param name="e"></param>23 private void btnQual_Click(object sender, EventArgs e)24 {25 //如果txtDisplay文本框不为空,则进行计算26 if (txtDisplay.Text != String.Empty)27 {28 number = this.txtDisplay.Text;29 this.txtDisplay.Text += "=";30 this.txtDisplay.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 /// <summary>44 /// 限制txtDisplay中的字符输入45 /// </summary>46 /// <param name="sender"></param>47 /// <param name="e"></param>48 private void txtDisplay_KeyPress(object sender, KeyPressEventArgs e)49 {50 //如果输入的不是数字类别,也不是回车键、Backspace键、+ - * / ( ),则txtDisplay_KeyPress取消该输入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.我们注意到btnQual_Click事件中有一个方法Calculator.dealWith(),同样我们将Calculator代码书写如下:

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 //循环字符串中的所有字符并赋值给numberQueue队列 12 foreach (char c in number) 13 { 14 15 numberQueue.Enqueue(c); 16 } 17 18 //拆分队列中的字符,构成一个数字与“+”或“-”的组合,然后逐个放入二维数组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对应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 //如果是"+"或"-" 52 if (opera == '+' || opera == '-') 53 { 54 opreandArray[j, 0] = operand1; 55 opreandArray[j, 1] = opera.ToString(); 56 j++; 57 operand1 = null; 58 } 59 //如果是"*"或"/" 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 }107 108 }109 opreand2 = null;110 }111 }112 }113 114 115 //把二维数组中的数计算,赋值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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有