C#实现简单的计算器功能(窗体)

这篇文章主要为大家详细介绍了C#实现简单的计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#实现简单的计算器功能的具体代码,供大家参考,具体内容如下

1.界面设计

2.代码

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace calculator3 {     public partial class Form1 : Form     {         private string num1, num2;//计算器的操作数,成员变量         private string opr;//操作符         public Form1()         {             InitializeComponent();         }         //数字按钮点击事件的方法         private void NumClick(object sender, EventArgs e)         {             Button button = (Button)sender;             if (string.IsNullOrEmpty(opr))//如果还没有输入操作符             {                 num1 = num1 + button.Text;//输入第一个参与运算的数;字符串的链接个十百千             }             else             {                 num2 = num2 + button.Text;//输入第二个参与运算的数;字符串的链接个十百千             }             txtResult.Text = txtResult.Text + button.Text;         }         //操作符按钮点击事件的方法         private void oprClick(object sender, EventArgs e)         {             Button button=(Button)sender;             if (String.IsNullOrEmpty(num2))//如果还没有输入数字,则不允许按操作符             {                 MessageBox.Show("此时不应该按入操作符!");                 return;             }             opr = button.Text;             txtResult.Text = txtResult.Text + button.Text;         }         //“=”事件,即计算         private void btnGet_Click(object sender, EventArgs e)         {             if (String.IsNullOrEmpty(opr)                 || String.IsNullOrEmpty(num1)                 || String.IsNullOrEmpty(num2))             {                 MessageBox.Show("您输入的内容有误!");                 return;             }                                         txtResult.Text = txtResult.Text + "=";//将“=”拼接到框框里             //进行两个数的运算                 switch (opr)                 {                      case "+":                         txtResult.Text = txtResult.Text + (Int32.Parse(num1) + Int32.Parse(num2));                         break;                     case "-":                         txtResult.Text = txtResult.Text + (Int32.Parse(num1) - Int32.Parse(num2));                         break;                     case "*":                         txtResult.Text = txtResult.Text + (Int32.Parse(num1) * Int32.Parse(num2));                         break;                     case "/":                         if (num2 == "0")                         {                             MessageBox.Show("除数不可以为零!");                         }                         txtResult.Text = txtResult.Text + (Int32.Parse(num1) / Int32.Parse(num2));                         break;                 }                      }         //清除事件         private void btnClear_Click(object sender, EventArgs e)         {             txtResult.Text = "";             num1 = "";             num2 = "";             opr = "";         }        } }

3.总结分析

按钮点击事件:当多数按钮的点击效果一致时,可使用同一个Click事件(名字一致即可)

//仅作举例使用 //关键代码 Button button = (Button)sender; //此时字符串的链接 num1 = num1 + button.Text;//输入第一个参与运算的数;字符串的链接个十百千

代码不足之处

仅供两个操作数的运算使用,新加操作数比较麻烦

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持0133技术站。

以上就是C#实现简单的计算器功能(窗体)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程