更改代码c#

更改代码c#,c#,C#,我有一些类似的代码用于显示在所有计算方法中重复的计算。我如何在一个方法中使用这个通用代码,并将运算符传递给它 通用代码: double lhs = double.Parse(lhsOperand.Text); double rhs = double.Parse(rhsOperand.Text); // Do some calculation which sets 'outcome' // Set 'sign' equal to the s

我有一些类似的代码用于显示在所有计算方法中重复的计算。我如何在一个方法中使用这个通用代码,并将运算符传递给它

通用代码:

       double lhs = double.Parse(lhsOperand.Text);
       double rhs = double.Parse(rhsOperand.Text);

       // Do some calculation which sets 'outcome'
       // Set 'sign' equal to the string value of the operator

       expression.Text = lhsOperand.Text + sign + rhsOperand.Text;
       result.Text = outcome.ToString();
下面是一个示例,说明我当前如何根据用户选择的操作调用不同的方法:

    private void calculate_Click(object sender, EventArgs e)
    {
        if (addition.Checked)
             additionValues();

         if (subtraction.Checked)
             subtractValues();

         if (multiplication.Checked)
             multiplicationValues();

         if (division.Checked)
             divisionValues();

         if (remainder.Checked)
             remainderValues();
    }         
这里有一个计算方法的例子,它们都有一个共同的模式。如何创建一个可以将操作传递给它的方法,并让它根据传递给它的运算符处理多个计算

    private void subtractValues()
    {
        try
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome;
            outcome = lhs - rhs;
            expression.Text = lhsOperand.Text + "-" + rhsOperand.Text;
            result.Text = outcome.ToString();
        }
        catch (Exception e)
        {
            result.Text = e.ToString();
        }


    }
    private void additionValues()
    {
        try
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome;
            outcome = lhs + rhs;
            expression.Text = lhsOperand.Text + "+" + rhsOperand.Text;
            result.Text = outcome.ToString();
        }
        catch(Exception e)
        {
            result.Text = e.ToString();
        }
    }
    private void multiplicationValues()
    {
        try
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome;
            outcome = lhs * rhs;
            expression.Text = lhsOperand.Text + "*" + rhsOperand.Text;
            result.Text = outcome.ToString();
        }
        catch (Exception e)
        {
            result.Text = e.ToString();
        }
    }
    private void divisionValues()
    {
        try
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome;
            outcome = lhs / rhs;
            expression.Text = lhsOperand.Text + "/" + rhsOperand.Text;
            result.Text = outcome.ToString();
        }
        catch (DivideByZeroException e)
        {
            result.Text = e.ToString();
        }
        catch (Exception e)
        {
            result.Text = e.ToString();
        }

    }
    private void remainderValues()
    {
        try
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome;
            outcome = lhs % rhs;
            expression.Text = lhsOperand.Text + "%" + rhsOperand.Text;
            result.Text = outcome.ToString();
        }
        catch (DivideByZeroException e)
        {
            result.Text = e.ToString();
        }
        catch (Exception e)
        {
            result.Text = e.ToString();
        }
    }

    private void quit_Click(object sender, EventArgs e)
    {
        this.Close();
    }

“一个方法对应所有值”是什么意思?double lhs=double.Parse(lhsOperand.Text);double-rhs=double.Parse(rhsOperand.Text);expression.Text=lhsOperand.Text+符号+rhsOperand.Text;result.Text=output.ToString();这一部分是所有方法中的一部分。我想做一个方法,然后调用它们,然后我需要它。好吧,你为什么不把这部分放在它自己的方法中呢?属于codereview.stackexchange.comi需要传输不同的符号什么类型是“符号”?符号的类型是这样的?如果没有,请详细说明目的和预期结果。private void subtractValues(){try{int lhs=int.Parse(lhsOperand.Text);int rhs=int.Parse(rhsOperand.Text);int output;output=lhs-rhs;expression.Text=lhsOperand.Text+“-”+rhsOperand.Text;result.Text=output.ToString();}catch(异常e){result.Text=e.ToString();}
private void MultiMethod(string sign)
{
    double lhs = double.Parse(lhsOperand.Text);
    double rhs = double.Parse(rhsOperand.Text);

    switch(sign)
    {
        case "+":
            outcome = lhs + rhs;
            break;
        case "-":
            outcome = lhs - rhs;
            break;
        case "*":
            outcome = lhs * rhs;
            break;
        case "%":
            outcome = lhs % rhs;
            break;
        case "/":
            outcome = lhs / rhs;
            break;
    }

    expression.Text = lhsOperand.Text + sign + rhsOperand.Text;
    result.Text = outcome.ToString();
}