//----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // File: HelloMath2.cpp // Class: None // Parent: None // Children: None // Purpose: Takes an angle and precision from the user. Writes to a file the // angle in degrees, sine, cosine, and tangent of that angle, going from // 0 to the angle given by the user, to the precision specified by the user. //----------------------------------------------------------------------------- // 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[], const unsigned long maxSizeOfString, FILE *fptr ){ return fgets(inputString, maxSizeOfString, fptr) == NULL;} bool GetStringFromKeyboard( char inputString[], const unsigned long maxSizeOfString ){ return GetStringFromFile( inputString, maxSizeOfString, stdin );} 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 ); double ConvertFromRadiansToDegrees( double angleInRadians ){return angleInRadians*57.2957795; /*1 radian is approx 57.3 degrees*/} double ConvertFromDegreesToRadians( double angleInDegrees ){return angleInDegrees/57.2957795;/*1 radian is approximately 57.3 degrees*/} 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 ); //----------------------------------------------------------------------------- int main() { //get the angle from the user and convert to a long WriteStringToScreen("Enter an integer between 180 and 720 representing an angle in degrees:"); char stringInDegrees[15]; GetStringFromKeyboard( stringInDegrees, 15 ); long angleInDegrees; if( ConvertStringToLong(stringInDegrees, angleInDegrees, 360) == false ) WriteStringToScreen("\nYour angle was not valid. 360 degrees will be used instead."); else if(angleInDegrees<180 || angleInDegrees>720) { WriteStringToScreen("\nYour angle was not valid. 360 degrees will be used instead."); angleInDegrees=360; } //get the precision from the user and convert to a long WriteStringToScreen("\nEnter the precision for writing numbers as an integer from 1 to 17:"); char precisionString[15]; GetStringFromKeyboard(precisionString, 15); long precisionNumber; if( ConvertStringToLong( precisionString, precisionNumber, 5 ) == false ) WriteStringToScreen("\nYour precision was not valid. 5 will be used instead."); else if( precisionNumber<1 || precisionNumber>17 ) { WriteStringToScreen("\nYour precision was not valid. 5 will be used instead."); precisionNumber=5; } double angleInRadians; //print to a file the values of the angle in degrees and the sine, cosine, and tangent of the angle. if( FILE *fileToWrite = FileOpenWithMessageIfCannotOpen("HelloMath2.txt","w") ) { for( int i=0; i <= angleInDegrees; i++ ) { angleInRadians=ConvertFromDegreesToRadians(i); WriteDoubleToFileWithEExponentInSpecifiedPrecision( i, precisionNumber, fileToWrite ); WriteDoubleToFileWithEExponentInSpecifiedPrecision( sin(angleInRadians), precisionNumber, fileToWrite); WriteDoubleToFileWithEExponentInSpecifiedPrecision( cos(angleInRadians), precisionNumber, fileToWrite); WriteDoubleToFileWithEExponentInSpecifiedPrecision( tan(angleInRadians), precisionNumber, fileToWrite); fprintf(fileToWrite,"\n"); } fclose(fileToWrite); } // 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; }