简体   繁体   中英

Calculator to calculate multiple equations

I'm really new to C# and needed to create a calculator class that does multiple equations. For example 4+5*8-3/2. Why it doesn't work? It only does additions and I don't know how to make it do subtraction or do multiplication. Here is the code I have already created, but it doesn't work. Could somebody please help me. It only adds numbers and nothing else. I have tried absolutely everything.

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;
        }
    }
}

You must perform an operation of the left value and right value of the operator.

Your loop proceeds one by one to read the characters, you have to memorize the operator because you don't know the right operand.

public char LastOperator { get; set; }

/* ... */

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

As soon as stack reaches two values in its list (with the Count property), you can perform the calculation:

var result = PerformCalculation(LastOperator, stack);

This new result must be inserted into the stack as the left operator, having previously cleared your stack with the Clear method.

In the case of an empty stack at the beginning of your program, you just have to insert the value in stack.

Note: I don't want to give you the solution if it is a duty in which one learns by seeking, but leads.

Anyway, you don't have to touch the PerformCalculation method, it works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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