在这种情况下,我们必须从一个名为 small.txt 的文件中读取。该文件的内容如下所示:
NOT 11010011
AND 10010010 11001110
OR 10011001 11100101
CONVERT 10010110
LSHIFT 11001101 3
WRONG 01010100 10101010
每个单词都表示该文件中新行的开始。我遇到的问题是让我的代码在最后(第 3 列)中读取。每列长度为十个空格。以下是我必须遵守的规则:
根据文件中的命令,对命令后面的操作数进行操作。每个命令都独立于另一个命令,因此一个命令不能影响另一个命令。
这些命令都是二进制命令,但你不能为此使用 C++ 的内置命令。这意味着没有:“~、&、|、<<(用作二元运算符时)”。你当然可以使用 && 和 ||,它们是不同的。<< 可以像往常一样用于 cout,只是不能用作二元运算符。
您不能使用字符串来读取操作数。
我努力了:
inputFile.ignore(20, '\n')
但如果这是正确的方法。我没有正确使用它。
这是我的代码:
/*
====================================================================
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