LAB3 (20pts) - The Text ADT
Purpose: In this lab you will explore the implementation and applications of a Text ADT, a simplified version of the STL’s string class. This lab is found at pg. 2 in your lab manual, which can be accessed through our Libraries --> Databases --> Safari Tech Books Online -->C++ Data Structures A Laboratory Course OR at this link.
Pre-lab: Implement methods for Text ADT.
1. Carefully read pages 2-9 in the lab manual. Besides telling you exactly what you need to do, the manual provides background information and helpful hints. Use their discussion of the methods to create brief pre- and post-conditions for all methods you are implementing. The pre and post documentation goes in .h to serve as documentation for the clients using your class. Some developpers put them in both .h and .cpp. In this course it is sufficient to put them in .h, but write them before you implement the methods because they will help you with the implementation.
2. Create a project named Text ADT and populate it with the following files. Run your project. It should compile and run eventhough the methods in the .cpp file are not implemented yet; for now they each have a a dummy body to allow you to complie with no errors. You will work on implementing most of these functions as required below.
- config.h - a header file used to turn certain debugging tests on and off
- Text.h - starting header file for the Text ADT
- Text.cpp - your implementation file. Completing most of the implementation of Text.cpp is the prelab exercise and you must start on this before the lab session.
- test1.cpp - the test driver for your implementation of the Text ADT
- Note: depending on the C++ compiler you have, you might get an error about strcpy being "deprecated and unsafe to use". In this case try replacing in the constructor the line "strcpy(buffer,charSeq);" with "strcpy_s(buffer, bufferSize, charSeq);" FYI: This might happen in Microsoft Visual Studio and your code might not be portable (might give compilation errors in XCode or CLion on Mac OS, for instance).
3. Implement (the first 8 methods of the Text ADT – from constructor to showStructure()) and test (tests 1-1 through 1-5) the Text ADT, following the steps in the Implementation Notes (pg. 5) and Testing (pg. 9) sections. Before the lab, get at least 80% of the test cases to work.
4. Use the Text Worksheets.pdf to help you with your work - if not handed by instructor, print this file and provide answers for the testing scenarios in the document.
5. Bring your Text class and Text Worksheets.pdf to lab.
6. Some additional info you might find useful follows.
- For more information about C-strings, in particular, notice that a C-string consists of an array of characters, with the null character (‘\0’) indicating the end of the string (though not necessarily the end of the array). For example, for both of the following strings, cout << prints hello, and strlen returns 5.
‘h’ |
‘e’ |
‘l’ |
‘l’ |
‘o’ |
‘\0’ |
‘x’ |
- To get the length of a C-string, not counting the null character, use the function strlen. For example:
#include <cstring> //try <string> if cstring library is not found
...
char * str = “hello world”; // null-term. array of chars
cout << strlen(str) << endl; // prints 11 |
- Here’s some documentation to help you write the copy constructor:
Text:: Text ( const Text &valueText )
// Copy constructor, creates a copy of valueText. Called whenever
//
// 1) a string is passed to a function using call by value,
// 2) a function returns a string, or
// 3) a string is initialized using another string -- as in the
// declarations:
// Text str1("First"),
// str2 = str1;
{
// Step 1: Allocate memory for buffer
// Step 2: Copy the elements of valueText.buffer into buffer
// Step 3: ??? (There’s one more thing to do.)
} |
- Here’s the implementation for the overloaded assignment operator:
void Text::operator= ( const Text& other )
// Assigns other to a Text object.
{
if (this != &other) {
int rlen = other.getLength(); // Length of other
if ( rlen >= bufferSize ) // If other will not fit
{
delete [] buffer; // Release buffer and
bufferSize = rlen + 1; // allocate a new
buffer = new char [ bufferSize ]; // (larger) buffer
}
strcpy(buffer, other.buffer); // Copy other’s buffer
bufferSize = other.bufferSize; // Copy other’s bufferSize
}
} |
- Here’s the implementation for showStructure:
void Text:: showStructure () const
// Outputs the characters in a string. This operation is intended for
// testing/debugging purposes only.
{
int j; // Loop counter
for ( j = 0 ; j < bufferSize ; j++ )
cout << j << "\t";
cout << endl;
for ( j = 0 ; buffer[j] != '\0' ; j++ )
cout << buffer[j] << "\t";
cout << "\\0" << endl;
} |
- For operator[], don’t be confused by the fact that you’re overloading an operator. Just write the function so that it returns the value specified in the Text ADT Specification.
- For clear, to clear a Text object means to make it so that showStructure prints the same thing as it would for an empty Text object (e.g., empty in test1.cpp). Thus we must delete the buffer, then reallocate the buffer with a size of one, assign to buffer[0] = ‘\0’ and set bufferSize to one.
In-lab: Programming Exercise 1
1. If any test cases for your Text class are not working, you may ask the instructor for help.
2. Do Programming Exercise 1 from the lab manual. Begin with the starting code, LexicalAnalyzer.cpp, which handles file input for you. Note: make sure you do NOT have two main() methods in one project. (Temporarely, you could change one to something like main33()).
3. Other files you might find useful for this activity are
textio.cpp - code stubs for certain methods used in the lab
progsamp.dat - the input file for LexicalAnalyzer.cpp
subjects.txt - another sample input file
4. Two possible outputs for this activity are output_1 and output_2. When done with this activity, run your program with progsmap.dat as your input, and create a snapshot of your output. Name it ex1_YourLastName.png (or .jpg). You will submit this picture with your project.
Note: use the global path when inputing into your program the name of your text file (similarly, I used /Users/svisa/Desktop/TextADT/TextADT/progsmap.dat).
Post-lab: Programming Exercise 3
- Do Programming Exercise 3. You are recommended to start the post-lab during lab. It will take longer than the in-lab part.
- Search the web for information on how to use strcmp to compare two character arrays.
- Follow the lab instructions for exercise three to test your implementation of the operators.
- A possible output for this activity is output_3. When done with this activity, run your program to test it as described in your labmanual, and take a snapshot of your output; name it ex3_YourLastName.png (or .jpg). You will submit this picture with your project.
- Submit the two pictures and the zip file of your CLion TextADT project/folder (Control-click on the folder and select Compress). Hand Text Worksheet.pdf to ptofessor OR slide it under professor's office door, Taylor 304.
Grading:
- Text Worksheet.pdf: 4pts
- Pre, post in .h files: 2pts (make sure you have them for ALL methods, not only the ones you wrote.)
- 8 methods (3 are given to you, 5 you are implementing): 10pts
- Progr. ex. 1: 2pts (1 pt is the picture)
- Progr. ex. 3: 2pts (1 pt is the picture)