//----------------------------------------------------------------------------- // File: HelloMath1.cpp // Class: None // Parent: None // Children: None // Purpose: Tests out various mathematical functions and prints to screen. // Author: Melanie Fox // Date: 4/13/07 //----------------------------------------------------------------------------- // 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) //----------------------------------------------------------------------------- #define PI 3.14159265358979 #define INPUT_ANGLE_ARRAY_SIZE 100 #define DEFAULT_ANGLE 45.0 #define OUTPUT_PRECISION 5 //----------------------------------------------------------------------------- // 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 ); double ConvertFromRadiansToDegrees( double angleInRadians ); double ConvertFromDegreesToRadians( double angleInDegrees ); //----------------------------------------------------------------------------- int main( void ) { // Prompt user to enter the value of an angle in degrees & store the string. char angleInDegreesString[INPUT_ANGLE_ARRAY_SIZE]; WriteStringToScreen( "Enter an angle in degrees: " ); GetStringFromKeyboard(angleInDegreesString, INPUT_ANGLE_ARRAY_SIZE); WriteStringToScreen( "\nYou entered: " ); WriteStringToScreen( angleInDegreesString ); //Convert input angle from string to double and check whether value is a real number. //If value is not a real number, set angle to default value. double angleInDegreesDouble = DEFAULT_ANGLE; if( ConvertStringToDouble( angleInDegreesString, angleInDegreesDouble, DEFAULT_ANGLE ) == NULL ) WriteStringToScreen( "Entered quantity is not a real number.\nAngle of 45 degrees will be used instead.\n" ); WriteStringToScreen( "Angle in degrees is: " ); WriteDoubleToFileWithEExponentInSpecifiedPrecision( angleInDegreesDouble, OUTPUT_PRECISION, stdout ); //Convert angle from degrees to radians and print to screen. double angleInRadiansDouble; angleInRadiansDouble = ConvertFromDegreesToRadians( angleInDegreesDouble ); WriteStringToScreen( "\nAngle in radians is: " ); WriteDoubleToFileWithEExponentInSpecifiedPrecision( angleInRadiansDouble, OUTPUT_PRECISION, stdout ); //Calculate sin, cos, & tan and print to screen. WriteStringToScreen( "\nSin of the angle is: " ); WriteDoubleToFileWithEExponentInSpecifiedPrecision( sin( angleInRadiansDouble ), OUTPUT_PRECISION, stdout ); WriteStringToScreen( "\nCos of the angle is: " ); WriteDoubleToFileWithEExponentInSpecifiedPrecision( cos( angleInRadiansDouble ), OUTPUT_PRECISION, stdout ); WriteStringToScreen( "\nTan of the angle is: " ); WriteDoubleToFileWithEExponentInSpecifiedPrecision( tan( angleInRadiansDouble ), OUTPUT_PRECISION, stdout ); //Pause before exiting program until key is pressed. getchar(); // A normal program exit returns 0 (other return values usually signal an error) return 0; } //----------------------------------------------------------------------------- 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 = 15; 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; } //----------------------------------------------------------------------------- bool GetStringFromFile( char inputString[], unsigned long sizeOfString, FILE *fptr ) { return( fgets( inputString, sizeOfString, fptr ) ); } //----------------------------------------------------------------------------- bool GetStringFromKeyboard( char inputString[], unsigned long sizeOfString ) { return( GetStringFromFile( inputString, sizeOfString, stdin ) ); } //----------------------------------------------------------------------------- bool WriteStringToFile( const char outputString[], FILE *fptr ) { return( fprintf( fptr, "%s\n", outputString ) ); } //----------------------------------------------------------------------------- bool WriteStringToScreen( const char outputString[] ) { return( WriteStringToFile( outputString, stdout ) ); } //----------------------------------------------------------------------------- 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; } //----------------------------------------------------------------------------- double ConvertFromRadiansToDegrees( double angleInRadians ) { return( angleInRadians*180.0/PI ); } //----------------------------------------------------------------------------- double ConvertFromDegreesToRadians( double angleInDegrees ) { return( angleInDegrees*PI/180.0 ); }