//----------------------------------------------------------------------------- // File: HelloMath1.cpp // Class: None // Parent: None // Children: None // Purpose: Converts an angle to degrees and calculates the sine, cosine, and tangent // of that angle. //----------------------------------------------------------------------------- // 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[], unsigned long maxSizeOfString, FILE *fptr ) { return fgets(inputString, maxSizeOfString, stdin) != NULL; } bool GetStringFromKeyboard( char inputString[], 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*/} //----------------------------------------------------------------------------- int main() { //get the angle and convert to a double. WriteStringToScreen("Enter an angle in degrees:"); char stringInDegrees[100]; unsigned long maxSizeOfString = 100; GetStringFromKeyboard( stringInDegrees, maxSizeOfString ); double angleInDegrees; if(ConvertStringToDouble(stringInDegrees, angleInDegrees, 45)==NULL) WriteStringToScreen("\nYour angle was not valid. 45 degrees will be used instead."); //Do math calculations. double angleInRadians=ConvertFromDegreesToRadians(angleInDegrees); double sinOfAngle = sin(angleInRadians); double cosOfAngle = cos(angleInRadians); double tanOfAngle = tan(angleInRadians); //Print each quantity with a string saying what it is. Then print to screen. //printing degrees char stringDegrees[100]; sprintf( stringDegrees, "\nAngle in degrees= %f\n", angleInDegrees ); WriteStringToScreen( stringDegrees ); //print radians char stringRadians[100]; sprintf( stringRadians, "\nAngle in radians= %f\n", angleInRadians ); WriteStringToScreen( stringRadians ); //print sine char stringSine[100]; sprintf( stringSine, "\nSine of angle= %f\n", sinOfAngle ); WriteStringToScreen( stringSine ); //print cosine char stringCosine[100]; sprintf( stringCosine, "\nCosine of angle= %f\n", cosOfAngle ); WriteStringToScreen( stringCosine ); //print tangent char stringTangent[100]; sprintf( stringTangent, "\nTangent of angle= %f\n", tanOfAngle ); WriteStringToScreen( stringTangent ); // 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; } //----------------------------------------------------------------------------- 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; }