COMMAND Operand #1 Operand #2 Shift Result
----------------------------------------------------------
NOT 11010011 00101100
AND 10010010 11001110 10000010
OR 10011001 11100101 11111101
CONVERT 10010110 150
LSHIFT 11001101 3 01101000
Now I need to input the below data, which comes from a text file, into my program to look like the above output:
NOT 11010011
AND 10010010 11001110
OR 10011001 11100101
CONVERT 10010110
LSHIFT 11001101 3
What c++ code do I use to input the data from this text file and get it to output the result which I need in the very first example with the command, operand 1, operand 2, shift, and result? Maybe a SWITCH but I don't know?
Thank you for any help possible...greatly appreciated
Your input is in prefix notation. That is, you have the operation first, then the operand(s). That is, in fact, the most convenient method of receiving input.
You need a set of handlers for NOT, AND, OR, CONVERT, LSHIFT, RSHILT, ...
You'll also need a function that can read in binary digits and one that can write them out.
The main loop reads the operand and calls the appropriate handler. The handler reads the input it requires, does its computation and writes the output.
I wrote a simple function to read one line from the input file and store the values in the struct.
After that I had a separate function to interpret what action was required. I did indeed use a switch-case to carry out the action.
I also used the strtol() function to convert the binary values to a long int, and a short function which I wrote to output the long int as a string of binary digits.
I suppose the main principle is to break down the large task into smaller, more manageable chunks. Then instead of being overwhelmed by the size of the complete program, you only need to focus attention on each small subtask. Using separate functions is a good way to do that.
It might be feasible to carry out the actions without conversion from string to integer and back again. There's more than one way to look at this.