C++: How to search for quoatations using stacks

坚强是说给别人听的谎言 提交于 2019-12-11 16:12:28

问题


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.


回答1:


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.




回答2:


you just have to do the same thing you have done with the parenthesis create stack for the opening and closing and then pop each stack one by one and then check the validation by comparing the popped values



来源:https://stackoverflow.com/questions/35948673/c-how-to-search-for-quoatations-using-stacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!