本文实例为大家分享了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;//输入第一个参与运算的数;字符串的链接个十百千
代码不足之处
仅供两个操作数的运算使用,新加操作数比较麻烦
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
查看更多关于C#实现简单的计算器功能(窗体)的详细内容...