Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I Laboratory 1 (because periods are not legal in the preprocessor na follows: fi

ID: 3875133 • Letter: I

Question

I Laboratory 1 (because periods are not legal in the preprocessor na follows: fifadet TEXT H idetine TEXT H fendif Compilat // Rest of code in file Cor When the compiler preprocessor is reading the file, it encounters statement. -ifndef"means .IF Not DEFined"..The string fn ,ited the titat op er called T TEXT_H identifier has not been created, the statement is true; it TRXT_H statement, defines a TEXT K identifier, and processes everything whether the preprocessor has created (defined) an identifier cal up through the fendit statement. Next time the file gets included, TEXT defined, so the sifndef TEXT.JH is false and all lines get skipped through t Testing Step 1: Implement the operations in the Text ADT using this string scheme. Base your implementation on the following class the file Tert.h declaratiog (Note: all header files for this book are included in the student file s including a copy of the header file for this lab for the student's convenietc being introduced to implementing ADTs with C++) claes Text public /I Construetors and operator- Text (const char charSeq: Text (const Text& other ): void operator - eonst Text&other; // Destructor Text O: // Text operations int gettength const: char operator [ int n const: void clear : /I Output the string structure used in testing/debugging void shovStructure () const; /I toUpper/toLover operations (Exercise 2) Text toUpper const: Text toLover() const: /I Relational operations (Exercise 3) / Initialize using char /1 Copy constructor II Assignment //charactets / Subscript // Clear string /I Create lower-case copy Create upper-case copy 1 operatorconst Text& other const: bool operator (const Texts other) const: bool operator > (const Texts other) const: private: I Data senbers bufferSize: I Size of the string buffer char* buffer: /I Text b II sequence uffer containing a null-terminated char Step 2: Save document your code. entation of the Text ADT in the file Tertepp Be st sure w

Explanation / Answer

test1.cpp

//--------------------------------------------------------------------
//
// Laboratory 1                                           test1.cpp
//
// Test program for the operations in the Text ADT
//
//--------------------------------------------------------------------

#include <iostream>
#include "Text.h"
#include "config.h"

//--------------------------------------------------------------------
//
// Function prototype

void copyTester ( Text copyText );   // copyText is passed by value
void print_help ( );

//--------------------------------------------------------------------

int main()
{
    Text a("a"),                // Predefined test text objects
         alp("alp"),
         alpha("alpha"),
         epsilon("epsilon"),
         empty,
         assignText,          // Destination for assignment
         inputText;           // Input text object
    int n;                        // Input subscript
    char ch,                      // Character specified by subscript
         selection;               // Input test selection

    // Get user test selection.
    print_help();

    // Execute the selected test.
    cin >> selection;

    cout << endl;
    switch ( selection )
    {
      case '1' :
           // Test 1 : Tests the constructors.
           cout << "Structure of various text objects: " << endl;
           cout << "text object: alpha" << endl;
           alpha.showStructure();
           cout << "text object: epsilon" << endl;
           epsilon.showStructure();
           cout << "text object: a" << endl;
           a.showStructure();
           cout << "empty text object" << endl;
           empty.showStructure();
           break;
      case '2' :
           // Test 2 : Tests the length operation.
           cout << "Lengths of various text object:" << endl;
           cout << " alpha   : " << alpha.getLength() << endl;
           cout << " epsilon : " << epsilon.getLength() << endl;
           cout << " a       : " << a.getLength() << endl;
           cout << " empty   : " << empty.getLength() << endl;
           break;

      case '3' :
           // Test 3 : Tests the subscript operation.
           cout << "Enter a subscript : ";
           cin >> n;
           ch = alpha[n];
           cout << " alpha[" << n << "] : ";
           if ( ch == '' )
              cout << "\0" << endl;
           else
              cout << ch << endl;
           break;

      case '4' :
           // Test 4 : Tests the assignment and clear operations.
           cout << "Assignments:" << endl;
           cout << "assignText = alpha" << endl;
           assignText = alpha;
           assignText.showStructure();
           cout << "assignText = a" << endl;
           assignText = a;
           assignText.showStructure();
           cout << "assignText = empty" << endl;
           assignText = empty;
           assignText.showStructure();
           cout << "assignText = epsilon" << endl;
           assignText = epsilon;
           assignText.showStructure();
           cout << "assignText = assignText" << endl;
           assignText = assignText;
           assignText.showStructure();
           cout << "assignText = alpha" << endl;
           assignText = alpha;
           assignText.showStructure();
           cout << "Clear assignText" << endl;
           assignText.clear();
           assignText.showStructure();
           cout << "Confirm that alpha has not been cleared" << endl;
           alpha.showStructure();
           break;

      case '5' :
           // Test 5 : Tests the copy constructor and operator= operations.
           cout << "Calls by value:" << endl;
           cout << "alpha before call" << endl;
           alpha.showStructure();
           copyTester(alpha);
           cout << "alpha after call" << endl;
           alpha.showStructure();

           cout << "a before call" << endl;
           a.showStructure();
           a = epsilon;
           cout << "a after call" << endl;
           a.showStructure();
           cout << "epsilon after call" << endl;
           epsilon.showStructure();
           break;

#if   LAB1_TEST1
      case '6' :                                  // In-lab Exercise 2
           // Test 6 : Tests toUpper and toLower
           cout << "Testing toUpper and toLower."
                << "Enter a mixed case string: " << endl;
       cin >> inputText;
       cout << "Input string:" << endl;
           inputText.showStructure();
           cout << "Upper case copy: " << endl;
           inputText.toUpper().showStructure();
           cout << "Lower case copy: " << endl;
           inputText.toLower().showStructure();
           break;
#endif // LAB1_TEST1

#if   LAB1_TEST2
      case '7' :                                  // In-lab Exercise 3
           // Test 7 : Tests the relational operations.
           cout << " left     right     <   ==   > " << endl;
           cout << "--------------------------------" << endl;
           cout << " alpha    epsilon    " << (alpha<epsilon)
                << "    " << (alpha==epsilon) << "   "
                << (alpha>epsilon) << endl;
           cout << " epsilon   alpha     " << (epsilon<alpha)
                << "    " << (epsilon==alpha) << "   "
                << (epsilon>alpha) << endl;
           cout << " alpha     alpha     " << (alpha<alpha) << "    "
                << (alpha==alpha) << "   " << (alpha>alpha) << endl;
           cout << " alp      alpha     " << (alp<alpha) << "    "
                << (alp==alpha) << "   " << (alp>alpha) << endl;
           cout << " alpha      alp      " << (alpha<alp) << "    "
                << (alpha==alp) << "   " << (alpha>alp) << endl;
           cout << "   a       alpha     " << (a<alpha) << "    "
                << (a==alpha) << "   " << (a>alpha) << endl;
           cout << " alpha       a       " << (alpha<a) << "    "
                << (alpha==a) << "   " << (alpha>a) << endl;
           cout << " empty     alpha     " << (empty<alpha) << "    "
                << (empty==alpha) << "   " << (empty>alpha) << endl;
           cout << " alpha     empty     " << (alpha<empty) << "    "
                << (alpha==empty) << "   " << (alpha>empty) << endl;
           cout << " empty     empty     " << (empty<empty) << "    "
                << (empty==empty) << "   " << (empty>empty) << endl;
           break;
#endif // LAB1_TEST2

      default :
           cout << "'" << selection << "' specifies an inactive or invalid test" << endl;
    }

    return 0;
}

//--------------------------------------------------------------------

void copyTester ( Text copyText )

// Dummy routine that is passed a text object using call by value. Outputs
// copyText and clears it.

{
    cout << "Copy of text object" << endl;
    copyText.showStructure();
    cout << "Clear copy" << endl;
    copyText.clear();
    copyText.showStructure();
}

//--------------------------------------------------------------------

void print_help()
{
    cout << endl << "Tests:" << endl;
    cout << " 1 Tests the constructors" << endl;
    cout << " 2 Tests the length operation" << endl;
    cout << " 3 Tests the subscript operation" << endl;
    cout << " 4 Tests the assignment and clear operations" << endl;
    cout << " 5 Tests the copy constructor and operator= operations" << endl;

    cout << " 6 Tests the toUpper and toLower operations      "
#if   LAB1_TEST1
         << "(Active   : "
#else
         << "(Inactive : "
#endif   // LAB1_TEST1
         << "In-lab Exercise 2)" << endl;

    cout << " 7 Tests the relational operations    "
#if   LAB1_TEST2
         << "           (Active   : "
#else
         << "           (Inactive : "
#endif   // LAB1_TEST2
         << "In-lab Exercise 3)" << endl;
    cout << "Select the test to run : ";
}


Text.h

//--------------------------------------------------------------------
//
// Laboratory 1                                           test1.cpp
//
// Test program for the operations in the Text ADT
//
//--------------------------------------------------------------------

#include <iostream>
#include "Text.h"
#include "config.h"

//--------------------------------------------------------------------
//
// Function prototype

void copyTester ( Text copyText );   // copyText is passed by value
void print_help ( );

//--------------------------------------------------------------------

int main()
{
    Text a("a"),                // Predefined test text objects
         alp("alp"),
         alpha("alpha"),
         epsilon("epsilon"),
         empty,
         assignText,          // Destination for assignment
         inputText;           // Input text object
    int n;                        // Input subscript
    char ch,                      // Character specified by subscript
         selection;               // Input test selection

    // Get user test selection.
    print_help();

    // Execute the selected test.
    cin >> selection;

    cout << endl;
    switch ( selection )
    {
      case '1' :
           // Test 1 : Tests the constructors.
           cout << "Structure of various text objects: " << endl;
           cout << "text object: alpha" << endl;
           alpha.showStructure();
           cout << "text object: epsilon" << endl;
           epsilon.showStructure();
           cout << "text object: a" << endl;
           a.showStructure();
           cout << "empty text object" << endl;
           empty.showStructure();
           break;
      case '2' :
           // Test 2 : Tests the length operation.
           cout << "Lengths of various text object:" << endl;
           cout << " alpha   : " << alpha.getLength() << endl;
           cout << " epsilon : " << epsilon.getLength() << endl;
           cout << " a       : " << a.getLength() << endl;
           cout << " empty   : " << empty.getLength() << endl;
           break;

      case '3' :
           // Test 3 : Tests the subscript operation.
           cout << "Enter a subscript : ";
           cin >> n;
           ch = alpha[n];
           cout << " alpha[" << n << "] : ";
           if ( ch == '' )
              cout << "\0" << endl;
           else
              cout << ch << endl;
           break;

      case '4' :
           // Test 4 : Tests the assignment and clear operations.
           cout << "Assignments:" << endl;
           cout << "assignText = alpha" << endl;
           assignText = alpha;
           assignText.showStructure();
           cout << "assignText = a" << endl;
           assignText = a;
           assignText.showStructure();
           cout << "assignText = empty" << endl;
           assignText = empty;
           assignText.showStructure();
           cout << "assignText = epsilon" << endl;
           assignText = epsilon;
           assignText.showStructure();
           cout << "assignText = assignText" << endl;
           assignText = assignText;
           assignText.showStructure();
           cout << "assignText = alpha" << endl;
           assignText = alpha;
           assignText.showStructure();
           cout << "Clear assignText" << endl;
           assignText.clear();
           assignText.showStructure();
           cout << "Confirm that alpha has not been cleared" << endl;
           alpha.showStructure();
           break;

      case '5' :
           // Test 5 : Tests the copy constructor and operator= operations.
           cout << "Calls by value:" << endl;
           cout << "alpha before call" << endl;
           alpha.showStructure();
           copyTester(alpha);
           cout << "alpha after call" << endl;
           alpha.showStructure();

           cout << "a before call" << endl;
           a.showStructure();
           a = epsilon;
           cout << "a after call" << endl;
           a.showStructure();
           cout << "epsilon after call" << endl;
           epsilon.showStructure();
           break;

#if   LAB1_TEST1
      case '6' :                                  // In-lab Exercise 2
           // Test 6 : Tests toUpper and toLower
           cout << "Testing toUpper and toLower."
                << "Enter a mixed case string: " << endl;
       cin >> inputText;
       cout << "Input string:" << endl;
           inputText.showStructure();
           cout << "Upper case copy: " << endl;
           inputText.toUpper().showStructure();
           cout << "Lower case copy: " << endl;
           inputText.toLower().showStructure();
           break;
#endif // LAB1_TEST1

#if   LAB1_TEST2
      case '7' :                                  // In-lab Exercise 3
           // Test 7 : Tests the relational operations.
           cout << " left     right     <   ==   > " << endl;
           cout << "--------------------------------" << endl;
           cout << " alpha    epsilon    " << (alpha<epsilon)
                << "    " << (alpha==epsilon) << "   "
                << (alpha>epsilon) << endl;
           cout << " epsilon   alpha     " << (epsilon<alpha)
                << "    " << (epsilon==alpha) << "   "
                << (epsilon>alpha) << endl;
           cout << " alpha     alpha     " << (alpha<alpha) << "    "
                << (alpha==alpha) << "   " << (alpha>alpha) << endl;
           cout << " alp      alpha     " << (alp<alpha) << "    "
                << (alp==alpha) << "   " << (alp>alpha) << endl;
           cout << " alpha      alp      " << (alpha<alp) << "    "
                << (alpha==alp) << "   " << (alpha>alp) << endl;
           cout << "   a       alpha     " << (a<alpha) << "    "
                << (a==alpha) << "   " << (a>alpha) << endl;
           cout << " alpha       a       " << (alpha<a) << "    "
                << (alpha==a) << "   " << (alpha>a) << endl;
           cout << " empty     alpha     " << (empty<alpha) << "    "
                << (empty==alpha) << "   " << (empty>alpha) << endl;
           cout << " alpha     empty     " << (alpha<empty) << "    "
                << (alpha==empty) << "   " << (alpha>empty) << endl;
           cout << " empty     empty     " << (empty<empty) << "    "
                << (empty==empty) << "   " << (empty>empty) << endl;
           break;
#endif // LAB1_TEST2

      default :
           cout << "'" << selection << "' specifies an inactive or invalid test" << endl;
    }

    return 0;
}

//--------------------------------------------------------------------

void copyTester ( Text copyText )

// Dummy routine that is passed a text object using call by value. Outputs
// copyText and clears it.

{
    cout << "Copy of text object" << endl;
    copyText.showStructure();
    cout << "Clear copy" << endl;
    copyText.clear();
    copyText.showStructure();
}

//--------------------------------------------------------------------

void print_help()
{
    cout << endl << "Tests:" << endl;
    cout << " 1 Tests the constructors" << endl;
    cout << " 2 Tests the length operation" << endl;
    cout << " 3 Tests the subscript operation" << endl;
    cout << " 4 Tests the assignment and clear operations" << endl;
    cout << " 5 Tests the copy constructor and operator= operations" << endl;

    cout << " 6 Tests the toUpper and toLower operations      "
#if   LAB1_TEST1
         << "(Active   : "
#else
         << "(Inactive : "
#endif   // LAB1_TEST1
         << "In-lab Exercise 2)" << endl;

    cout << " 7 Tests the relational operations    "
#if   LAB1_TEST2
         << "           (Active   : "
#else
         << "           (Inactive : "
#endif   // LAB1_TEST2
         << "In-lab Exercise 3)" << endl;
    cout << "Select the test to run : ";
}


Text.cpp

/* ***USE ZYBOOK 5.15 FOR C STRING SYNTAX INFORMATION*** */

#include "Text.h"
#include <iostream>
#include <cstring>
#include <fstream>
#include <iomanip>

using namespace std;

Text::Text (const char* charSeq){
   bufferSize = strlen (charSeq) + 1;
   buffer = new char[bufferSize];
   strcpy(buffer, charSeq);
}

Text::Text (const Text& other){
   bufferSize = other.getLength();
   buffer = new char[bufferSize];
   strcpy(buffer, other.getLetters());
}

void Text::operator = (const Text& other){
   if(buffer != other.getLetters()){
   bufferSize = other.getLength();
   buffer = new char[bufferSize];
   strcpy(buffer, other.getLetters());
   }
}

char Text::operator [] (int n) const{
   if (n < bufferSize){
       return buffer[n];
   } else {
       return '';
   }
}

int Text::getLength()const{
   return (bufferSize - 1);
}

char* Text::getLetters()const{
   return buffer;
}

void Text::clear(){
   buffer[0] = '';
}

Text::~Text(){
   delete buffer;
}

void Text::showStructure()const{
cout << buffer << endl;
}

// ******** Individual Stuff **********

//Text Text::toUpper(const Text& other){
//   int i = 0;
//   char* word = other;
//   char c;
//   while (word[i]){
//       c=word[i];
//       word[i] = toupper(c);
//       i++;
//   }
//
//}
//
//Text Text::toLower(const Text& other){
//   int i = 0;
//   char* word = other;
//   char c;
//   while (word[i]){
//       c=word[i];
//       word[i] = tolower(c);
//       i++;
//   }
//
//}
// ************************** TEXTIO.CPP STUFF ***************************************
istream & operator >> ( istream &input, Text &inputText )

// Text input function. Extracts a string from istream input and
// returns it in inputText. Returns the state of the input stream.

{
    const int textBufferSize = 256;     // Large (but finite)
    char textBuffer [textBufferSize];   // text buffer

    // Read a string into textBuffer, setw is used to prevent buffer
    // overflow.

    input >> setw(textBufferSize) >> textBuffer;

    // Apply the Text(char*) constructor to convert textBuffer to
    // a string. Assign the resulting string to inputText using the
    // assignment operator.

    inputText = textBuffer;

    // Return the state of the input stream.

    return input;
}

//--------------------------------------------------------------------

ostream & operator << ( ostream &output, const Text &outputText )

// Text output function. Inserts outputText in ostream output.
// Returns the state of the output stream.

{
   output << outputText.buffer;
   return output;
}

config.h

/**
* Text class (Lab 1) configuration file.
* Activate test 'N' by defining the corresponding LAB1_TESTN to have the value 1.
*/

#define LAB1_TEST1   0       // Programming exercise 2: toUpper and toLower
#define LAB1_TEST2   0       // Programming exercise 3: ==, <, and >