0

私は一生、入力を連続して表示し、次にこの数値の出力を表示する方法を理解できません。

数値をどこかに保存してから表示するにはどうしますか? 私は非常に多くのサークルに参加したため、自分自身を混乱させて忘却しました。何をする必要があるかはわかっていますが、どのように、または正確にどこでそれを行うべきかはわかりません。

public partial class Form1 : Form
{
    char c;
    double num1;
    double num2;

    public Form1()
    {
        InitializeComponent();
    }
    private void btn0_Click(object sender, EventArgs e)
    {
        txtBox.Text += 0;
    }
    private void btn1_Click(object sender, EventArgs e)
    {
        txtBox.Text += 1;
    }
    private void btn2_Click(object sender, EventArgs e)
    {
        txtBox.Text += 2;
    }
    private void btn3_Click(object sender, EventArgs e)
    {
        txtBox.Text += 3;
    }
    private void btn4_Click(object sender, EventArgs e)
    {
        txtBox.Text += 4;
    }
    private void btn5_Click(object sender, EventArgs e)
    {
        txtBox.Text += 5;
    }
    private void btn6_Click(object sender, EventArgs e)
    {
        txtBox.Text += 6;
    }
    private void btn7_Click(object sender, EventArgs e)
    {
        txtBox.Text += 7;
    }
    private void btn8_Click(object sender, EventArgs e)
    {
        txtBox.Text += 8;
    }
    private void btn9_Click(object sender, EventArgs e)
    {
        txtBox.Text += 9;
    }
    private void btnDecimal_Click(object sender, EventArgs e)
    {
        if (!txtBox.Text.Contains('.'))
            txtBox.Text += '.';
    }
    private void btnAddition_Click(object sender, EventArgs e)
    {
        c = '+';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnSubtraction_Click(object sender, EventArgs e)
    {
        c = '-';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnMultiplication_Click(object sender, EventArgs e)
    {
        c = '*';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnDivision_Click(object sender, EventArgs e)
    {
        c = '/';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnClear_Click(object sender, EventArgs e)
    {
        txtBox.Clear();
    }
    private void btnEqual_Click(object sender, EventArgs e)
    {

        num2 = double.Parse(txtBox.Text);
        double result;
        switch (c)
        {
            case '+':
                result = num1 + num2;
                txtBox.Text = result.ToString();
                break;
            case '-':
                result = num1 - num2;
                txtBox.Text = result.ToString();
                break;
            case '/':
                if (num2 != 0)
                {
                    result = num1 / num2;
                    txtBox.Text = result.ToString();
                }
                else
                {
                    txtBox.Text = "You can't divide by zero... sign up for Math 100 please =)";
                }
                break;
            case '*':
                result = num1 * num2;
                txtBox.Text = result.ToString();
                break;
        }
    }
}

}

4

1 に答える 1

2

これを配線させていただきますが、ここに私がまとめたものがあります。

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private string op;
    private string num1;
    private string num2;

    private void handleNumberButtonClick(object sender, System.EventArgs e)
    {
        var num = Convert.ToInt16(((Button)sender).Tag);
        if (op == null)
            num1 += num;
        else
            num2 += num;
        PrintEquation(num1, op, num2);
    }

    private void PrintEquation(string first, string oper = null, string second = null, string equals = null, string result = null)
    {
        txtBox.Text = first + oper + second + equals + result;
    }

    private void handleOperatorButtonClick(object sender, System.EventArgs e)
    {
        op = ((Button)sender).Tag.ToString();
        PrintEquation(num1, op);
    }

    private void btnDecimal_Click(object sender, EventArgs e)
    {
        if (op == null && !num1.Contains(".")) num1 += ".";
        if (op != null && !num2.Contains(".")) num2 += ".";
        this.PrintEquation(num1, op, num2);
    }
    private void btnClear_Click(object sender, EventArgs e)
    {
        txtBox.Clear();
        op = null;
        num1 = null;
        num2 = null;
    }
    private void btnEqual_Click(object sender, EventArgs e)
    {
        double result = 0;
        var first = Convert.ToDouble(num1);
        var second = Convert.ToDouble(num2);
        switch (op)
        {
            case "+":
                result = first + second;
                break;
            case "-":
                result = first - second;
                break;
            case "/":
                if (second != 0)
                {
                    result = first / second;
                }
                else
                {
                    errorLbl.Text = "You can't divide by zero... sign up for Math 100 please =)";
                }
                break;
            case "*":
                result = first * second;
                break;
        }
        this.PrintEquation(num1, op, num2, "=", result.ToString());
    }
}

物事がどのように連携するかの手がかりを得るために、これは .designer.cs ファイルでボタンがどのように見えるかです:

        this.btn0.Location = new System.Drawing.Point(12, 60);
        this.btn0.Name = "btn0";
        this.btn0.Size = new System.Drawing.Size(75, 23);
        this.btn0.TabIndex = 1;
        this.btn0.Tag = "0";
        this.btn0.Text = "btn0";
        this.btn0.UseVisualStyleBackColor = true;
        this.btn0.Click += new System.EventHandler(this.handleNumberButtonClick);

.Tag 値とイベントハンドラに注意してください。

オペレータは次のようになります (これは追加です。タグとクリック イベント ハンドラに注意してください)。

        this.addButton.Location = new System.Drawing.Point(178, 60);
        this.addButton.Name = "addButton";
        this.addButton.Size = new System.Drawing.Size(75, 23);
        this.addButton.TabIndex = 11;
        this.addButton.Tag = "+";
        this.addButton.Text = "add";
        this.addButton.UseVisualStyleBackColor = true;
        this.addButton.Click += new System.EventHandler(this.handleOperatorButtonClick);

そして、このような小数:

        this.button4.Location = new System.Drawing.Point(178, 176);
        this.button4.Name = "button4";
        this.button4.Size = new System.Drawing.Size(75, 23);
        this.button4.TabIndex = 15;
        this.button4.Tag = ".";
        this.button4.Text = "decimal";
        this.button4.UseVisualStyleBackColor = true;
        this.button4.Click += new System.EventHandler(this.btnDecimal_Click);

ああ、これを明確なボタン定義に入れます

        this.button5.Click += new System.EventHandler(this.btnClear_Click);

@Mark Hall@Martin Jamesの洞察に感謝します。

于 2013-09-02T02:47:33.420 に答える