zoukankan      html  css  js  c++  java
  • WinForm 小练习 计算器(已简化)

      1 bool a; //判断上一次按键是加减乘除还是数字
      2         string c = ""; //判断加减乘除操作
      3         decimal f;//记录第一个值以及接收运算结果
      4         /// <summary>
      5         /// 数字按键
      6         /// </summary>
      7         /// <param name="sender"></param>
      8         /// <param name="e"></param>
      9         private void button1_Click(object sender, EventArgs e)
     10         {
     11             Button b = sender as Button;//sender是事件的主体,e是事件的数据,这里把sender转换为button类的目的是获取用户操作,因为事件主体是一个button,所以sender可以转换为button类。
     12             if (textBox2.Text == "0" || a == false)
     13             {
     14                 textBox2.Text = b.Text;
     15             }
     16             else
     17             {
     18                 textBox2.Text += b.Text;
     19             }
     20             if (textBox1.Text == "")
     21             {
     22                 f = Convert.ToDecimal(textBox2.Text);
     23             }
     24             a = true;
     25         }
     26         /// <summary>
     27         /// 加减乘除
     28         /// </summary>
     29         /// <param name="sender"></param>
     30         /// <param name="e"></param>
     31         private void button4_Click(object sender, EventArgs e)
     32         {
     33             Button b = sender as Button;
     34             if (textBox2.Text.LastIndexOf(".") == (textBox2.Text.Length - 1))
     35             {
     36                 textBox2.Text = textBox2.Text.Substring(0, (textBox2.Text.Length - 1));
     37             }           
     38             if (a == false)
     39             {
     40                 textBox1.Text = textBox1.Text.Substring(0, (textBox1.Text.Length - 1)) + b.Text;
     41             }
     42             else
     43             {
     44                 textBox1.Text += textBox2.Text + b.Text;
     45                 if (c == "1")
     46                 {
     47                     f = f + Convert.ToDecimal(textBox2.Text);
     48                 }
     49                 else if (c == "2")
     50                 {
     51                     f = f - Convert.ToDecimal(textBox2.Text);
     52                 }
     53                 else if (c == "3")
     54                 {
     55                     f = f * Convert.ToDecimal(textBox2.Text);
     56                 }
     57                 else if (c == "4")
     58                 {
     59                     f = f / Convert.ToDecimal(textBox2.Text);
     60                 }
     61                 textBox2.Text = f.ToString();
     62             }
     63             if (b.Text == "+")
     64             {
     65                 c = "1";
     66             }
     67             else if (b.Text == "-")
     68             {
     69                 c = "2";
     70             }
     71             else if (b.Text == "*")
     72             {
     73                 c = "3";
     74             }
     75             else if (b.Text == "/")
     76             {
     77                 c = "4";
     78             }
     79             a = false;
     80         }
     81         /// <summary>
     82         /// 83         /// </summary>
     84         /// <param name="sender"></param>
     85         /// <param name="e"></param>
     86         private void button17_Click(object sender, EventArgs e)
     87         {
     88             if (textBox2.Text.Contains(".") == false)
     89             {
     90                 textBox2.Text += ".";
     91             }
     92         }
     93         /// <summary>
     94         /// 等于
     95         /// </summary>
     96         /// <param name="sender"></param>
     97         /// <param name="e"></param>
     98         private void button15_Click(object sender, EventArgs e)
     99         {
    100             if (textBox2.Text.LastIndexOf(".") == (textBox2.Text.Length - 1))
    101             {
    102                 textBox2.Text = textBox2.Text.Substring(0, (textBox2.Text.Length - 1));
    103             }
    104             if (c == "1")
    105             {
    106                 f = f + Convert.ToDecimal(textBox2.Text);
    107             }
    108             else if (c == "2")
    109             {
    110                 f = f - Convert.ToDecimal(textBox2.Text);
    111             }
    112             else if (c == "3")
    113             {
    114                 f = f * Convert.ToDecimal(textBox2.Text);
    115             }
    116             else if (c == "4")
    117             {
    118                 f = f / Convert.ToDecimal(textBox2.Text);
    119             }
    120             textBox1.Text = "";
    121             textBox2.Text = f.ToString();
    122         }
    123         /// <summary>
    124         /// 清空
    125         /// </summary>
    126         /// <param name="sender"></param>
    127         /// <param name="e"></param>
    128         private void button14_Click(object sender, EventArgs e)
    129         {
    130             textBox1.Text = "";
    131             textBox2.Text = "0";
    132             a = true;
    133             c = "";
    134         }

  • 相关阅读:
    开发培训体会——写好代码的一些编码规则和设计原则
    开发培训体会——写好代码的一些基本原理
    传说中的奖励通报
    NetBeans 6.7 Milestone 2 Now Available for Download!
    Software Process Engine of BeyondTrack
    开发培训体会——代码的价值
    NetBeans 6.7 Milestone 2 Now Available for Download!
    传说中的奖励通报
    有关_sprintf函数重定义错误的思考
    Software Process Engine of BeyondTrack
  • 原文地址:https://www.cnblogs.com/mazhijie/p/5618524.html
Copyright © 2011-2022 走看看