gpt4 book ai didi

c++ - 处理标准一维数组时忽略前导空格

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:48 25 4
gpt4 key购买 nike

在这种情况下,我们必须从一个名为 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

最佳答案

您的代码已经使用了流中的前两列,并且流的位置超过了这些列。反过来,不必调用 ignore 来将流位置移动到这些列之后。除了忽略ifstream提供了一些您可能会觉得有用的附加功能。

关于c++ - 处理标准一维数组时忽略前导空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13353821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com