博客
关于我
Leedcode2-后缀表达式结果
阅读量:802 次
发布时间:2023-01-30

本文共 1745 字,大约阅读时间需要 5 分钟。

evaluation of arithmetic expressions. The valid operators include addition, subtraction, multiplication, and division. Each operand can be either an integer or another expression.

Examples

The expression ["2", "1", "+", "3", "*"] evaluates to ((2 + 1) * 3), resulting in 9. Similarly, the expression ["4", "13", "5", "/", "+"] evaluates to (4 + (13 / 5)), which equals 6.

Compiler Implementation

The implementation of the compiler involves two main approaches: Forward Analysis and Backward Analysis.

  • Forward Analysis

    • Traverse the expression from left to right.
    • Maintain a stack to store intermediate results.
    • For each operator, apply it to the top of the stack and the current operand.
    • Example: ["2", "1", "+", "3", "*"]
      • Traverse 2, push to stack.
      • Traverse 1, push to stack.
      • Traverse "+", pop 1 and 2, compute 3, push result.
      • Traverse 3, push to stack.
      • Traverse "*", pop 3 and 3, compute 9, push result.
  • Backward Analysis

    • Traverse the expression from right to left.
    • Use a stack to store intermediate results.
    • For each operator, pop operands from the stack and apply the operator.
    • Example: ["4", "13", "5", "/", "+"]
      • Traverse 4, push to stack.
      • Traverse 13, push to stack.
      • Traverse "5", push to stack.
      • Traverse "/", pop 5 and 13, compute 2.6, push result.
      • Traverse "+", pop 2.6 and 4, compute 6.6, push result.
  • How to use the code

    • Input expressions as vectors of strings.
    • Use strlen to determine string lengths.
    • Use strtol or stod for type conversion of operands and results.

    Status

    The current implementation supports basic arithmetic operations and ensures correct computation of expressions. Further works can include:

    • Addition of floating-point numbers.
    • Handling of parentheses and operator precedence.
    • Error checking for invalid expressions.

    转载地址:http://etgyk.baihongyu.com/

    你可能感兴趣的文章
    OpenCV与AI深度学习 | 2024年AI初学者需要掌握的热门技能有哪些?
    查看>>
    OpenCV与AI深度学习 | CIB-SE-YOLOv8: 优化的YOLOv8, 用于施工现场的安全设备实时检测 !
    查看>>
    OpenCV与AI深度学习 | CoTracker3:用于卓越点跟踪的最新 AI 模型
    查看>>
    OpenCV与AI深度学习 | OpenCV中八种不同的目标追踪算法
    查看>>
    OpenCV与AI深度学习 | OpenCV图像拼接--Stitching detailed使用与参数介绍
    查看>>
    OpenCV与AI深度学习 | OpenCV如何读取仪表中的指针刻度
    查看>>
    OpenCV与AI深度学习 | OpenCV常用图像拼接方法(一) :直接拼接
    查看>>
    OpenCV与AI深度学习 | OpenCV常用图像拼接方法(三):基于特征匹配拼接
    查看>>
    OpenCV与AI深度学习 | OpenCV常用图像拼接方法(二) :基于模板匹配拼接
    查看>>
    OpenCV与AI深度学习 | OpenCV常用图像拼接方法(四):基于Stitcher类拼接
    查看>>
    OpenCV与AI深度学习 | OpenCV快速傅里叶变换(FFT)用于图像和视频流的模糊检测(建议收藏!)
    查看>>
    OpenCV与AI深度学习 | PaddleOCR 2.9 发布, 正式开源文本图像智能分析利器
    查看>>
    OpenCV与AI深度学习 | SAM2(Segment Anything Model 2)新一代分割一切大模型介绍与使用(步骤 + 代码)
    查看>>
    OpenCV与AI深度学习 | T-Rex Label !超震撼 AI 自动标注工具,开箱即用、检测一切
    查看>>
    OpenCV与AI深度学习 | YOLO11介绍及五大任务推理演示(目标检测,图像分割,图像分类,姿态检测,带方向目标检测)
    查看>>
    OpenCV与AI深度学习 | YOLOv10在PyTorch和OpenVINO中推理对比
    查看>>
    OpenCV与AI深度学习 | YOLOv11来了:将重新定义AI的可能性
    查看>>
    OpenCV与AI深度学习 | YOLOv8自定义数据集训练实现火焰和烟雾检测(代码+数据集!)
    查看>>
    OpenCV与AI深度学习 | YOLOv8重磅升级,新增旋转目标检测,又该学习了!
    查看>>
    OpenCV与AI深度学习 | 一文带你读懂YOLOv1~YOLOv11(建议收藏!)
    查看>>