#define ALLOW_INSTRUCTOR_CODE /*************************************************************************** *************************************************************************** ****** BEFORE BEGINNING OR TURNING IN THIS EXERCISE IT IS EXTREMELY ****** ****** IMPORTANT THAT YOU READ, FULLY UNDERSTAND, AND ADHERE TO THE ****** ****** REQUIREMENTS STATED IN THE DOCUMENT TITLED, "ASSIGNMENT ****** ****** SUBMISSION REQUIREMENTS", DOWNLOADABLE FROM THE COURSE WEB ****** ****** SITE. ****** *************************************************************************** **************************************************************************/ /*************************************************************************** ************* C/C++ Programming 2: Assignment 5 - Exercise 3 ************* ******** BEGIN EXERCISE REQUIREMENT - THIS MUST BE A "C++" PROGRAM ******** *************************************************************************** (5 points) In ANSI/ISO C/C++, floating literals are written in either scientific or standard notation as types float, double, or long double, as determined by their suffix or absence thereof. During the parsing phase of program compilation the source code is broken up into separate tokens, some or all of which may be floating literals. Write functions OpenFile and DetectFloats: OpenFile syntax: void OpenFile(const char *fileName, ifstream &inFile); Parameters: fileName - a pointer to the name of a file to be opened inFile - a reference to the ifstream object to be used to open the file Synopsis: Opens the file named in in the read-only text mode using the object. If the open fails an error message is output to cerr and the program is terminated with an error exit code. Return: void if the open succeeds; otherwise, the function does not return. DetectFloats syntax: StatusCode DetectFloats(const char *cp); Parameters: cp - a pointer to a string to be analyzed Synopsis: Analyzes the string in and determines if it represents a syntactically legal floating literal, and if so, its data type. Return: one of the following enumerated status codes representing the result of the syntactic analysis: NOTFLOATING, TYPE_FLOAT, TYPE_DOUBLE, TYPE_LDOUBLE * DetectFloats must implement a state machine and may use only one variable other than its formal parameter . * DetectFloats must make all decisions based upon only the current character in the string (no look backs or look aheads). * Test the program on each string in file "TestFile5.txt", downloaded from the course web site. Note that only every other string in this file is a legal floating literal. Be careful in assuming that your program works based strictly upon this, however. Since these strings do not represent all possible combinations, your program could parse them correctly while still containing a bug. * In addition to the usual source code and screen dump turned in for most other exercises, also turn in the corresponding state diagram. It must be neatly hand or machine drawn and must correspond exactly to the source code. HINTS: * 9 states are required. * Before starting you must have a good understanding of the various floating literal forms. The ANSI/ISO standard uses the following recursive adaptation of BNF (Backus Naur Form) to describe them. Note that floating literals may never start with a sign: floating literal: fractional-constant exponent-part(opt) floating-suffix(opt) decimal-digit-sequence exponent-part floating-suffix(opt) fractional-constant: decimal-digit-sequence(opt).decimal-digit-sequence decimal-digit-sequence. exponent-part: e sign(opt) decimal-digit-sequence E sign(opt) decimal-digit-sequence sign: one of + - decimal-digit-sequence: decimal-digit decimal-digit-sequence decimal-digit floating-suffix: one of f l F L decimal-digit: one of 0 1 2 3 4 5 6 7 8 9 Hint: * Place input test file "TestFile5.txt" in the appropriate default location described in the "FILE LOCATION QUESTIONS AND ANSWERS" section of the "ASSIGNMENT SUBMISSION REQUIREMENTS" document. *************************************************************************** ************************ END EXERCISE REQUIREMENT ************************* **************************************************************************/ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING ABOVE IT. **************************************************************************/ /* * TODO: * REPLACE THIS COMMENT WITH YOUR TITLE BLOCK. */ // The following enum defines the status codes returned by the DetectFloats // function. Do not change it without instructor permission. enum StatusCode { NOTFLOATING, TYPE_FLOAT, TYPE_DOUBLE, TYPE_LDOUBLE }; /* * TODO: * REPLACE THIS COMMENT WITH YOUR C++ CODE, PLUS ANYTHING NECESSARY TO * SUPPORT MY TEST CODE BELOW. DO NOT INCLUDE ANY UNNEEDED HEADER FILES! */ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING BELOW IT. **************************************************************************/ /* ***** Change nothing below this comment without instructor permission. **** * Everything that follows was written to help test/verify your code. You * do not need to understand the details of my code to write yours! */ #ifdef ALLOW_INSTRUCTOR_CODE const char * const FILENAME = "TestFile5.txt"; int main() { const int BUFSIZE = 128; // input string buffer size const int SAFEBUF = BUFSIZE - 1; // limit input string char buf[BUFSIZE]; // input buffer ifstream inFile; OpenFile(FILENAME, inFile); // Get one string at a time and pass it to the parser function. while (inFile >> setw(SAFEBUF) >> buf) { cout << '"' << buf << "\" is "; switch (DetectFloats(buf)) { case NOTFLOATING: cout << "not a floating literal.\n"; break; case TYPE_FLOAT: cout << "type float.\n"; break; case TYPE_DOUBLE: cout << "type double.\n"; break; case TYPE_LDOUBLE: cout << "type long double.\n"; break; default: cout << "*****internal error*****\n"; break; } } inFile.close(); return EXIT_SUCCESS; } #endif