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

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

csharp 复制代码
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}");
    }
}
相关推荐
meilindehuzi_a4 小时前
从零理解并实现 RAG:用 LangChain.js 构建语义检索问答系统
开发语言·javascript·langchain
风流 少年4 小时前
Julia
开发语言·julia
江畔柳前堤5 小时前
GO01-Go 语言与主流编程语言深度对比
开发语言·人工智能·后端·微服务·云原生·golang·go
:-)5 小时前
算法-归并排序
java·开发语言·数据结构·算法·排序算法
GIS阵地6 小时前
QgsRasterDataProvider 完整详解(QGIS 3.40.13 C++)
开发语言·c++·qt·开源软件·qgis
yaoxin5211237 小时前
462. Java 反射 - 获取声明类与封闭类
java·开发语言·python
阿里嘎多学长7 小时前
2026-07-14 GitHub 热点项目精选
开发语言·程序员·github·代码托管
charlie1145141918 小时前
Cinux: 为大内核铺路
开发语言·c++·操作系统·现代c++
2501_914245938 小时前
C语言设计模式详解:从理论到实践的完整指南
c语言·开发语言·设计模式
Hazenix9 小时前
Go 指南:一篇文章速通 Golang
开发语言·后端·golang