//----------------------------------------------------------------------------- // File: BioE215Lab1Part2DAJ.cpp // Class: None // Parent: None // Children: None // Purpose: Lab 1: Using Part 1 to Open/Close and Write to a File //----------------------------------------------------------------------------- // 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 long int ValidateStringforLongInput ( char * inputString, double sizeOfString, long int defaultValue); FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute); bool WriteDoubleToFileWithEExponentInSpecifiedPrecision( double x, int precision, FILE *fptr); //----------------------------------------------------------------------------- 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 between 180 and 720.\n" ); printf( "Please use the following integer format\n (XXX): "); char angleArray[20]; GetStringFromKeyboard(angleArray, 20); printf( "\n"); int inputAngle = ValidateStringforLongInput(angleArray, 20, 360); if( (inputAngle > 720) || (inputAngle < 180) ) { inputAngle = 360; printf("Sorry your angle was not in the proper range, so the default 360 will be used.\n"); } printf( "Please enter an precision value between 1 and 17.\n" ); printf( "Please use the following integer format\n (XX): "); char precisionArray[20]; GetStringFromKeyboard(precisionArray, 20); printf( "\n"); int precisionValue = ValidateStringforLongInput(precisionArray, 20, 5); if( (precisionValue > 17) || (precisionValue < 1) ) { precisionValue = 5; printf("Sorry your precision was not in the proper range, so the default 5 will be used.\n"); } printf("Angle is %d: Precision is %d: \n", inputAngle, precisionValue); FILE *outputFILE = FileOpenWithMessageIfCannotOpen("HelloMath2.txt", "w"); for(int i = 0; i <= inputAngle; i++) { WriteDoubleToFileWithEExponentInSpecifiedPrecision(i, precisionValue, outputFILE); WriteDoubleToFileWithEExponentInSpecifiedPrecision(ConvertFromDegreesToRadians(i), precisionValue, outputFILE); WriteDoubleToFileWithEExponentInSpecifiedPrecision(sinf((float)ConvertFromDegreesToRadians(i)), precisionValue, outputFILE); WriteDoubleToFileWithEExponentInSpecifiedPrecision(cosf((float)ConvertFromDegreesToRadians(i)), precisionValue, outputFILE); WriteDoubleToFileWithEExponentInSpecifiedPrecision(tanf((float)ConvertFromDegreesToRadians(i)), precisionValue, outputFILE); fprintf(outputFILE, "\n"); } fclose(outputFILE); 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 long int ValidateStringforLongInput ( char * inputString, double sizeOfString, long int defaultValue) { long int returnValue = 0; bool foundReturnCodeInString = false; bool isValidInputString = true; int i = 0; //Scan to Make Sure Each Element is a Digit while (!foundReturnCodeInString) { if( inputString[i] == 10) { //Found Return Code and Stop foundReturnCodeInString = true; //endpointer = &inputString[i]; } else { if( !isdigit(inputString[i]) ) { isValidInputString = false; } i++; if(i >= sizeOfString) foundReturnCodeInString = true; } } if(isValidInputString) { ConvertStringToLong(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; } //----------------------------------------------------------------------------- FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute) { FILE *returnValue = fopen(filename, attribute); if(returnValue == NULL) { printf("Error: The Specified File Could Not be Opened. Check File name and Attribute. \n"); } return returnValue; }