简易计算器WinForm方式
在博客园上看到的,忘记路径了
效果图:
Form1.cs
namespace 简易计算器 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string FuHao; //定义操作符号(+,-,*,/) private string num1; //定义第一个参数(就是参与运算的第一个数) private string num2; //定义第二个参数 private string oldValue = "0"; //定义老参数 private void btn_Clean_Click(object sender, EventArgs e) { txt_Vlaue.Text = "0"; FuHao = string.Empty; oldValue = "0"; } private void btn_GetVlaue_Click(object sender, EventArgs e) { if (FuHao == string.Empty) { txt_Vlaue.Text = num1.ToString(); } else { GetValue(); } } private void btn_add_Click(object sender, EventArgs e) { if (num2 != null && num2 != string.Empty) { GetValue(); } FuHao = "+"; oldValue = "0"; } private void btn_Jian_Click(object sender, EventArgs e) { if (num2 != null && num2 != string.Empty) { GetValue(); } FuHao = "-"; oldValue = "0"; } private void btn_Cheng_Click(object sender, EventArgs e) { if (num2 != null && num2 != string.Empty) { GetValue(); } FuHao = "*"; oldValue = "0"; } private void btn_Chu_Click(object sender, EventArgs e) { if (num2 != null && num2 != string.Empty) { GetValue(); } FuHao = "/"; oldValue = "0"; } private void btn_0_Click(object sender, EventArgs e) { InputName("0"); } private void btn_1_Click(object sender, EventArgs e) { InputName("1"); } private void btn_2_Click(object sender, EventArgs e) { InputName("2"); } private void btn_3_Click(object sender, EventArgs e) { InputName("3"); } private void btn_4_Click(object sender, EventArgs e) { InputName("4"); } private void btn_5_Click(object sender, EventArgs e) { InputName("5"); } private void btn_6_Click(object sender, EventArgs e) { InputName("6"); } private void bvtn_7_Click(object sender, EventArgs e) { InputName("7"); } private void btn_8_Click(object sender, EventArgs e) { InputName("8"); } private void btn_9_Click(object sender, EventArgs e) { InputName("9"); } public void InputName(string value) { if (oldValue != "0") { txt_Vlaue.Text = oldValue + value; } else { if (value == ".") { txt_Vlaue.Text = "0" + value; } else { txt_Vlaue.Text = value; } } oldValue = txt_Vlaue.Text; if (FuHao != null && FuHao.ToString().Trim() != string.Empty) { num2 = txt_Vlaue.Text; } else { num1 = txt_Vlaue.Text; } } private void GetValue() { double numa = double.Parse(num1); double numb = double.Parse(num2); double result = 0; switch (FuHao) { case "+": result = numa + numb; break; case "-": result = numa - numb; break; case "*": result = numa * numb; break; default: result = numa / numb; break; } txt_Vlaue.Text = result.ToString(); FuHao = string.Empty; oldValue = "0"; num1 = txt_Vlaue.Text; num2 = string.Empty; } } }