在這種情況下,我們必須讀取一個名爲small.txt的文件。該文件的內容如下所示:操作標準一維數組時忽略主要空格
NOT 11010011
AND 10010010 11001110
OR 10011001 11100101
CONVERT 10010110
LSHIFT 11001101 3
WRONG 01010100 10101010
每個單詞都指示此文件中新行的開頭。我遇到的問題是讓我的代碼在最後(第三列)中讀取。每列有十個空格。以下是我必須遵守的規則:
根據文件中的命令,對命令後面的操作數執行操作。每個命令都與另一個命令相互獨立,因此一個命令不會影響另一個。
這些命令都是二進制命令,但不能使用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
使用inputFile >> n1 >> n2不是更簡單嗎?在閱讀單詞後得到n1和n2是整數的兩個數字。 –