1

この場合、small.txt というファイルから読み取る必要があります。ファイルの内容は次のようになります。

NOT      11010011
AND      10010010  11001110
OR       10011001  11100101
CONVERT  10010110
LSHIFT   11001101  3
WRONG    01010100  10101010

すべての単語は、このファイルの新しい行の始まりを示します。私が抱えている問題は、コードを最後 (3 列目) で読み取ることです。各列の長さは 10 個のスペースです。私が守らなければならないルールは次のとおりです。

ファイル内のコマンドに基づいて、コマンドに続くオペランドに対して操作を実行します。各コマンドは互いに独立しているため、1 つのコマンドが別のコマンドに影響を与えることはありません。

コマンドはすべてバイナリ コマンドですが、これには C++ の組み込みコマンドを使用できません。これは、「~、&、|、<< (2 項演算子として使用する場合)」を意味しません。もちろん、&& と || を使用できますが、これらは異なります。<< 二項演算子としてではなく、いつものように 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
4

1 に答える 1

0

コードは既にストリームの最初の 2 列を消費しており、ストリームの位置はそれらの列を過ぎています。次に、これらの列を超えてストリーム位置を移動するために ignore を呼び出す必要はありません。ignore ifstreamに加えて、便利な追加機能がいくつか提供されます。

于 2012-11-13T01:47:36.460 に答える