4

好的,我想我已经解决了大部分问题,但它不喜欢我传递我认为的常量。任何帮助将不胜感激,谢谢。

另外,对于 !inputFile 部分,我不确定如何像我的老师建议的那样完成返回(EXIT_FAILURE)。

另外,关于您只使用数组的一部分的建议,那仍然允许我在函数中使用整个东西吗?

该程序应该采用这样的文件:例如:NOT 11010001,它应该将命令作为字符串读取,将二进制文件作为数组读取,然后对二进制文件执行命令。

这里的代码只是主要的功能,只是不想一下子全部发送一个墙,如果这看起来不错,那么我会很乐意添加其余的。此外, void Operate () 函数几乎以一种或另一种方式调用所有其他函数......我不确定这是导致它的原因还是什么。打印表只有 cout 是一张用于放置所有信息的表。

他们的标题对我来说很奇怪,所以假设这些是正确的。

/* ========================================================================== */

/* Prototypes */

int Power (int, int);

int ReadFile (ifstream inputFile);

void Operate (const int, ifstream&, string, int, int, int);

void CommandNot (const int, int, int);

void CommandAnd (const int, int, int, int);

void CommandOr (const int, int, int, int);

int CommandConvert (const int, int, int);

void CommandLshift (const int, int, int, int);

void PrintTable ();

void PrintOperand (const int &, int);

int main ()
{

//Constants

const int BIT_SIZE = 8;


//Variables


string fileName = "binaryData.txt";

    int operandOne [BIT_SIZE];

    int operandTwo [BIT_SIZE];

    int operandResult [BIT_SIZE];

    ifstream inputFile;

    PrintTable ();
    Operate (BIT_SIZE, inputFile, fileName, operandOne[BIT_SIZE], operandTwo[BIT_SIZE], operandResult[BIT_SIZE]);

    return 0;
}

void PrintTable ()
{

    cout << "=================================================" << endl;
    cout << "=      Eight Bit Binary Number Manipulator      =" << endl;
    cout << "=================================================" << endl << endl;

    cout << setw(14) << "COMMAND" << "Operand #1" << "Operand #2" << "Shift" << "Result" << endl;
    cout << "----------------------------------------------------------------------" << endl;

}

void Operate (const int BIT_SIZE, ifstream& inputFile, string fileName, int operandOne[], int operandTwo[], int operandResult[])
{
    //Variables
    int count, shift;
    char myChar;
    string command;

    const int SIZE = BIT_SIZE;  //rename constant

    inputFile.open (fileName);

    if ( !inputFile )       //Check if file opened sucessfully
    {
        cout << "Error: Data file could not be opened" << endl;
    }

    while (inputFile)       //Read file, and apply commands
    {
        inputFile >> command;
        cout << command << endl;
        for ( count = 0;  count < SIZE; count++ )
        {
            inputFile >> myChar;
            operandOne[count] = myChar - '0';
        }

        if (command == "NOT")
        {
            CommandNot (BIT_SIZE, operandOne[BIT_SIZE], operandResult[BIT_SIZE]);
            PrintOperand (BIT_SIZE, operandResult[BIT_SIZE]);
        }
        else if (command == "AND")
        {
            count = 0;
            for ( count = 0;  count < SIZE; count++ )
            {
                inputFile >> myChar;
                operandTwo[count] = myChar - '0';
            }

            CommandAnd (BIT_SIZE, operandOne[BIT_SIZE], operandTwo[BIT_SIZE], operandResult[BIT_SIZE]);
            PrintOperand (BIT_SIZE, operandResult[BIT_SIZE]);
        }
        else if (command == "OR")
        {
            count = 0;
            for ( count = 0;  count < SIZE; count++ )
            {
                inputFile >> myChar;
                operandTwo[count] = myChar - '0';
            }

            CommandOr (BIT_SIZE, operandOne[BIT_SIZE], operandTwo[BIT_SIZE], operandResult[BIT_SIZE]);
            PrintOperand (BIT_SIZE, operandResult[BIT_SIZE]);
        }
        else if (command == "CONVERT")
        {
            CommandConvert (BIT_SIZE, operandOne[BIT_SIZE], operandResult[BIT_SIZE]);
            PrintOperand (BIT_SIZE, operandResult[BIT_SIZE]);
        }
        else if (command == "LSHIFT")
        {
            inputFile >> shift;
            CommandLshift (BIT_SIZE, operandOne[BIT_SIZE], operandResult[BIT_SIZE], shift);
            PrintOperand (BIT_SIZE, operandResult[BIT_SIZE]);
        }

        else
        {
            command = "INVALID";
            PrintOperand (BIT_SIZE, operandOne[BIT_SIZE]);
            cout << "--- ERROR! Invalid Command ---";
        }
    }

    inputFile.clear();
    inputFile.close();

    return ;
}

void CommandNot (const int BIT_SIZE, int operandOne[], int operandResult[])
{

    int count;
    const int SIZE = BIT_SIZE;

    for ( count = 0;  count < SIZE; count++ )
    {
        if (operandOne[count] == 0)
        {
            operandResult[count] = 1;
        }
        else
        {
            operandResult[count] = 0;
        }
    }

}

void CommandAnd (const int BIT_SIZE, int operandOne[], int operandTwo[], int operandResult[])
{

    int count;
    const int SIZE = BIT_SIZE;

    for ( count = 0;  count < SIZE; count++ )
    {
        if ((operandOne[count] == 1) && (operandTwo[count] == 1))
        {
            operandResult[count] = 1;
        }
        else
        {
            operandResult[count] = 0;
        }
    }

}

void CommandOr (const int BIT_SIZE, int operandOne[], int operandTwo[], int operandResult[])
{

    int count;
    const int SIZE = BIT_SIZE;

    for ( count = 0;  count < SIZE; count++ )
    {
        if ((operandOne[count] == 0) && (operandTwo[count] == 0))
        {
            operandResult[count] = 0;
        }
        else
        {
            operandResult[count] = 1;
        }
    }

}

int CommandConvert (const int BIT_SIZE, int operandOne[])
{

    int count;
    const int SIZE = BIT_SIZE;
    int baseTenResult = 0;
    int place;

    for ( count = 0;  count < SIZE; count++ )
    {
        place = SIZE - (count + 1);
        if (operandOne[count] == 1)
        {
            baseTenResult = baseTenResult + Power (2, place);
        }
        else 
        {
            continue;
        }
    }

    return baseTenResult;
}

void CommandLshift (const int BIT_SIZE, int operandOne[], int operandResult[], int shift)
{

    int count;
    const int SIZE = BIT_SIZE;
    int shiftStart = SIZE - shift;

    for ( count = 0;  count < SIZE-shift; count++ )
    {
        operandResult[count] = operandOne[count + shift];
    }

    for ( count = SIZE - shift; count < SIZE; count++ )
    {
        operandResult[count] = 0;
    }

}

int Power (int base, int power)
{
    int count;
    int result = 1;

    for ( count = 0; count < power; count++ )
    {
        result = result * base;
    }

    return result;
}

void PrintOperand (const int BIT_SIZE, int operandResult[])
{

    int count;
    const int SIZE = BIT_SIZE;

    for ( count = 0;  count < SIZE; count++ )
    {
        cout << operandResult[count];
    }

}
4

1 回答 1

4

您需要从 调用函数main。你不能通过重新声明它们来做到这一点:

void PrintTable();
void Operate (const int BIT_SIZE, ifstream& inputFile, string fileName, int operandOne[], int operandTwo[], int operandResult[]);

相反,您需要调用它们:

PrintTable();
Operate(BITSIZE,inputFile,fileName,operandOne,operandTwo,operandResult);

但是请注意,还有另一个问题:Operate最后需要三个整数参数,但您似乎尝试使用整数数组作为参数。您需要从每个数组中选择一个元素并仅将其作为参数提交。


(编辑)您的问题发生了一些变化,我还不够了解,无法说出您的数据和函数的最佳整体结构应该是什么,但是如果您正在处理整数数组并且您想通过整个数组到一个函数,你可以这样做(我已经将它简化为一个只接受一个整数数组的函数。当然你可以有多个函数参数):

#include <iostream>

const int SIZE = 3;

/* This is a simpified version of 'operate'. It
   takes one argument, which is an array of integers. */    
void operate(int a[])
{
  /* The only thing I do is to iterate through
     all elements of the array and print them. */
  int i = 0;
  while (i < SIZE) {
    std::cout << a[i] << std::endl;
    ++i;
  }

  /* IMPORTANT: The length of the array is defined by a
     global constant SIZE. Alternatively, you can pass
     along the size of the array as a separate argument
     to the function. */
}

int main()
{
  /* Main program. Here is our array: */
  int my_array[SIZE] = { 1,2,3 };

  /* And here we call our function: */
  operate(my_array);
  return 0;
}

但是,所有这些都有些复杂,而且不像 C++(与 C 相比)中那样方便。十有八九,你会更好地不使用数组,并将它们替换为std::vector. 最好在cppreference上查看如何使用它的示例(也可以单击一些成员函数,例如构造函数并push_back获取特定的代码示例。)

于 2012-11-16T04:42:41.513 回答