//----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // File: HelloMath3.cpp // Class: None // Parent: None // Children: None // Purpose: Reads a matrix in from a file. Calculates 2*matrix and writes to a file. //----------------------------------------------------------------------------- // The following are standard C/C++ header files. // If a filename is enclosed inside < > it means the header file is in the Include directory. // If a filename is enclosed inside " " it means the header file is in the current directory. #include // Character Types #include // Mathematical Constants #include // Variable Argument Lists #include // Standard Input/Output Functions #include // Utility Functions #include // String Operations #include // Signals (Contol-C + Unix System Calls) #include // Nonlocal Goto (For Control-C) #include // Time and Date information #include // Verify Program Assertion #include // Error Codes (Used in Unix system()) #include // Floating Point Constants #include // Implementation Constants #include // Standard Definitions #include // Exception handling (e.g., try, catch throw) //----------------------------------------------------------------------------- bool GetStringFromFile( char inputString[], unsigned long maxSizeOfString, FILE *fptr ) { return fgets( inputString, maxSizeOfString, fptr ) != NULL;} bool WriteStringToFile( const char outputString[], FILE *fptr ) { return fprintf( fptr, outputString) != NULL; } bool WriteStringToScreen( const char outputString[] ){return WriteStringToFile(outputString, stdout);} const char* ConvertStringToDouble( const char *s, double &returnValue, double defaultValue ); FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute ); bool WriteDoubleToFileWithEExponentInSpecifiedPrecision( double x, int precision, FILE *fptr ); const char* ConvertStringToLong( const char *s, long &returnValue, long defaultValue ); bool GetDoubleRowElementsFromFile( double *array, unsigned int numberOfCols, FILE *fptr ); bool GetDoubleMatrixElementsFromFile( double *array, unsigned int numberOfRows, unsigned int numberOfCols, FILE *fptr); bool WriteDoubleRowElementsToFile( const double *array, unsigned int numberOfCols, int precision, FILE *fptr ); bool WriteDoubleMatrixElementsToFile(const double *array, unsigned int numberOfRows, unsigned int numberOfCols, int precision, FILE *fptr); void AddMatrices( const double *arrayA, const double *arrayB, double *arraySum, unsigned int numberOfElements); //----------------------------------------------------------------------------- int main() { int rows = 5; int cols = 7; if( FILE *fileToRead = FileOpenWithMessageIfCannotOpen("HelloMath3In.txt","r")) { double *matrix = new double[rows*cols]; //get the matrix elements from the file if( GetDoubleMatrixElementsFromFile(matrix,rows,cols,fileToRead) == false ) WriteStringToScreen("\n There was an error reading in the matrix."); else { double *finalMatrix = new double[rows*cols]; //add the matrix to itself and print to a new file. AddMatrices( matrix, matrix, finalMatrix, rows*cols ); if( FILE *fileToWrite = FileOpenWithMessageIfCannotOpen("HelloMath3Out.txt","w") ) if( WriteDoubleMatrixElementsToFile(finalMatrix,rows,cols,3,fileToWrite) == false ) WriteStringToScreen("\n There was an error writing to the the file."); } _fcloseall(); } // Keep the screen displayed until the user presses the Enter key WriteStringToScreen( "\n\n Press Enter to terminate the program: " ); int key = getchar(); return 0; } //----------------------------------------------------------------------------- const char* ConvertStringToDouble( const char *s, double &returnValue, double defaultValue ) { // Default return value (in case the string is not a valid number) returnValue = defaultValue; // Check if s is a NULL string or "&^%$#" or "abc" or "123huh" if( s ) { // Use the standard math function strtod to parse the number char *pointerToCharacterAfterNumber = NULL; double x = strtod( s, &pointerToCharacterAfterNumber ); // Ensure the character after the number is a space or ’\0’, not ’a’ or ’z’ or ... char characterAfterNumber = pointerToCharacterAfterNumber ? *pointerToCharacterAfterNumber : 'z'; if( characterAfterNumber == '\0' || isspace(characterAfterNumber) ) { returnValue = x; return pointerToCharacterAfterNumber; } } return NULL; } //----------------------------------------------------------------------------- FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute ) { FILE *openedFile=fopen(filename,attribute); if( !openedFile ) printf("Error opening file."); return openedFile; } //----------------------------------------------------------------------------- bool WriteDoubleToFileWithEExponentInSpecifiedPrecision( double x, int precision, FILE *fptr ) { int fieldWidth=precision+8; // Create the format specifier and print the number char format[20]; sprintf( format, " %%- %d.%dE", fieldWidth, precision ); return fprintf( fptr, format, x ) > 0; } //----------------------------------------------------------------------------- const char* ConvertStringToLong( const char *s, long &returnValue, long defaultValue ) { // Default return value (in case the string is not a valid number) returnValue = defaultValue; // Check if s is a NULL string or "&^%$#" or "abc" or "123huh" if( s ) { // Use the standard math function strtod to parse the number char *pointerToCharacterAfterNumber = NULL; long x = strtol( s, &pointerToCharacterAfterNumber, 10 ); // Ensure the character after the number is a space or ’\0’, not ’a’ or ’z’ or ... char characterAfterNumber = pointerToCharacterAfterNumber ? *pointerToCharacterAfterNumber : 'z'; if( characterAfterNumber == '\0' || isspace(characterAfterNumber) ) { returnValue = x; return pointerToCharacterAfterNumber; } } return NULL; } //----------------------------------------------------------------------------- bool GetDoubleRowElementsFromFile( double *array, unsigned int numberOfCols, FILE *fptr ) { char inputString[1000]; if( GetStringFromFile(inputString,1000,fptr) ) { const char *ptrToCharAfterDouble = inputString; for( unsigned int i=0; i