//----------------------------------------------------------------------------- // File: HelloMath2.cpp // Class: None // Parent: None // Children: None // Purpose: Tests out various mathematical functions and my coding abilities //----------------------------------------------------------------------------- // 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) //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Prototypes for local functions (functions not called by code in other files) //----------------------------------------------------------------------------- bool GetStringFromKeyboard( char inputString[], unsigned long sizeOfString ); bool GetStringFromFile( char inputString[], unsigned long sizeOfString, FILE *fptr ); bool WriteStringToScreen( const char outputString[] ); bool WriteStringToFile( const char outputString[], FILE *fptr ); bool WriteDoubleToFileWithEExponentInSpecifiedPrecision( double x, int precision, FILE *fptr); const char* ConvertStringToDouble( const char *s, double &returnValue, double defaultValue ); const char* ConvertStringToLong( const char *s, long &returnValue, long defaultValue ); double ConvertFromRadiansToDegrees( double angleInRadians ); double ConvertFromDegreesToRadians( double angleInDegrees ); FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute); //----------------------------------------------------------------------------- int main( void ) { // Write " Hello math!" to the screen and then put a newline printf( " Hello math!\n" ); // Prompt the user to enter an angle in degrees between 180 an 720 degrees printf( " Enter an integer from 180 to 720 that represents an angle in degrees\n"); printf( " If the integer is < 180 or >720, program will default to 360:\n"); // Verify angle is an integer in the correct range, or default to 360 char angleStringFromKeyboard[10]; GetStringFromKeyboard( angleStringFromKeyboard, 10); double angleInDegrees; ConvertStringToDouble( angleStringFromKeyboard, angleInDegrees, 360); // Prompt the user to enter the precision for writing numbers printf( " Enter the precision for writing numbers, from 1 to 17\n"); printf( " If precision <1 or >17, program will default to 5:\n"); char precisionStringFromKeyboard[10]; GetStringFromKeyboard( precisionStringFromKeyboard, 10); long userPrecision; ConvertStringToLong( precisionStringFromKeyboard, userPrecision, 5); // Create file to write results FILE* filePoint; filePoint = FileOpenWithMessageIfCannotOpen( "HelloMath2Results.txt", "w+" ); // For loop to convert the angle to radians and calculate sin, cos, and tan // over range 0:angleInDegrees for( int i = 0; i <= (int)angleInDegrees; i++) { double angleInRadians = ConvertFromDegreesToRadians(i); double sinAngle = sin(angleInRadians); double cosAngle = cos(angleInRadians); double tanAngle = tan(angleInRadians); double angleInDegreesToPrint = i; WriteDoubleToFileWithEExponentInSpecifiedPrecision( angleInDegreesToPrint, userPrecision, filePoint); WriteDoubleToFileWithEExponentInSpecifiedPrecision( angleInRadians, userPrecision, filePoint); WriteDoubleToFileWithEExponentInSpecifiedPrecision( sinAngle, userPrecision, filePoint); WriteDoubleToFileWithEExponentInSpecifiedPrecision( cosAngle, userPrecision, filePoint); WriteDoubleToFileWithEExponentInSpecifiedPrecision( tanAngle, userPrecision, filePoint); fprintf( filePoint, "\n"); } fclose(filePoint); // Keep the screen displayed until the user presses the Enter key printf( "\n\n Press Enter to terminate the program: " ); int key = getchar(); return 0; } //----------------------------------------------------------------------------- bool GetStringFromKeyboard( char inputString[], unsigned long sizeOfString ) { return GetStringFromFile( inputString, sizeOfString, stdin ) ; } //----------------------------------------------------------------------------- bool GetStringFromFile( char inputString[], unsigned long sizeOfString, FILE *fptr ) { return fgets( inputString, sizeOfString, fptr ) != NULL; } //----------------------------------------------------------------------------- bool WriteStringToScreen( const char outputString[] ) { return( WriteStringToFile( outputString, stdout ) ); } //----------------------------------------------------------------------------- bool WriteStringToFile( const char outputString[], FILE *fptr ) { return fputs( outputString, fptr ) != EOF; } //----------------------------------------------------------------------------- const char* ConvertStringToDouble( const char *s, double &returnValue, double defaultValue ) { // Default return value, use if string is not a valid number returnValue = defaultValue; // Check if s is a NULL string or not a number "sldkfn" 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' char characterAfterNumber = pointerToCharacterAfterNumber ? *pointerToCharacterAfterNumber : 'z'; if( characterAfterNumber == '\0' || isspace(characterAfterNumber) && x >= 180 && x <= 720 ) { returnValue = x; return pointerToCharacterAfterNumber; } else { printf( "Invalid angle value, default to %g\n", defaultValue); } } return NULL; } //----------------------------------------------------------------------------- const char* ConvertStringToLong( const char *s, long &returnValue, long defaultValue ) { // Default return value, use if string is not a valid number returnValue = defaultValue; // Check if s is a NULL string or not a number "sldkfn" 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' char characterAfterNumber = pointerToCharacterAfterNumber ? *pointerToCharacterAfterNumber : 'z'; if( characterAfterNumber == '\0' || isspace(characterAfterNumber) && x >= 1 && x <= 17 ) { returnValue = x; return pointerToCharacterAfterNumber; } else { printf( "Invalid precision value, default to %d\n", defaultValue); } } return NULL; } //----------------------------------------------------------------------------- double ConvertFromRadiansToDegrees( double angleInRadians ) { double pi = 3.1415926; return(angleInRadians*180/pi); } //----------------------------------------------------------------------------- double ConvertFromDegreesToRadians( double angleInDegrees ) { double pi = 3.1415926; return(angleInDegrees*pi/180); } //----------------------------------------------------------------------------- bool WriteDoubleToFileWithEExponentInSpecifiedPrecision( double x, int precision, FILE *fptr) { // Ensure the precision (number of digits in the mantissa after the decimal point) makes sense. // Next, calculate the field width so it includes one extra space to the right of the number. if( precision < 0 || precision > 17 ) precision = 5; 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; } //----------------------------------------------------------------------------- FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute) { FILE *stream; if( (stream = fopen( filename, attribute )) == NULL) { printf("cannot open file\n"); } else { printf("results file opened\n"); } return stream; } //-----------------------------------------------------------------------------