Skip to content

Instantly share code, notes, and snippets.

@chenyuxiang0425
Created August 21, 2020 16:57
Show Gist options
  • Save chenyuxiang0425/ffca8f4835f1e0b73e2ee5cfbcaebcbb to your computer and use it in GitHub Desktop.
Save chenyuxiang0425/ffca8f4835f1e0b73e2ee5cfbcaebcbb to your computer and use it in GitHub Desktop.
栈的应用——算术表达式求值(1+(2+3)*(4+5))
  • 算术表达式求值

算术表达式求值,(1+(2+3)*(4+5))

  • 用两个栈,一个保存运算符,一个保存操作数
  • 将操作数压入操作数栈
  • 将运算符压入运算符栈
  • 忽略左括号
  • 遇到右括号,弹出一个运算符,弹出所需要的操作数,并将运算符和操作数运算的结果压入操作数栈

计算实例

  • (1+((2+3)*(4+5)))
  • (1+((5)*(4+5)))
  • (1+(5*9))
  • (1+45)
  • 46 最后操作数栈只有一个值
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment