簡體   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