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