//----------------------------------------------------------------------------- // File: BioE215Lab1Part1DAJ.cpp // Class: None // Parent: None // Children: None // Purpose: Lab 1: Refreshes C++ language for Sim TK //----------------------------------------------------------------------------- // 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) #include "BioE215StringModule.h" // Additional String Functions created for BioE215 //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Prototypes for local functions (functions not called by code in other files) //----------------------------------------------------------------------------- static double ConvertFromRadiansToDegrees ( double angleInRadians ); static double ConvertFromDegreesToRadians ( double angleInDegrees ); static double ValidateStringforAngleInput ( char * inputString, double sizeOfString, double defaultValue); //----------------------------------------------------------------------------- int main( int numberOfCommandLineArguments, char *arrayOfCommandLineArguments[] ) { // Write " Hello math!" to the screen and then put a newline printf( "Hello Math Exercises 1!\n" ); // Prompt user for input printf( "Please enter an angle in degrees using the following format\n (XXX.XXXX): "); char inputArray[20]; GetStringFromKeyboard(inputArray, 20); printf( "\n"); double inputAngle = ValidateStringforAngleInput(inputArray, 20, 45); fprintf(stdout, " Here are the calculations:\n"); fprintf(stdout, " Degrees : %f \n", inputAngle); fprintf(stdout, " Radians : %f \n", ConvertFromDegreesToRadians(inputAngle) ); fprintf(stdout, " Sine(Deg): %f \n", sinf((float) ConvertFromDegreesToRadians(inputAngle)) ); fprintf(stdout, " Cos (Deg): %f \n", cosf((float) ConvertFromDegreesToRadians(inputAngle)) ); fprintf(stdout, " Tan (Deg): %f \n", tanf((float) ConvertFromDegreesToRadians(inputAngle)) ); printf( "Press any key to exit the program."); int key = getchar(); // A normal program exit returns 0 (other return values usually signal an error) return 0; } //----------------------------------------------------------------------------- static double ValidateStringforAngleInput ( char * inputString, double sizeOfString, double defaultValue) { double returnValue = 0; bool foundReturnCodeInString = false; bool isValidInputString = true; int i = 0; //Scan to Make Sure Each Element is either a Digit or a '.' while (!foundReturnCodeInString) { if( inputString[i] == 10) { //Found Return Code and Stop foundReturnCodeInString = true; //endpointer = &inputString[i]; } else { if( !isdigit(inputString[i]) && (inputString[i] != 46) ) { isValidInputString = false; } i++; if(i >= sizeOfString) foundReturnCodeInString = true; } } if(isValidInputString) { ConvertStringToDouble(inputString, returnValue, defaultValue); } else { returnValue = defaultValue; fprintf(stdout, "Sorry, your input was not valid so the default value of %g was used instead.\n", returnValue); } return returnValue; } //----------------------------------------------------------------------------- static double ConvertFromRadiansToDegrees ( double angleInRadians ) { return angleInRadians * 180/3.1415926535897932384626433832795028841971; } //----------------------------------------------------------------------------- static double ConvertFromDegreesToRadians ( double angleInDegrees ) { return angleInDegrees * 3.1415926535897932384626433832795028841971/180; }