Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Text.h ------------------------------------------------------------------------------------ // Laboratory 1 Text.h // **Instructor's Solution** // Class declaration for the array implementation of the Text ADT // //-------------------------------------------------------------------- #ifndef TEXT_H #define

image text in transcribed

 

Text.h

------------------------------------------------------------------------------------

// Laboratory 1 Text.h

// **Instructor's Solution**

// Class declaration for the array implementation of the Text ADT

//

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

#ifndef TEXT_H

#define TEXT_H

#include

#include

using namespace std;

class Text

{

public:

// Constructors and operator=

Text(const char *charSeq = ""); // Initialize using char*

Text(const Text &other); // Copy constructor

void operator = (const Text &other); // Assignment

Text operator += (const Text &input2); //lab 2

// Destructor

~Text();

// Text operations

int getLength() const; // # characters

char operator [] (int n) const; // Subscript

void clear(); // Clear string

// Output the string structure -- used in testing/debugging

void showStructure() const;

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

// In-lab operations

// toUpper/toLower operations (Programming Exercise 2)

Text toUpper() const; // Create upper-case copy

Text toLower() const; // Create lower-case copy

// Relational operations (Programming Exercise 3)

bool operator == (const Text& other) const;

bool operator

bool operator > (const Text& other) const;

char toReverse(Text obj, int position) const; //lab 2

private:

int count = 0;

// Data members

int bufferSize; // Size of the string buffer

char *buffer; // Text buffer containing a null-terminated sequence of characters

// Friends

// Text input/output operations (In-lab Exercise 1)

friend istream & operator >> (istream& input, Text& inputText);

friend ostream & operator

};

#endif

Text.cpp

---------------------------------------------------------------------------------------------------

#include

#include

#include

#include

#include "Text.h"

Text::Text ( const char *charSeq )

{

bufferSize = strlen(charSeq) + 1;

buffer = new char[bufferSize];

strcpy(buffer, charSeq);

}

Text::Text ( const Text &other )

{

bufferSize = other.bufferSize;

buffer = new char[bufferSize];

strcpy(buffer, other.buffer);

}

void Text::operator = ( const Text &other )

{

int l = other.bufferSize;//l=length

if(l>bufferSize)

{

delete[] buffer;

bufferSize = other.bufferSize;

buffer = new char[bufferSize];

}

else if (l

{

delete[] buffer;

bufferSize = other.bufferSize;

buffer = new char[bufferSize];

}

strcpy(buffer, other.buffer);

}

Text::~Text ()

{

delete[] buffer;

}

int Text::getLength () const

{

return bufferSize -1;

}

char Text::operator [] ( int n ) const

{

if (n >= 0 && n

return buffer[n];

}

return '0';

}

void Text::clear ()

{

buffer[0] = '0';

}

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

cout

cout

for (j = 0; buffer[j] != '0'; j++)

cout

cout

}

Text Text::toUpper( ) const

{

Text temp(buffer);

for (int i = 0; i

{

if ((buffer[i] > 96 && (buffer[i]

{

temp.buffer[i] = char(buffer[i] - 32);

}

}

return temp;

}

Text Text::toLower( ) const

{

Text temp(buffer);

for (int i = 0; i

{

if ((buffer[i] > 64 && (buffer[i]

{

temp.buffer[i] = char(buffer[i] + 32);

}

}

return temp;

}

bool Text::operator == (const Text& other) const

{

if (getLength() == other.getLength())

{

if (bufferSize == 0)

{

return true;

}

else

{

for (int i = 0; i

{

if (buffer[i] != other.buffer[i])

{

return false;

}

}

return true;

}

}

else

{

return false;

}

}

bool Text::operator

{

if (getLength()

{

return true;

}

else

{

return false;

}

}

bool Text::operator > ( const Text& other ) const

{

if (getLength() > other.getLength())

{

return true;

}

else

{

return false;

}

}

test1.cpp

--------------------------------------------------------------------

//

// Laboratory 1 test1.cpp

//

// Test program for the operations in the Text ADT

//

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

#include

#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

inputText2;

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

switch (selection)

{

case '1':

// Test 1 : Tests the constructors.

cout

cout

alpha.showStructure();

cout

epsilon.showStructure();

cout

a.showStructure();

cout

empty.showStructure();

break;

case '2':

// Test 2 : Tests the length operation.

cout

cout

cout

cout

cout

break;

case '3':

// Test 3 : Tests the subscript operation.

cout

cin >> n;

ch = alpha[n];

cout

if (ch == '0')

cout

else

cout

break;

case '4':

// Test 4 : Tests the assignment and clear operations.

cout

cout

assignText = alpha;

assignText.showStructure();

cout

assignText = a;

assignText.showStructure();

cout

assignText = empty;

assignText.showStructure();

cout

assignText = epsilon;

assignText.showStructure();

cout

assignText = assignText;

assignText.showStructure();

cout

assignText = alpha;

assignText.showStructure();

cout

assignText.clear();

assignText.showStructure();

cout

alpha.showStructure();

break;

case '5':

// Test 5 : Tests the copy constructor and operator= operations.

cout

cout

alpha.showStructure();

copyTester(alpha);

cout

alpha.showStructure();

cout

a.showStructure();

a = epsilon;

cout

a.showStructure();

cout

epsilon.showStructure();

break;

#if LAB1_TEST1

case '6': // In-lab Exercise 2

// Test 6 : Tests toUpper and toLower

cout

cin >> inputText;

cout

inputText.showStructure();

cout

inputText.toUpper().showStructure();

cout

inputText.toLower().showStructure();

break;

#endif // LAB1_TEST1

#if LAB1_TEST2

case '7': // In-lab Exercise 3

// Test 7 : Tests the relational operations.

cout "

cout

cout

epsilon)

cout

alpha)

cout

alpha)

cout

alpha)

cout

alp)

cout

alpha)

cout

a)

cout

alpha)

cout

empty)

cout

empty)

break;

#endif // LAB1_TEST2

#if LAB2_TEST1

case '8':

cout

cin >> inputText;

inputText.toReverse(inputText, 0);

//inputText.showStructure();

break;

#endif // LAB2_TEST1

#if LAB2_TEST2

case '9':

cout

cin >> inputText;

cout

cin >> inputText2;

inputText += inputText2;

inputText.toReverse(inputText, 0);

break;

#endif // LAB2_TEST2

default:

cout

}

system("PAUSE");

return 0;

}

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

void copyTester(Text copyText)

// Dummy routine that is passed a text object using call by value. Outputs

// copyText and clears it.

{

cout

copyText.showStructure();

cout

copyText.clear();

copyText.showStructure();

}

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

void print_help()

{

cout

cout

cout

cout

cout

cout

cout

#if LAB1_TEST1

#else

#endif // LAB1_TEST1

cout

#if LAB1_TEST2

#else

#endif // LAB1_TEST2

cout

cout

cout

}

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 1 // Programming exercise 2: toUpper and toLower

#define LAB1_TEST2 1 // Programming exercise 3: ==,

#define LAB2_TEST1 1 // Programming recursion

#define LAB2_TEST2 1

textio.cpp

-------------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------- // // Laboratory 1, In-lab Exercise 1 textio.cpp // // String input/output operations // //--------------------------------------------------------------------

#include #include

#include "Text.h"

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

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

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

 

{ output   Q4. (30 pts) On the foundation of the previous Text ADT homework, add the following methods to the Text class: 1. Implement the function toReverse0 to reverse recursively the order of letters of the given Text object. Test your implementation by activating LAB2_TESTI (15 pts) Function prototype: Text toReverselText obj, int position) const 2. Implement the operators +-. Test it by activating LAB-TEST2. (15 pts) Function prototype: Text operator +(const Text &input2); Notel: Source code with the test cases are provided (text.h and test2.cpp). Make sure you create corresponding methods in text.cpp as well. There were added the following two lines into the configuration file: #define LAB2 TEST1 0 #define LAB2 TEST2 0 // Programming recursion // Programming operation and recursion Plus, there are included test cases to test your implementation

need help in C++!!

everything is given just need to implement the two function ,Text toReverse(Text obj, int position) const; and Text operator += (const Text &input2); in Text.cpp file. please attach the output as well. thank you.

Q4. (30 pts) On the foundation of the previous Text ADT homework, add the following methods to the Text class: 1. Implement the function to Reverse() to reverse recursively the order of letters of the given Text object. Test your implementation by activating LAB2_TEST1. (15 pts) Function prototype: Text to Reverse (Text obj, int position) const; 2. Implement the operators +=. Test it by activating LAB2_TEST2. (15 pts) Notel: Function prototype: Text operator += (const Text &input2); Source code with the test cases are provided (text.h and test2.cpp). Make sure you create corresponding methods in text.cpp as well. There were added the following two lines into the configuration file: #define LAB2_TEST10 #define LAB2_TEST20 // Programming recursion // Programming operation + and recursion Plus, there are included test cases to test your implementation. Q4. (30 pts) On the foundation of the previous Text ADT homework, add the following methods to the Text class: 1. Implement the function to Reverse() to reverse recursively the order of letters of the given Text object. Test your implementation by activating LAB2_TEST1. (15 pts) Function prototype: Text to Reverse (Text obj, int position) const; 2. Implement the operators +=. Test it by activating LAB2_TEST2. (15 pts) Notel: Function prototype: Text operator += (const Text &input2); Source code with the test cases are provided (text.h and test2.cpp). Make sure you create corresponding methods in text.cpp as well. There were added the following two lines into the configuration file: #define LAB2_TEST10 #define LAB2_TEST20 // Programming recursion // Programming operation + and recursion Plus, there are included test cases to test your implementation.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

To implement the two methods toReverse and operator in your Text class within Textcpp follow these steps 1 toReverseText obj int position This function will reverse the string contained in the Text ob... 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_2

Step: 3

blur-text-image_step3

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

More Books

Students explore these related Databases questions