FaRKle0079
2[H]4U
- Joined
- Jan 11, 2005
- Messages
- 2,686
Hey all,
So I'm having a difficult time conceptually figuring out how I'm going to be parsing input commands for one of my C++ class' assignments.
The input commands can have some of these following forms (# = a number)::
(command # # #)(command #)(command (command (command #) # #)( command #) #)
The program keeps reading till EOF, so input can either be from a file or from a user typing on his or her keyboard. There are a bunch of different commands, and each command has a specified amount of numbers following it. All commands are enclosed in ( ), but there can be any amount of whitespace or the character '0' between commands.
The first way I thought of doing this was just passing everything into a string via cin since it would automatically get rid of the whitespace. That way would force me to make multiple cases however in case there were 0's between the commands and it requires me to make multiple cases for the last # in each command since it may or may not be right up against a ) to end the command. I'd go from reading all the contents of each cin to reading it character by character, storing the indexes of the beginning and end of what I want, and then putting that into a substring.
The commands furthest inside the ( ) will be executed first and then it'll work its way out to the outer commands. The way this is set up has me thinking of trying to store the commands and their various parameters in a binary tree. Because of the inside to out structure it also seems like recursion would work well for executing the commands (still researching on how to do this though).
Would you guys have any methods you suggest I look into on how to parse/input these commands? Using cin will probably work, but I'm wondering if there are any tips I haven't learned yet.
So I'm having a difficult time conceptually figuring out how I'm going to be parsing input commands for one of my C++ class' assignments.
The input commands can have some of these following forms (# = a number)::
(command # # #)(command #)(command (command (command #) # #)( command #) #)
The program keeps reading till EOF, so input can either be from a file or from a user typing on his or her keyboard. There are a bunch of different commands, and each command has a specified amount of numbers following it. All commands are enclosed in ( ), but there can be any amount of whitespace or the character '0' between commands.
The first way I thought of doing this was just passing everything into a string via cin since it would automatically get rid of the whitespace. That way would force me to make multiple cases however in case there were 0's between the commands and it requires me to make multiple cases for the last # in each command since it may or may not be right up against a ) to end the command. I'd go from reading all the contents of each cin to reading it character by character, storing the indexes of the beginning and end of what I want, and then putting that into a substring.
The commands furthest inside the ( ) will be executed first and then it'll work its way out to the outer commands. The way this is set up has me thinking of trying to store the commands and their various parameters in a binary tree. Because of the inside to out structure it also seems like recursion would work well for executing the commands (still researching on how to do this though).
Would you guys have any methods you suggest I look into on how to parse/input these commands? Using cin will probably work, but I'm wondering if there are any tips I haven't learned yet.
Last edited: