We have to read from a file in this case called small.txt. The contents of the file look like the following:
NOT 11010011
AND 10010010 11001110
OR 10011001 11100101
CONVERT 10010110
LSHIFT 11001101 3
WRONG 01010100 10101010
Every word indicates the beginning of a new line in this file. The problem I'm having is getting my code to read in the last (3rd column). Each column is ten spaces in length. The following are the rules I have to follow:
Based on the commands in the file, perform operations on the operands following the command. Each command is independent from another, so one command cannot affect another.
The commands are all binary commands, but you can't use C++'s built in commands for this. This means no: “~, &, |, << (when used as a binary operator)”. You can of course use && and ||, these are different. << Can be used for cout as always, just not as a binary operator.
you cannot use strings to read in the operands.
I have tried:
inputFile.ignore(20, '\n')
but if that is the right way. I'm not using it correctly.
Here is my code:
/*
====================================================================
SUMMARY
Read commands which will require the program to perform some operation on either one or two bit
patterns, determine the result of the operation, and output accordingly.
NOT:
Takes 1 operand and performs a bitwise logical NOT. At each position, if the operand has a 0,
the result will contain a
1. If the operand has a 1, the result will contain a 0.
Eg. operand 11010011
result 00101100
In logical operations, a 1 represents TRUE and a 0 FALSE.
AND:
Takes 2 operands and performs a bitwise logical AND. At each position, if both operand 1 and
operand 2 contain a 1, the result will be a 1. Otherwise the result is 0.
Eg. operand 1 10010010
operand 2 11001110
result 10000010
OR:
Takes 2 operands and performs a bitwise logical OR. At each position, if either operand 1 or
2 or both contain a 1, the result will contain a 1. Otherwise the result will contain a 0
(inclusive OR).
Eg. operand 1 10011001
operand 2 11100101
result 11111101
CONVERT:
Takes 1 operand and converts it to a base 10 integer. Note: we will let every bit in these
binary numbers represent part of a positive binary integer, i.e. there is no "sign" bit.
Thus we can only represent positive integers in the range from 0 thru (28 - 1).
Eg. operand 10010110
result = (1 * 2**7) + (0 * 2**6) + (0 * 2**5) + (1 * 2**4)
+ (0 * 2**3) + (1 * 2**2) + (1 * 2**1) + (0 * 2**0) = 150 in base 10
LSHIFT:
Logical Shift to Left Takes 1 operand and an integer N as input. The bit values are shifted N
positions to the left. Data "pushed off" the left end is lost. Zeroes replace the lost bits.
Eg. operand 1 11001101 N = 3
result 01101000
You may assume N is valid, i.e. 0 <= N <= 8
======================================================================
ASSUMPTIONS
The binary operands will contain exactly 8 bits, where a bit is a binary digit. A byte contains
8 bits.
Check for invalid command names. Assume that the binary operands are all correctly given in the
data file.
======================================================================
INPUT
From the data file binaryData.txt.
=======================================================================
OUTPUT
Echo print all input values. Then output in a suitable fashion the results of the operation
performed, and any necessary error messages.
*/
/* ========================================================================================*/
/* HEADER FILES */
#include <iomanip> // needed for output manipulation
#include <iostream> // needed for standard I/O routines
#include <fstream>
#include <string> // needed for reading data from files
using namespace std;
/* ====================================================================================*/
/* FUNCTION /* ================================================================== */
/* NAMED GLOBAL CONSTANTS */
const int ARRAY_SIZE = 8; // array size
/* =========================================================================== */
/* MAIN FUNCTION */
int main (){
int numbers[ARRAY_SIZE]; // array with 8 elements
int secondArray[ARRAY_SIZE]; // array two with 8 elements
int thirdArray[ARRAY_SIZE]; // array three with 8 elements
int count = 0; // loop counter variable
ifstream inputFile; // input file stream object
// open the file
inputFile.open ("small.txt");
// exit if a fatal error occurs opening the file
if( !inputFile ){
cout << "Error: Data file could not be opened \n";
system ("pause");
return (EXIT_FAILURE);
} // end of not in file if statement
// read the array
string word;
while(inputFile){
// stores the word read in by the file
inputFile >> word;
if(word == "NOT"){
cout << word << " ";
// This allows you to be able to read in each number one by one
for(int i = 0; i < ARRAY_SIZE; i++){
char letter(20);
inputFile >> letter;
letter = letter - '0';
numbers[i] = static_cast<int>(letter);
if (letter == 0){
numbers[i] = letter + 1;
}
else{
numbers[i] = letter - 1;
}
} // end of first for loop
for(int i = 0; i < ARRAY_SIZE; i++){
cout << numbers[i];
} // end of second for loop
} // end of if word == not check
if(word == "AND"){
cout << "\n\n" << word << " ";
// This allows you to be able to read in each number one by one
for(int i = 0; i < ARRAY_SIZE; i++){
//inputFile.ignore(20, '\n');
char letter;
inputFile.ignore(10) >> letter;
letter = letter - '0';
secondArray[i] = static_cast<int>(letter);
} // end of first for loop
for(int i = 0; i < ARRAY_SIZE; i++){
cout << secondArray[i];
} // end of second for loop
} // end of if word == not check
} // end of while inputFile loop
inputFile.clear ( );
inputFile.close ( );
cout << endl;
system ("pause");
return (0);
} // end of main function
Your code has already consumed the first two columns from the stream and the position of the stream is past those columns. In turn, it isn't necessary to call ignore to move the stream position past those columns. Besides ignore ifstream provides some additional functions that you may find useful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.