//----------------------------------------------------------------------------- // File: HelloMath1.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 ); const char* ConvertStringToDouble( const char *s, double &returnValue, double defaultValue ); double ConvertFromRadiansToDegrees( double angleInRadians ) { return angleInRadians * 57.295779513082320876798154814105170332405472466564321549160243861; } double ConvertFromDegreesToRadians( double angleInDegrees ) { return angleInDegrees * 0.017453292519943295769236907684886127134428718885417254560971914402; } //----------------------------------------------------------------------------- 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 printf( " Enter an angle in degrees:\n"); char stringFromKeyboard[10]; GetStringFromKeyboard( stringFromKeyboard, 10); double angleInDegrees; ConvertStringToDouble( stringFromKeyboard, angleInDegrees, 45); // Convert the angle to radians double angleInRadians = ConvertFromDegreesToRadians(angleInDegrees); // Calculate sine, cosine, and tangent of the angle double angleSin = sin(angleInRadians); double angleCos = cos(angleInRadians); double angleTan = tan(angleInRadians); // Write angle in deg, angle in rad, sine, cosine, and tangent to screen printf( " \n\n Angle in degrees: %g\n", angleInDegrees); printf( " \n Angle in radians: %g\n", angleInRadians); printf( " \n Sine of angle: %g\n", angleSin); printf( " \n Cosine of angle: %g\n", angleCos); printf( " \n Tangent of angle: %g\n", angleTan); // Exit program printf( "\n\n Press Enter to terminate the program: " ); 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) ) { returnValue = x; return pointerToCharacterAfterNumber; } } return NULL; }