问题
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