Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I already have this code and I need a main to run it. It should read in a file l

ID: 3639677 • Letter: I

Question

I already have this code and I need a main to run it. It should read in a file like this:

5
00101011

and it should do the operations based on the code

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cctype>

using namespace std;

char notZeroOrOneToZero(char);
typedef enum { NoMove, Left, Right } Move;

class Instruction {
public:
Instruction() : _bit('0'), _dir(NoMove), _nextRow(0) { }
Instruction(const string &instr) : _bit('0'), _dir(NoMove), _nextRow(0) {
if ((instr[0] == '0') || (instr[0] == '1')) {
_bit = instr[0];
}
switch (instr[1]) {
case 'L': _dir = Left; break;
case 'R': _dir = Right; break;
case 'S': _dir = NoMove; break;
default: break;
}
if (isdigit(instr[2])) {
_nextRow = instr[2] - '0';
}
}
inline char bit() const { return _bit; }
inline Move dir() const { return _dir; }
inline int nextRow() const { return _nextRow; }
private:
char _bit;
Move _dir;
int _nextRow;
};

class BitStream {
public:
BitStream() { _theBits[0] = '0'; _pos = 1; }
BitStream(const string &s, size_t p) : _theBits(s.begin(), s.end()), _pos(p) {
// make sure pos and theBits are valid
if (_theBits.size() == 0) { _theBits[0] = '0'; }
_pos = min(_pos, _theBits.size());
transform(_theBits.begin(), _theBits.end(), _theBits.begin(), notZeroOrOneToZero);
}
void nextState(const Instruction &i) {
_theBits[_pos - 1] = i.bit();
if (i.dir() == Left) {
if (_pos > 0) --_pos;
} else if (i.dir() == Right) {
if (_pos < _theBits.size() - 1) ++_pos;
}
}
void display() const {
cout << _pos << endl;
copy(_theBits.begin(), _theBits.end(), ostream_iterator<char>(cout, ""));
cout << endl;
}
private:
vector<char> _theBits;
size_t _pos;
};

char notZeroOrOneToZero(char c) {
char c1 = c;
if ((c != '0') && (c != '1')) {
c1 = '0';
}
return c1;
}
//NEED MAIN HERE
int main ()
{

}

Explanation / Answer

#include #include #include #include #include #include using namespace std; char notZeroOrOneToZero(char); typedef enum { NoMove, Left, Right } Move; class Instruction { public: Instruction() : _bit('0'), _dir(NoMove), _nextRow(0) { } Instruction(const string &instr) : _bit('0'), _dir(NoMove), _nextRow(0) { if ((instr[0] == '0') || (instr[0] == '1')) { _bit = instr[0]; } switch (instr[1]) { case 'L': _dir = Left; break; case 'R': _dir = Right; break; case 'S': _dir = NoMove; break; default: break; } if (isdigit(instr[2])) { _nextRow = instr[2] - '0'; } } inline char bit() const { return _bit; } inline Move dir() const { return _dir; } inline int nextRow() const { return _nextRow; } private: char _bit; Move _dir; int _nextRow; }; class BitStream { public: BitStream() { _theBits[0] = '0'; _pos = 1; } BitStream(const string &s, size_t p) : _theBits(s.begin(), s.end()), _pos(p) { // make sure pos and theBits are valid if (_theBits.size() == 0) { _theBits[0] = '0'; } _pos = min(_pos, _theBits.size()); transform(_theBits.begin(), _theBits.end(), _theBits.begin(), notZeroOrOneToZero); } void nextState(const Instruction &i) { _theBits[_pos - 1] = i.bit(); if (i.dir() == Left) { if (_pos > 0) --_pos; } else if (i.dir() == Right) { if (_pos < _theBits.size() - 1) ++_pos; } } void display() const { cout