首页 > 代码库 > C# Windows form制作简易计算器

C# Windows form制作简易计算器

        在经过一段时间的C#学习后,试着做了一个计算器的小工程。

      

         计算器的界面如上图,包含基本的+、-、*、/运算以及1/x运算和清零功能。代码如下:

      

         

  1 using System;  2 using System.Collections.Generic;  3 using System.ComponentModel;  4 using System.Data;  5 using System.Drawing;  6 using System.Linq;  7 using System.Text;  8 using System.Threading.Tasks;  9 using System.Windows.Forms; 10  11 namespace calcDemo_2_ 12 { 13     public partial class Form1 : Form 14     { 15         public Form1() 16         { 17             InitializeComponent(); 18         } 19         double step1, step2, step3; 20         string str;//定义运算符 21         int i = 0; 22         string numberStr = "";//两个以上的数字拼接 23         private void button0_Click(object sender, EventArgs e)//按钮0 24         { 25             if (double.Parse(textBox1.Text) > 0) 26             { 27  28                 numberStr = numberStr + "0"; 29                 textBox1.Text = numberStr; 30             } 31             else if (textBox1.Text == "0.") 32             { 33                 numberStr = numberStr + "0"; 34                 textBox1.Text = numberStr; 35             } 36             else 37             { 38                 textBox1.Text = "0"; 39                 numberStr = ""; 40             } 41         } 42         private void button1_Click(object sender, EventArgs e)//按钮1 43         { 44             numberStr += "1"; 45             textBox1.Text = numberStr; 46         } 47         private void button2_Click(object sender, EventArgs e)//按钮2 48         { 49             numberStr = numberStr + "2"; 50             textBox1.Text = numberStr; 51         } 52         private void button3_Click(object sender, EventArgs e)//按钮3 53         { 54             numberStr = numberStr + "3"; 55             textBox1.Text = numberStr; 56         } 57         private void button4_Click(object sender, EventArgs e)//按钮4 58         { 59             numberStr = numberStr + "4"; 60             textBox1.Text = numberStr; 61         } 62  63         private void button5_Click(object sender, EventArgs e)//按钮5 64         { 65             numberStr = numberStr + "5"; 66             textBox1.Text = numberStr; 67         } 68  69         private void button6_Click(object sender, EventArgs e)//按钮6 70         { 71             numberStr = numberStr + "6"; 72             textBox1.Text = numberStr; 73         } 74  75         private void button7_Click(object sender, EventArgs e)//按钮7 76         { 77             numberStr = numberStr + "7"; 78             textBox1.Text = numberStr; 79         } 80  81         private void button8_Click(object sender, EventArgs e)//按钮8 82         { 83             numberStr = numberStr + "8"; 84             textBox1.Text = numberStr; 85         } 86  87         private void button9_Click(object sender, EventArgs e)//按钮9 88         { 89             numberStr = numberStr + "9"; 90             textBox1.Text = numberStr; 91         } 92         private void buttonPoint_Click(object sender, EventArgs e)//小数点 93         { 94             i = i + 1; 95             if (i == 1 && textBox1.Text.Length != 0) 96             { 97                 numberStr = textBox1.Text.ToString(); 98                 numberStr += "."; 99             }100             textBox1.Text = numberStr;101         }102         private void buttonadd_Click(object sender, EventArgs e)//加法代码103         {104             str = "+";105             if (textBox1.Text.Length != 0)106             {107                 step1 = double.Parse(textBox1.Text);108             }109             else110             {111                 step1 = 0;112             }113             textBox1.Text = "0";114             numberStr = "";115             i = 0;//按下+号键后获取第一个数step1;116         }117 118         private void buttonsub_Click(object sender, EventArgs e)//减法代码119         {120             str = "-";121             if (textBox1.Text.Length != 0)122             {123                 step1 = double.Parse(textBox1.Text);124             }125             else126             {127                 step1 = 0;128             }129             textBox1.Text = "0";130             numberStr = "";131             i = 0;//按下-号键后获取第一个数step1;132         }133 134         private void buttonmul_Click(object sender, EventArgs e)//乘法代码135         {136             str = "*";137             if (textBox1.Text.Length != 0)138             {139                 step1 = double.Parse(textBox1.Text);140             }141             else142             {143                 step1 = 0;144             }145             textBox1.Text = "0";146             numberStr = "";147             i = 0;//按下*号键后获取第一个数step1;148         }149 150         private void buttondiv_Click(object sender, EventArgs e)//除法代码151         {152             str = "/";153             if (textBox1.Text.Length != 0)154             {155                 step1 = double.Parse(textBox1.Text);156             }157             else158             {159                 step1 = 0;160             }161             textBox1.Text = "0";162             numberStr = "";163             i = 0;//按下/号键后获取第一个数step1;164         }165         /// <summary>166         /// 设计一个方法,将获得的两个数和一个运算符执行+,-,*,/167         /// </summary>168         /// <param name="num1">第一个数</param>169         /// <param name="num2">第二个数</param>170         /// <param name="str1">运算符</param>171         /// <returns>经过运算返回*+、-、/、*的结果</returns>172         private double NumberFunction(double num1, double num2, string str1)173         {174             double result = 0;175             switch (str1)176             {177                 case "+":178                     result = num1 + num2;179                     break;180                 case "-":181                     result = num1 - num2;182                     break;183                 case "*":184                     result = num1 * num2;185                     break;186                 case "/":187                     if (step2 != 0)188                     {189                         result = num1 / num2;190                     }191                     break;192             }193             return result;194         }195 196         private void buttonresult_Click(object sender, EventArgs e)//按钮=197         {198             step2 = double.Parse(textBox1.Text);//按下后获取第二个数step2199             if (str != null)200             {201                 textBox1.Text = NumberFunction(step1, step2, str).ToString();202             }203             else204             {205                 textBox1.Text = step2.ToString();206             }//将step1、step2及str带入方法NumberFunction进行计算,并将结果显示到文本框;207             numberStr = "";208             i = 0;209         }210 211         private void textBox1_TextChanged(object sender, EventArgs e)212         {213 214         }215 216         private void buttonclear_Click(object sender, EventArgs e)//按钮C,重置所有数值217         {218             textBox1.Text = "0";219             numberStr = "";220             i = 0;221             step1 = 0;222             step2 = 0;223             step3 = 0;224         }225 226         private void buttondivx_Click(object sender, EventArgs e)//按钮1/x,输入一个数,得出1除以该数的值227         {228             if (textBox1.Text.Length != 0)229             {230                 step3 = double.Parse(textBox1.Text);231             }232             else233             {234                 step3 = 0;235             }236             if (step3 != 0)237             {238                 textBox1.Text = (1 / step3).ToString();239             }240             else241             {242                 textBox1.Text = "error";243             }244             numberStr = "";245         }246 247 248     }249 }

            经过多次运行,解决了诸多bug,例如第一个数为0的拼接问题、小数点的问题、0.0的问题,相信其中应该还存在着bug,若有朋友看到,敬请评论指点。C#新手,没什么高质量,高手勿笑。

C# Windows form制作简易计算器