可以用栈来检查算术表达式中的括号是否匹配。分析算术表达式时,初始栈为空,从左到右扫描字符,遇到字符“(”就将其入栈,遇到“)”就执行出栈操作。对算术表达式“(a+b*(a+b))/c)+(a+b)”,检查时,( );对算术表达式“((a+b/(a+b)-c/a)/b”,检查时,(请作答此空)。这两种情况都表明所检查的算术表达式括号不匹配。
- A.栈为空却要进行出栈操作
- B.栈已满却要进行入栈操作
- C.表达式处理已结束,栈中仍留有字符“(”
- D.表达式处理已结束,栈中仍留有字符“)”
正确答案及解析
正确答案
解析
对算术表达式(a+b*(a+b))/c)+(a+b)进行括号检查时,栈操作的顺序为:入栈,入栈,出栈,出栈,出栈,……当需要进行第三次出栈操作时,发现栈已空,已不可能完成出栈操作。对算术表达式((a+b/(a+b)-c/a)/b进行括号检查时,栈操作的顺序为: 入栈,入栈,入栈,出栈,出栈第1次出栈操作取出的是第3次刚入栈的“(”,第2次出栈操作取出的是第2次入栈的“(”。直到表达式检查结束,第1次入栈的“(”仍没有取出,因此,表达式中的括号并不匹配。本例是栈的重要应用之例。编译程序中常用栈来检查语句(或程序)中的括号是否匹配。在日常生活中,当人们进入一个复杂的大院时,需要多次进门,多次出门。每次出门,总是退出最近进门的院。在同一位置堆积货物时,总是先取最近刚放上的货。所以,栈的应用也是比较常见的。可以用这种日常生活中的例子来形象地理解栈的操作。
你可能感兴趣的试题
( )is the process of transforming information so it is unintelligible to anyone but the intended recipient.
-
- A.Encryption
- B.Decryption
- C.Security
- D.Protection
- 查看答案
As each application module is completed,it undergoes( )to ensure that it operates correctly and reliably.
-
- A.unit testing
- B.integration testing
- C.system testing
- D.acceptance testing
- 查看答案
( )algorithm specifies the way to arrange data in a particular order.
-
- A.Search
- B.Random
- C.Sorting
- D.Merge
- 查看答案
After analyzing the source code,( )generates machine instructions that will carry out the meaning of the program at a later time.
-
- A.an interpreter
- B.a linker
- C.a compiler
- D.a converter
- 查看答案
( )can help organizations to better understand the information contained within the data and will also help identify the data that is most important to the business and future business decisions.
-
- A.Data processing system
- B.Big Data analytics
- C.Cloud computing
- D.Database management
- 查看答案