使用递归的计算器

时间:2015-05-19 20:28:48

标签: c#

这是我的代码,我试图做一个基本的计算器。只有一个文本框,我会写两个数字并添加它们,并像大多数计算器一样将结果放在同一个文本框中。 我的问题是我收到了一个错误:

  

未处理的类型' System.StackOverflowException'   发生在WindowsFormsApplication1.exe

按下按钮+后写下一个号码。

public class calculator
{
    int acum = 0;
    int calcule(int option, int number)
    {
        switch (option)
        {
            case 3:
                acum = acum + number;
                break;
            case 4:
                acum = acum - number;
                break;
            case 5:
                acum = acum * number;
                break;
            case 6:
                acum = acum / number;
                break;
            default:
                break;
        }
        if (number == 0)
        {
            return acum;
        }
        else
        {
            return calculate(option, number);
        }
    }
}

private void btnadd_Click(object sender, EventArgs e)
{
    int numero1 = Convert.ToInt32(txtnumber.Text);

    calculadora calcular = new calculadora();

    txtnumber.Text = calculator.calculate(btnadd.TabIndex, number).ToString();
}

private void btnminus_Click(object sender, EventArgs e)
{
    int numero1 = Convert.ToInt32(txtnumber.Text);

    calculadora calcular = new calculadora();

    txtnumber.Text = calculator.calculate(btnminus.TabIndex, number).ToString();
}

private void button1_Click(object sender, EventArgs e)
{
    int number = Convert.ToInt32(txtnumber.Text);

    calculadora calcular = new calculadora();

    txtnumber.Text = calculator.calculate(button1.TabIndex, number).ToString();
}

private void button2_Click(object sender, EventArgs e)
{
    int numero1 = Convert.ToInt32(txtnumber.Text);

    calculadora calcular = new calculadora();

    txtnumber.Text = calculator.calculate(button2.TabIndex, number).ToString();
}

1 个答案:

答案 0 :(得分:2)

首先,您的代码中存在许多问题:

  1. acum不是静态的,每次你做计算器calc = new Calculator();该值设置为零。因此,即使代码中没有错误,结果也会是数字。
  2. arg编号的值在计算方法中永远不会改变,所以在这里输入else后,你无法逃脱递归调用(=> StackOverflowException) if (number == 0) { return acum; } else { return calculate(option, number); }
  3. 并且计算(...)是私有的......您无法在类计算器
  4. 之外访问它

    试试这个:

        enum OperationEnum
        {
            ADD=3,
            SUB=4,
            MUL=5,
            DIV=6
        }
        public class Calculator
        {
            public double Calculate(OperationEnum operation, params int[] operands)
            {
                if (operands == null)
                    throw new InvalidOperationException();
                if (operands.Length == 0)
                        return 0;
                if (operands.Length == 1)
                    return operands[0];
                switch (operation)
                {
                    case OperationEnum.ADD:
                        return Add(operands);
                    case OperationEnum.SUB:
                        return Subtract(operands);
                    case OperationEnum.MUL:
                        return Multiply(operands);
                    case OperationEnum.DIV:
                        return Divide(operands);
                    default:
                        throw new ArgumentException("operation");
                }
            }
            private double Divide(int[] operands)
            {
                if (operands.Length == 0)
                    return 0;
                var result = operands[0];
                for (int i = 1; i < operands.Length; i++)
                {
                    double divider = operands[i];
                    if (divider == 0)
                    {
                        throw new DivideByZeroException();
                    }
                    result /= divider;
                }
                return result;
            }
            private double Multiply(int[] operands)
            {
                if (operands.Length == 0)
                    return 0;
                double result = operands[0];
                for (int i = 1; i < operands.Length; i++)
                {
                    result *= operands[i];
                }
                return result;
            }
            private double Subtract(int[] operands)
            {
                if (operands.Length == 0)
                    return 0;
                var result = operands[0];
                for (int i = 1; i < operands.Length; i++)
                {
                    result -= operands[i];
                }
                return result;
            }
            private int Add(int[] operands)
            {
                return operands.Sum();
            }
        }
        private double _accumulator = 0;
        private void btnadd_Click(object sender, EventArgs e)
        {
            int numero1 = Convert.ToInt32(txtnumber.Text);
            Calculator calcular = new Calculator();
            _accumulator = calcular.Calculate(OperationEnum.ADD,_accumulator, number);
            txtnumber.Text = _accumulator.ToString()
        }