public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string FuHao; //定义操作符号(+,-,*,/) private string num1; //定义第一个参数(就是参与运算的第一个数) private string num2; //定义第二个参数 private string oldValue = "0"; //定义老参数
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; }
private void button0_Click(object sender, EventArgs e) { InputName("0"); }
private void button1_Click(object sender, EventArgs e) { InputName("1"); }
private void button2_Click(object sender, EventArgs e) { InputName("2"); }
private void button3_Click(object sender, EventArgs e) { InputName("3"); }
private void button4_Click(object sender, EventArgs e) { InputName("4"); }
private void button5_Click(object sender, EventArgs e) { InputName("5"); }
private void button6_Click(object sender, EventArgs e) { InputName("6"); }
private void button7_Click(object sender, EventArgs e) { InputName("7"); }
private void button8_Click(object sender, EventArgs e) { InputName("8"); }
private void button9_Click(object sender, EventArgs e) { InputName("9"); }
private void buttonjia_Click(object sender, EventArgs e) { if (num2 != null && num2 != string.Empty) { GetValue(); } FuHao = "+"; oldValue = "0"; }
private void buttonjian_Click(object sender, EventArgs e) { if (num2 != null && num2 != string.Empty) { GetValue(); } FuHao = "-"; oldValue = "0"; }
private void buttoncheng_Click(object sender, EventArgs e) { if (num2 != null && num2 != string.Empty) { GetValue(); } FuHao = "*"; oldValue = "0"; }
private void buttonchu_Click(object sender, EventArgs e) { if (num2 != null && num2 != string.Empty) { GetValue(); } FuHao = "/"; oldValue = "0"; }
private void buttonqing_Click(object sender, EventArgs e) { txt_Vlaue.Text = "0"; FuHao = string.Empty; oldValue = "0"; }
private void buttondeng_Click(object sender, EventArgs e) { if (FuHao == string.Empty) { txt_Vlaue.Text = num1.ToString(); } else { GetValue(); } } }
用winform编写简易计算器
最新推荐文章于 2022-01-15 13:38:14 发布