C++:如何使用堆栈搜索引用 [英] C++: How to search for quoatations using stacks
问题描述
我必须创建一个程序来读取文本文件并检查匹配的括号、方括号、[]
、引号和块注释.如果不平衡则返回 false,如果平衡则返回 true.
I have to create a program that reads in a text file and checks for matching parenthesis, brackets, []
, quote symbols, and block comment. If there is an imbalance it returns false, if it is balanced it returns true.
我已经完成了 [] () {}
并且需要帮助使其适用于 /* */
、" "
和 ' '
其中单/双引号内的所有内容和块注释都将被忽略.我尝试了很多东西,但都没有成功.
I have done [] () {}
and need help making it work for /* */
, " "
, and ' '
where everything inside the single/double quotes and the block comments are ignored. I've tried many things but have been unsuccessful in getting them to work.
推荐答案
程序现在需要处理两种状态/模式而不是一种:
The program now needs to handle two states/modes instead of one:
- 它以匹配模式"开始,将
( { [
放在堆栈上,如果匹配则弹出) } ]
. - 一旦你读到一个
/*
,你就会进入评论模式"并忽略一切,直到你读到*/
,此时你会回到匹配模式"" 与之前的堆栈."
和'
也是如此.
- It starts in "matching mode", putting
( { [
on the stack and popping) } ]
if they match. - As soon as you read a
/*
, you enter "comment mode" and ignore everything until you read*/
, at which point you return to "matching mode" with the previous stack. The same goes for"
and'
.
如果您在评论模式"下到达输入的结尾,您会打印出:不平衡符号"以及使您进入该模式的符号.
If you reach end of input while in "comment mode" you print out: "unbalanced symbol" with the symbol that made you enter that mode.
这篇关于C++:如何使用堆栈搜索引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!