//----------------------------------------------------------------------------- // File: HelloMath2.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 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) #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 angleInDegrees); double ConvertFromDegreesToRadians( long angleInRadians); FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute); //----------------------------------------------------------------------------- int main( int numberOfCommandLineArguments, char *arrayOfCommandLineArguments[] ) { char inputString[256]; const char fileAttributeR[5]="rb"; const char fileAttributeW[5]="w"; WriteStringToScreen("Please enter an integer between 180 and 720:\n"); GetStringFromKeyboard(inputString,256); long angleInDegrees; if(ConvertStringToLong(inputString,angleInDegrees,360)==NULL) { WriteStringToScreen("A default value of 360 (for an angle in degrees) will be used\n"); } if(angleInDegrees <180 || angleInDegrees >720 ) { angleInDegrees=360; WriteStringToScreen("A default value of 360 (for an angle in degrees) will be used\n"); } char precision[256]; WriteStringToScreen("Please enter the desired precision as an integer\n"); GetStringFromKeyboard(precision,256); long returnValueForPrecision; if(ConvertStringToLong(precision,returnValueForPrecision,5)==NULL) { WriteStringToScreen("A default value of 5 for precision will be used\n"); } if(returnValueForPrecision <1 || returnValueForPrecision >17 ) { returnValueForPrecision=5; WriteStringToScreen("A default value of 5 for precision will be used\n"); } FILE *pFile = FileOpenWithMessageIfCannotOpen("HelloMath2.txt",fileAttributeW); if (!pFile) return 1; for (int i=1;i<=angleInDegrees;i++) { WriteDoubleToFileWithExponentInSpecifiedPrecision(i,returnValueForPrecision,pFile); double angleInRadians=ConvertFromDegreesToRadians(i); WriteDoubleToFileWithExponentInSpecifiedPrecision(angleInRadians,returnValueForPrecision,pFile); WriteDoubleToFileWithExponentInSpecifiedPrecision(sin(angleInRadians),returnValueForPrecision,pFile); WriteDoubleToFileWithExponentInSpecifiedPrecision( cos(angleInRadians),returnValueForPrecision,pFile); WriteDoubleToFileWithExponentInSpecifiedPrecision(tan(angleInRadians),returnValueForPrecision,pFile); WriteStringToFile("\n",pFile); } fclose (pFile); WriteStringToScreen("\n\n Press Enter to terminate the program:"); int key = getchar(); return 0; } //__________________________________________________________________________________ bool GetStringFromKeyboard( char inputString[], unsigned long sizeOfString) { GetStringFromFile(inputString,256,stdin); return 0; } //_________________________________________________________________________________ bool GetStringFromFile(char inputString[], unsigned long sizeOfString, FILE* fptr) { fgets(inputString,sizeOfString,fptr); return 0; } //_____________________________________________________________________________________ const char* ConvertStringToLong( const char *s, long &returnValue, long defaultValue) { returnValue = defaultValue; if (s) { char *pointerToCharacterAfterNumber = NULL; long x =strtol(s,&pointerToCharacterAfterNumber,0); char characterAfterNumber = pointerToCharacterAfterNumber ? *pointerToCharacterAfterNumber : 'z'; if ( characterAfterNumber == '\0' || isspace (characterAfterNumber) ) { returnValue = x; return pointerToCharacterAfterNumber; } } return NULL; } //___________________________________________________________________________________________ bool WriteStringToScreen ( const char outputString[]) { WriteStringToFile(outputString, stdout); return 0; } //___________________________________________________________________________________ bool WriteStringToFile( const char outputString[], FILE *fptr) { fprintf(fptr,outputString); return 0; } //___________________________________________________________________________________________ double ConvertFromDegreesToRadians( long angleInDegrees) { double angleInRadians=angleInDegrees*(PI/180); return angleInRadians; } //____________________________________________________________________________________________________________ bool WriteDoubleToFileWithExponentInSpecifiedPrecision(double x, int precision, FILE *fptr) { if (precision < 0 || precision >17) 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; } //__________________________________________________________________________________________________________