繁体   English   中英

计算多个方程式的计算器

[英]Calculator to calculate multiple equations

我真的是 C# 的新手,需要创建一个执行多个方程式的计算器类。 例如 4+5*8-3/2。 为什么它不起作用? 它只会做加法,我不知道如何让它做减法或乘法。 这是我已经创建的代码,但它不起作用。 有人可以帮帮我吗? 它只添加数字,没有别的。 我已经尝试了一切。

namespace Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            var expression = "4+5*8";

            var calculator = new Calculator();

            foreach (var chr in expression)
            {
                calculator.KeyPress(chr);
            }

            Console.WriteLine(calculator.Value);
        }
    }

    public class Calculator
    {
        public double? Value { get; set; }
        private Stack<double> stack = new Stack<double>();

        public void KeyPress(char key)
        {
            if (Char.IsDigit(key))
            {
                // If the key is a digit, add it to the current value
                if (Value == null)
                {
                    Value = Char.GetNumericValue(key);
                }
                else
                {
                    Value = Value + Char.GetNumericValue(key);
                }
            }
            else if (key == '+' || key == '-' || key == '*' || key == '/')
            {
                // If the key is an operator, perform the pending calculation
                // and store the result in the stack
                double result = PerformCalculation(key, stack);
                stack.Push(result);
            }
        }


        private double PerformCalculation(char op, Stack<double> stack)
        {
            double result = 0;
            if (stack.Count > 1)
            {
                // Pop the last two values from the stack
                double right = stack.Pop();
                double left = stack.Pop();

                // Perform the calculation based on the operator
                switch (op)
                {
                    case '+':
                        result = left + right;
                        break;
                    case '-':
                        result = left - right;
                        break;
                    case '*':
                        result = left * right;
                        break;
                    case '/':
                        result = left / right;
                        break;
                }
            }
            else if (stack.Count == 1)
            {
                // If there is only one value in the stack, store it as the result
                result = stack.Pop();
            }

            // Only push the result back into the stack if the Value property is not null
            if (Value != null)
            {
                stack.Push(result);
            }
            return result;
        }
    }
}

您必须对运算符的左值和右值进行运算。

您的循环一个接一个地读取字符,您必须记住运算符,因为您不知道正确的操作数。

public char LastOperator { get; set; }

/* ... */

else if (key == '+' || key == '-' || key == '*' || key == '/')
{
     LastOperator = key;
}

一旦堆栈达到其列表中的两个值(使用Count属性),您就可以执行计算:

var result = PerformCalculation(LastOperator, stack);

这个新结果必须作为左运算符插入到堆栈中,之前已经使用Clear方法清除了堆栈。

如果程序开始时堆栈为空,则只需将值插入堆栈即可。

注意:我不想给你解决方案,如果这是一个人通过寻求来学习的职责,而是引导。

无论如何,您不必接触PerformCalculation方法,它工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM