C++: Stacks and Printing something out when you reach the end of a file or when reading the last character in a file

帅比萌擦擦* 提交于 2019-12-13 07:14:02

问题


I need to read from a file, character by character.

My specific problem:

If it finds a /*, it puts them into the stack, and then goes into "comment mode" where it ignores everything until it finds a */. If it never finds the matching pair */* in the entire file, it should print out "unbalanced symbol /" but it never prints that*


回答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. You don't need a stack while in this mode. 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.



来源:https://stackoverflow.com/questions/35951281/c-stacks-and-printing-something-out-when-you-reach-the-end-of-a-file-or-when

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