//数字键监听 private void btnNum_Click(object sender,EventArgs e) { if(sender is Button) { Button btn = (Button)sender; if (!stop) { //得到当前按键数字,追加 show.Text += btn.Text; } else { stop = false;//完成了一次算式运算之后重新初始化 show.Text = btn.Text;//清空并填充当前数据 handler = null;//委托方法列表清空 } } }
static void Main(string[] args) { Calcuate calc = (a, b) => a + b; Console.WriteLine($"3 + 4 = " + calc(3, 4)); }
//运算符监听 private void btnOperation_Click(object sender, EventArgs e) { Button btn = (Button)sender; if (!stop) { //追加运算符 show.Text += btn.Text; //记录当前算式运算符 operation = btn.Text; //委托相应的运算方法 switch (operation) { case "+": handler += math.Add; break; case "-": handler += math.Sub; break; case "*": handler += math.Mul; break; case "/": handler += math.Div; break; } } else { //还没输入过数字就输入运算符 MessageBox.Show("您还没输入第一操作数哦!"); } }