Question

In this lab, you will need to implement the following functions in Text ADT with C++ language(Not C#, Not Java please!):

PS: The program I'm using is Visual Studio just to be aware of the format. And I have provided all informations already! Please finish step 1, 2, 3, 4. Code is the correct format of C++ code.

a. Constructors and operator =

b. Destructor

c. Text operations (length, subscript, clear)

1. Implement the aforementioned operations in the Text ADT using the string representation scheme. Base your implementation on the class declaration from the file Text.h.

2. Save your implementation of the Text ADT in the file Text.cpp. Be sure to document your code.

3. Test your implementation of the Text ADT using the program in the file test1.cpp. And follow theOnlineTestPlan_Lab01.doc (attached) page 2-3 to test your code.

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 >

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

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 == '\0' )

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

<< " " << (alpha==epsilon) << " "

<< (alpha>epsilon) << endl;

cout << " epsilon alpha " << (epsilon

<< " " << (epsilon==alpha) << " "

<< (epsilon>alpha) << endl;

cout << " alpha alpha " << (alpha

<< (alpha==alpha) << " " << (alpha>alpha) << endl;

cout << " alp alpha " << (alp

<< (alp==alpha) << " " << (alp>alpha) << endl;

cout << " alpha alp " << (alpha

<< (alpha==alp) << " " << (alpha>alp) << endl;

cout << " a alpha " << (a

<< (a==alpha) << " " << (a>alpha) << endl;

cout << " alpha a " << (alpha

<< (alpha==a) << " " << (alpha>a) << endl;

cout << " empty alpha " << (empty

<< (empty==alpha) << " " << (empty>alpha) << endl;

cout << " alpha empty " << (alpha

<< (alpha==empty) << " " << (alpha>empty) << endl;

cout << " 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 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

// 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 < ( const Text& other ) const;
bool operator > ( const Text& other ) const;

private:

// 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 << ( ostream& output, const Text& outputText );

};

#endif

textio.cpp:

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

#include
#include

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

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;
}

progsamp.dat:

void main ( )
{
int j ,
total = 0 ;
for ( j = 1 ; j <= 20 ; j ++ )
total += j ;
}

Laboratory 1: Cover Sheet Name Date Section Place a check mark in the Assigned column next to the exercises your instructor has assigned to you. Attach this cover sheet to the front of the packet of materials you submit following the laboratory Assigned: Check or list exercise numbers Completed Activities Implementation Testing Programming Exercise 1 Programming Exercise 2 Programming Exercise 3 Analysis Exercise 1 Analysis Exercise 2 Total

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM :

CODE IN C++ :

Text.cpp:

#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstring>
#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 len = other.bufferSize;
if (len > bufferSize)
{
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
{
// check if n is out of range first
if(n >= 0 && n < getLength()) {
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 < bufferSize; j++)
cout << j << "\t";
cout << endl;
for (j = 0; buffer[j] != '\0'; j++)
cout << buffer[j] << "\t";
cout << "\\0" << endl;
}
Text Text::toUpper( ) const
{
Text temp(buffer);
for (int i = 0; i < bufferSize; i++)
{
if ((buffer[i] > 96 && (buffer[i] < 123)))
{
temp.buffer[i] = char(buffer[i] - 32);
}
}
return temp;
}
Text Text::toLower( ) const
{
Text temp(buffer);
for (int i = 0; i < bufferSize; i++)
{
if ((buffer[i] > 64 && (buffer[i] < 91)))
{
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 < bufferSize; i++)
{
if (buffer[i] != other.buffer[i])
{
return false;
}
}
return true;
}
}
else
{
return false;
}
}
}
bool Text::operator < ( const Text& other ) const
{
if (getLength() < other.getLength())
{
return true;
}
else
{
return false;
}
}
bool Text::operator > ( const Text& other ) const
{
if (getLength() > other.getLength())
{
return true;
}
else
{
return false;
}
}


Output:
ests: 1 Tests the constructors 2 Tests the length operation 3 Tests the subscript operation 4 Tests the assignment and clear

Add a comment
Know the answer?
Add Answer to:
In this lab, you will need to implement the following functions in Text ADT with C++...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are to implement a MyString class which is our own limited implementation of the std::string...

    You are to implement a MyString class which is our own limited implementation of the std::string Header file and test (main) file are given in below, code for mystring.cpp. Here is header file mystring.h /* MyString class */ #ifndef MyString_H #define MyString_H #include <iostream> using namespace std; class MyString { private:    char* str;    int len; public:    MyString();    MyString(const char* s);    MyString(MyString& s);    ~MyString();    friend ostream& operator <<(ostream& os, MyString& s); // Prints string    MyString& operator=(MyString& s); //Copy assignment    MyString& operator+(MyString&...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • I've posted 3 classes after the instruction that were given at start You will implement and...

    I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class...

    C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class in separate files for the interface and implementation so that the implementation can be compiled separately from any application program. Submit three files: YourLastNameFirstNameInitialMoney.cpp - This file contains only the Money class implementation YourLastNameFirstNameInitialProj4.cpp - This file contains only the main function to use/test your money class YourLastNameFirstNameInitialMoney.h - This file contains the header file information.   Optional bonus (10%): Do some research on make...

  • C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all...

    C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Submit your completed source (.cpp) file. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator:...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will...

    Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will call our version MyString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The MyString class will handle constructing strings, reading/printing, and accessing characters. In addition, the MyString object will have the ability to make a full deep-copy of itself...

  • Objectives You will implement and test a class called MyString. Each MyString object keeps track ...

    Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT