- 最後登錄
- 2022-2-18
- 在線時間
- 0 小時
- 註冊時間
- 2010-3-7
- 閱讀權限
- 20
- 精華
- 0
- UID
- 7741797
- 帖子
- 76
- 積分
- 193 點
- 潛水值
- 4402 米
| 本帖最後由 跨越青春嶺 於 2017-3-12 10:57 PM 編輯
供您參考,這樣或許更清楚明瞭.........?
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- namespace HW_11271158
- {
- public partial class Form1 : Form
- {
- private delegate double Cal(int a, int b);
- private Cal _cal = new Cal(ADD);
- public Form1()
- {
- InitializeComponent();
- addRadioButton.CheckedChanged += new EventHandler(operation_CheckedChanged);
- subRadioButton.CheckedChanged += new EventHandler(operation_CheckedChanged);
- mulRadioButton.CheckedChanged += new EventHandler(operation_CheckedChanged);
- divRadioButton.CheckedChanged += new EventHandler(operation_CheckedChanged);
- }
- private void operation_CheckedChanged(object sender, EventArgs e)
- {
- var currentRadioButton = sender as RadioButton;
- var operation = new Dictionary<string, Cal> {
- { "ADD", ADD },
- { "SUB", SUB },
- { "MUL", MUL },
- { "DIV", DIV },
- };
- _cal = operation[currentRadioButton.Tag.ToString()];
- }
- private static double ADD(int a, int b)
- {
- return a + b;
- }
- private static double SUB(int a, int b)
- {
- return a - b;
- }
- private static double MUL(int a, int b)
- {
- return a * b;
- }
- private static double DIV(int a, int b)
- {
- return a / b;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- var a = int.Parse(textBox1.Text);
- var b = int.Parse(textBox2.Text);
- textBox3.Text = _cal(a, b).ToString();
- }
- }
- }
複製代碼
... |
|