[HELP] Manipulating Bit Patterns

Hey, I need some help on this program.

I am supposed to read a file like this:

CONVERT 00000000
NOT 00000000
AND 00001111 11110000
OR 00001111 11110000
CONVERT 11111111
IR 11111111 00000000
NOT 11001100
LSHIFT 11001101 3
CONVERT 01011101
AND 10111011 00111000
LSHIFT 11001101 8
OR 10000001 10011001
CONVURT 10101010
LSHIFT 11111111 1


With the first word being the bitwise command.

How would I go about writing this program?
I'm gonna do this really roughly, and i'll try to elaborate on it more a bit later because i don't have too much time at the moment.

First thing's first, you need to figure out how you want to read the information from the text file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>

using namespace std

int main()
{
    string line;
    ifstream file("something.txt");
    
    if(file.is_open())
    {
        while(file.good())
        {
            getline(file, line);
            processLine(line); //this function we'll use to handle the specific line.
        }
    } else
        cout << "Error opening file." << endl;

    return 0;
}


From there i'd write a function to split a string, into a vector of its words.
vector<string> splitString(string s);
so if we had the string "the quick brown fox," the function would find all the white spaces in the line (using string::find). You would find white spaces at indexes 3, 9, and 15. Then use string::substr to get substrings from 0-3, 3-9, 9-15, 15-std::string::npos. Put the results in a vector and return it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void processLine(string s)
{
    vector<string> words = splitString(s);
    //So we know that the first word in the vector will be the operation. 
    if(words[0] == "CONVERT")
    {
        //do what you need to do to convert here
    }
    else if(words[0] == "NOT")
    {
        //NOT logic here
    }
    else if(words[0] == "AND")
    {
        //convert words[1], and words[2] into an integer value (perhaps using stringstream?)
        int a, b;
        cout << a & b << endl;
    }

    //and so on...
}


That's how I would begin to approach the problem.

Best of luck!
-Thumper
Topic archived. No new replies allowed.