//----------------------------------------------------------------------------- // File: HelloMath3.cpp // Class: None // Parent: None // Children: None // Purpose: Tests out various mathematical functions //----------------------------------------------------------------------------- // The following are standard C/C++ header files. // 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) #include #include #include #include #include using namespace std; //----------------------------------------------------------------------------- #define PI 3.1415926535 //----------------------------------------------------------------------------- // Prototypes for local functions (functions not called by code in other files) //----------------------------------------------------------------------------- bool WriteDoubleToFileWithExponentInSpecifiedPrecision( double x, int precision, FILE *fptr ); bool GetStringFromFile( char inputString[], unsigned long sizeOfString, FILE* fptr); bool GetStringFromKeyboard( char inputString[], unsigned long sizeOfString); bool WriteStringToScreen ( const char outputString[]); bool WriteStringToFile( const char outputString[], FILE *fptr); const char* ConvertStringToLong( const char *s, long &returnValue, long defaultValue); double ConvertFromRadiansToDegrees( long angleInRadians); double ConvertFromDegreesToRadians( long angleInDegrees); FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute);FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute); bool GetDoubleRowElementsFromFile(double *array, unsigned int numberOfCols, FILE *fptr); bool GetDoubleMatrixElementsFromFile(double *array, unsigned int numberOfRows, unsigned int numberOfCols, FILE *fptr); bool WriteDoubleRowElemementsToFile(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 AddMatrics(const double *arrayA, const double *arrayB, double *arraySum, unsigned int numberOfElements); //----------------------------------------------------------------------------- int main( int numberOfCommandLineArguments, char *arrayOfCommandLineArguments[] ) { const char fileAttributeR[5]="rb"; const char fileAttributeW[5]="w"; FILE *pFile = FileOpenWithMessageIfCannotOpen("HelloMath3In.txt",fileAttributeR); if (!pFile) return 1; double mArray[35]; double *Parray=mArray; int numberOfRows=5, numberOfCols=7; GetDoubleMatrixElementsFromFile(Parray,numberOfRows,numberOfCols,pFile); double arraySum[35]; double *ParraySum=arraySum; AddMatrics(mArray, mArray, Parray, 35); FILE *pFileout = FileOpenWithMessageIfCannotOpen("HelloMath3Out.txt",fileAttributeW); WriteDoubleMatrixELementsToFile(Parray, 5, 7, 8, pFileout); system("PAUSE"); return 0; } //------------------------------------------------------------------------------------------------ bool WriteDoubleMatrixELementsToFile(const double *array, unsigned int numberOfRows, unsigned int numberOfCols, int precision, FILE *fptr) { for (unsigned int i=0;i17) precision =5; int fieldWidth =precision +8; char format[20]; sprintf(format," %%- %d.%dE", fieldWidth, precision); return fprintf( fptr, format, x) >0; } //--------------------------------------------------------------------- FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute) { FILE *p2File; p2File=fopen(filename,attribute);//"r" if (p2File==NULL) { WriteStringToScreen("Sorry, could not open file that was requested, please check name and re-enter\n"); return NULL; } return p2File; } //__________________________________________________________________________________________________________