首页 > 解决方案 > 如何让我的计算器进行多次计算?(C#)

问题描述

我使用 Visual Studio 和 C# 编程语言创建了一个计算器。一切都很好,除了一件事。

我不能让计算器一次做多个计算(例如:3 + 3),我希望它可以做 3 + 3 + 3 + 3,但我不知道该怎么做。

(我把你应该感兴趣的代码留在下面)

    private void click_operazione(object sender, EventArgs e)
    {
        double i;
        if (double.TryParse(textBox1.Text, out i))
        {
            Button b = (Button)sender;
            Operazione = b.Text;
            numero = double.Parse((textBox1.Text));
            hapremuto_operazione = true;
            textBox2.Text = numero + " " + Operazione;
        }
    }

    private void risultato_click(object sender, EventArgs e)
    {
        hapremuto_uguale = true;
        switch (Operazione)
        {
            case "+":

                risultato = (numero + double.Parse(textBox1.Text)).ToString();
                textBox2.Text = textBox2.Text + " " + textBox1.Text;
                textBox1.Text = risultato;
                textBox2.Text = textBox2.Text + " = " + risultato;
                break;
            case "-":
                risultato = (numero - double.Parse(textBox1.Text)).ToString();
                textBox2.Text = textBox2.Text + " " + textBox1.Text;
                textBox1.Text = risultato;
                textBox2.Text = textBox2.Text + " = " + risultato;
                break;
            case "x":
                risultato = (numero * double.Parse(textBox1.Text)).ToString();
                if (risultato.Length <= 15)
                {
                    textBox2.Text = textBox2.Text + " " + textBox1.Text;
                    textBox1.Text = risultato;
                    textBox2.Text = textBox2.Text + " = " + risultato;
                }
                else
                {
                    textBox1.Text = "errore";
                    textBox2.Text = "errore";
                }
                break;
            case ":":
                risultato = (numero / double.Parse(textBox1.Text)).ToString();
                textBox2.Text = textBox2.Text + " " + textBox1.Text;
                textBox1.Text = risultato;
                textBox2.Text = textBox2.Text + " = " + risultato;
                break;
            case "%":
                risultato = ((numero * double.Parse(textBox1.Text)) / 100).ToString();
                textBox2.Text = textBox2.Text + " " + textBox1.Text;
                textBox1.Text = risultato;
                textBox2.Text = textBox2.Text + " = " + risultato;
                break;
        }
        ha_appena_messo_una_virgola = false;
    } 

我还没有尝试任何东西,我没有想到任何想法

计算器

标签: c#calculator

解决方案


You can create a delegate that accepts a method reference for you calculation

See Delegates.

public delegate double CalculateExpression(double value1, double value2);

You then then need to create methods that have the same parameter list as the delegate and same return type.

For example this will multiply:

Overall, I would suggest looking into delegates to solve this problem.

public double Multiply(double value1, double value2)
{
    return value1 * value2; 
    // add validation and etc. if needed instead of returning 
    // immediately .
}

Creating and using this delegate in the calling method.

var calcExpression = new CalculateExpression(Multiply);
double result = calcExpression(10, 10); // result is 100.

推荐阅读