C ++ Skip Functions

Ok, I think I fixed most of this, but I donโ€™t like that I pass the constants that I think. Any help would be greatly appreciated, thanks.

also, with a piece! inputFile, I'm not sure how to withdraw the return (EXIT_FAILURE), as suggested by my teacher.

Also, as for your suggestion using only one part of the array, will it still allow me to use all this in a function?

It is assumed that the program accepts such a file: for example: NOT 11010001 and it should read the command as a string, read the binary file as an array, and then execute the command in binary format.

The code here is just the main function, you just donโ€™t need to send a wall right away, if thatโ€™s good, then Iโ€™ll be happy to add the rest. Also, the void Operate () function pretty much calls all the other functions anyway ... I'm not sure if this caused it or what. The print table contains only a table in which all information is placed.

and the headings are uncomfortable for me here, so just assume they are right.

/* ========================================================================== */ /* 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
source share
1 answer

You need to call functions from main . You cannot do this by sorting them:

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

Instead, you need to call them:

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

Note that there is another problem: Operate requires three integer arguments at the end, but you are trying to use integer arrays as arguments. You will need to select one element from each array and present only this as an argument.


(EDIT) Several things have changed in your question, and I donโ€™t understand enough to indicate what the best overall structure of your data and functions should be, but if you are dealing with whole arrays and want to pass the whole array of a function, you can do it like this way (I simplified it for a function that takes only one array of integers. Of course, you can have more than one function argument):

 #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; } 

However, all this is a bit complicated and not as complicated as you could have it in C ++ (as opposed to C). In all likelihood, it will be much better for you not to use arrays and replace them with std::vector . Better cppreference check for examples on how to use it (also click on some of the member functions like constructor and push_back for specific code examples.)

+4
source

Source: https://habr.com/ru/post/1446372/


All Articles