C# 实现加减乘除 (备忘)

请给出 计算表达式 (4+5.0)/ 4.2 * (3+5.3)

using System;
using System.Collections.Generic;

public class ExpressionEvaluator
{
    public static double Evaluate(string expression)
    {
        var operands = new Stack<double>();
        var operators = new Stack<char>();

        for (int i = 0; i < expression.Length; i++)
        {
            char ch = expression[i];
            if (ch == ' ')
                continue;

            if (ch == '(')
            {
                operators.Push(ch);
            }
            else if (char.IsDigit(ch) || ch == '.')
            {
                string operand = ch.ToString();
                while (i + 1 < expression.Length && (char.IsDigit(expression[i + 1]) || expression[i + 1] == '.'))
                {
                    operand += expression[i + 1];
                    i++;
                }
                operands.Push(double.Parse(operand));
            }
            else if (ch == ')')
            {
                while (operators.Peek() != '(')
                {
                    ProcessOperator(operands, operators);
                }
                operators.Pop(); // Remove '('
            }
            else
            {
                while (operators.Count > 0 && Precedence(operators.Peek()) >= Precedence(ch))
                {
                    ProcessOperator(operands, operators);
                }
                operators.Push(ch);
            }
        }

        while (operators.Count > 0)
        {
            ProcessOperator(operands, operators);
        }

        return operands.Pop();
    }

    private static int Precedence(char op)
    {
        if (op == '+' || op == '-')
            return 1;
        if (op == '*' || op == '/')
            return 2;
        return 0;
    }

    private static void ProcessOperator(Stack<double> operands, Stack<char> operators)
    {
        char op = operators.Pop();
        double operand2 = operands.Pop();
        double operand1 = operands.Pop();
        double result = ApplyOperator(op, operand1, operand2);
        operands.Push(result);
    }

    private static double ApplyOperator(char op, double operand1, double operand2)
    {
        switch (op)
        {
            case '+':
                return operand1 + operand2;
            case '-':
                return operand1 - operand2;
            case '*':
                return operand1 * operand2;
            case '/':
                if (operand2 == 0)
                    throw new DivideByZeroException("Division by zero");
                return operand1 / operand2;
            default:
                throw new ArgumentException("Invalid operator");
        }
    }

    public static void Main(string[] args)
    {
        string expression = "(4+5.0)/4.2*(3+5.3)";
        double result = Evaluate(expression);
        Console.WriteLine($"Result of expression {expression} is {result}");
    }
}

相关推荐

  1. C# 实现乘除

    2024-05-12 12:32:07       35 阅读
  2. php乘除函数

    2024-05-12 12:32:07       64 阅读
  3. 高精度乘除

    2024-05-12 12:32:07       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-12 12:32:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-12 12:32:07       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-12 12:32:07       82 阅读
  4. Python语言-面向对象

    2024-05-12 12:32:07       91 阅读

热门阅读

  1. 计算机视觉教学实训解决方案

    2024-05-12 12:32:07       36 阅读
  2. 1080:余数相同问题

    2024-05-12 12:32:07       29 阅读
  3. [C/C++] -- 适配器模式

    2024-05-12 12:32:07       36 阅读
  4. 整体意义的构成与构建

    2024-05-12 12:32:07       41 阅读
  5. 【负载均衡式在线OJ项目day5】OJ服务模块概要

    2024-05-12 12:32:07       34 阅读
  6. 复习用到知识(asp.net)

    2024-05-12 12:32:07       36 阅读
  7. sass详解与使用

    2024-05-12 12:32:07       34 阅读
  8. Ubuntu设置中午输入法

    2024-05-12 12:32:07       36 阅读
  9. XML 解析器

    2024-05-12 12:32:07       28 阅读