Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

INSTRUCTOR DRIVER FILE: #define INSTRUCTOR_FILE // DO NOT DEFINE THIS MACRO IN ANY FILES YOU CREATE /*** YOU DO NOT NEED TO UNDERSTAND THE CODE

image text in transcribed

image text in transcribed

INSTRUCTOR DRIVER FILE:

#define INSTRUCTOR_FILE // DO NOT DEFINE THIS MACRO IN ANY FILES YOU CREATE

/*** YOU DO NOT NEED TO UNDERSTAND THE CODE IN THIS FILE TO WRITE YOURS ***/

/******************** DO NOT MODIFY THIS FILE IN ANY WAY ********************/ // Everything in this file was written to help test/verify your code and must // not be altered in any way. Do not rename this file or copy anything from // it into your file(s). This file does not necessarily represent good coding // technique, proper formatting/style, or meet the requirements your code must // meet. You do not need to understand the code in this file to write yours. //**************************************************************************** #ifdef INSTRUCTOR_FILE

#include #include #include using namespace std;

const char * const FILENAME = "TestFile2.txt";

void OpenFile(const char *fileName, ifstream &inFile); int Reverse(ifstream &inFile, const int level);

int main() { ifstream inFile; int thisSeparator;

OpenFile(FILENAME, inFile); while ((thisSeparator = Reverse(inFile, 1)) != EOF) cout.put(char(thisSeparator)); inFile.close(); return EXIT_SUCCESS; } #endif

C2A2E4 (6 points - C++ Program) Exclude any existing source code files that may already be in your IDE project and add two new ones. naming them C2A2E4 OpenFile.cpp and C2A2E4 Reverse.cpp. Also add instructor-supplied source code file C2A2E4_main-Driver.cpp. Do not write a main function main already exists in the instructor supplied file and it will use the code you write. File C2A2E4_OpenFile.cpp must contain a function named OpenFile. 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 fileName in the read-only text mode using the inFile object. If the open fails an error message is output to cerr and the program is terminated with an error exit code. The error message must mention the name of the failing file. Return: void if the open succeeds; otherwise, the function does not retum. File C2A2E4 Reverse.cpp must contain a function named Reverse. Reverse syntax: int Reverse(ifstream &infile, const int level); Parameters: inFile - a reference to an ifstream object representing a text file open in a readable text mode. level - recursive level of this function call: 1 => Ist coll 2 => 2nd coll. etc. Synopsis: Recursively reads one character at a time from the text file in infile until a separator is read. Then any non-separator characters that were read are displayed in reverse order with the last and next to next to last characters capitalized. Finally, the separator is returned to the calling function. Reverse neither reverses nor prints separators but merely returns them. The code in the instructor-supplied driver file is responsible for printing each returned separator. Definition of separator: any whitespace (as defined by the standard library isspace function), a period, a question mark, an exclamation point a comma, a colon, a semicolon, or the end of the file. Return: the current separator The Reverse function must: 1. Implement a recursive solution and be able to display words of any length. 2. Be tested with instructor-supplied data file TestFile2.txt, which must be placed in the program's "working directory": 3. not declare more than two non-const variables other than its two parameters. 4. not use arrays, static objects, external objects, dynamic memory allocation, or the peek function. 5. not use anything from , , , or . 6. not use any variable names or comments that specifically state a capitalization position (such as last, 3rd to last, next to next to last, etc.). Example If the text file contains: What! Another useless, stupid, and unnecessary program? Yes: What else?: Try input redirection. VL/ 7.18.-=+#/ and Reverse is called using: while ((thisSeparator = Reverse(inFile, 1)) != EOF) cout.put(thisSeparator) the following is displayed: TAHWI rehtOna selesu, diputS. DnA yrasseceNnU margOrP? Sey: tAhW eSIE?: YT tuPnl noitceriDeR. [1] 1.19./=+= Submitting your solution Send the three source code files to the assignment checker with the subject line C2A2E4_ID, where ID is your 9-character UCSD student ID. See the course document titled "How to Prepare and Submit Assignments for additional exercise formatting. submission, and assignment checker requirements. Hints and Recommendations: 1. See course book notes 12.7 and 12.8 for recursive examples. 2. Recursive functions should use the fewest variables necessary and should not use external or static variables. 3. When calling Reverse always pass the expression level +1 as its second argument. 4. Writing an inline function that determines if its parameter is a separator and returns type bool is cleaner than directly testing for separators in the Reverse function. Note the following: a. Whitespace is not just the space character itself but is every character defined as whitespace by the isspace function. b. The ispunct function is not suitable for this exercise because it detects more than just the punctuator characters used as separators. 5. Because the value of macro EOF is type int, the implementation dependent negative value it represents cannot be reliably represented by type char. Casting a type char expression to type int does not eliminate the problem because the extra bits needed to represent EOF will have already been lost. Also, if a type int expression contains the value of EOF. casting it to type char results in a value that cannot represent EOF. 6. The step by step algorithm I recommend is shown below, although you are not required to use it. Regardless, remember that separators must never be displayed or reversed by the Reverse function but must merely be returned by it. The instructor-supplied "driver" file is responsible for displaying returned separators. Algorithm: Each time function Reverse is called it must do the following: A. Read the next input character and store it in a type int automatic variable named thischar. B. If the character in thisChar is a separator Return the character to the caller. ELSE 1) Call Reverse and store its return value in a type int automatic variable named thisSeparator. 2) If the current recursive level is a capitalization level Display the character in thischar as capitalized. ELSE Display the character in thisChar unaltered. 3) Return the character in this Separator to the caller. C2A2E4 (6 points - C++ Program) Exclude any existing source code files that may already be in your IDE project and add two new ones. naming them C2A2E4 OpenFile.cpp and C2A2E4 Reverse.cpp. Also add instructor-supplied source code file C2A2E4_main-Driver.cpp. Do not write a main function main already exists in the instructor supplied file and it will use the code you write. File C2A2E4_OpenFile.cpp must contain a function named OpenFile. 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 fileName in the read-only text mode using the inFile object. If the open fails an error message is output to cerr and the program is terminated with an error exit code. The error message must mention the name of the failing file. Return: void if the open succeeds; otherwise, the function does not retum. File C2A2E4 Reverse.cpp must contain a function named Reverse. Reverse syntax: int Reverse(ifstream &infile, const int level); Parameters: inFile - a reference to an ifstream object representing a text file open in a readable text mode. level - recursive level of this function call: 1 => Ist coll 2 => 2nd coll. etc. Synopsis: Recursively reads one character at a time from the text file in infile until a separator is read. Then any non-separator characters that were read are displayed in reverse order with the last and next to next to last characters capitalized. Finally, the separator is returned to the calling function. Reverse neither reverses nor prints separators but merely returns them. The code in the instructor-supplied driver file is responsible for printing each returned separator. Definition of separator: any whitespace (as defined by the standard library isspace function), a period, a question mark, an exclamation point a comma, a colon, a semicolon, or the end of the file. Return: the current separator The Reverse function must: 1. Implement a recursive solution and be able to display words of any length. 2. Be tested with instructor-supplied data file TestFile2.txt, which must be placed in the program's "working directory": 3. not declare more than two non-const variables other than its two parameters. 4. not use arrays, static objects, external objects, dynamic memory allocation, or the peek function. 5. not use anything from , , , or . 6. not use any variable names or comments that specifically state a capitalization position (such as last, 3rd to last, next to next to last, etc.). Example If the text file contains: What! Another useless, stupid, and unnecessary program? Yes: What else?: Try input redirection. VL/ 7.18.-=+#/ and Reverse is called using: while ((thisSeparator = Reverse(inFile, 1)) != EOF) cout.put(thisSeparator) the following is displayed: TAHWI rehtOna selesu, diputS. DnA yrasseceNnU margOrP? Sey: tAhW eSIE?: YT tuPnl noitceriDeR. [1] 1.19./=+= Submitting your solution Send the three source code files to the assignment checker with the subject line C2A2E4_ID, where ID is your 9-character UCSD student ID. See the course document titled "How to Prepare and Submit Assignments for additional exercise formatting. submission, and assignment checker requirements. Hints and Recommendations: 1. See course book notes 12.7 and 12.8 for recursive examples. 2. Recursive functions should use the fewest variables necessary and should not use external or static variables. 3. When calling Reverse always pass the expression level +1 as its second argument. 4. Writing an inline function that determines if its parameter is a separator and returns type bool is cleaner than directly testing for separators in the Reverse function. Note the following: a. Whitespace is not just the space character itself but is every character defined as whitespace by the isspace function. b. The ispunct function is not suitable for this exercise because it detects more than just the punctuator characters used as separators. 5. Because the value of macro EOF is type int, the implementation dependent negative value it represents cannot be reliably represented by type char. Casting a type char expression to type int does not eliminate the problem because the extra bits needed to represent EOF will have already been lost. Also, if a type int expression contains the value of EOF. casting it to type char results in a value that cannot represent EOF. 6. The step by step algorithm I recommend is shown below, although you are not required to use it. Regardless, remember that separators must never be displayed or reversed by the Reverse function but must merely be returned by it. The instructor-supplied "driver" file is responsible for displaying returned separators. Algorithm: Each time function Reverse is called it must do the following: A. Read the next input character and store it in a type int automatic variable named thischar. B. If the character in thisChar is a separator Return the character to the caller. ELSE 1) Call Reverse and store its return value in a type int automatic variable named thisSeparator. 2) If the current recursive level is a capitalization level Display the character in thischar as capitalized. ELSE Display the character in thisChar unaltered. 3) Return the character in this Separator to the caller

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases 11th British National Conference On Databases Bncod 11 Keele Uk July 7 9 1993 Proceedings Lncs 696

Authors: Michael F. Worboys ,Anna F. Grundy

1993rd Edition

3540569219, 978-3540569213

More Books

Students also viewed these Databases questions

Question

Make a flow chart plz

Answered: 1 week ago