Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Requirements: This week you'll be making the following refinements to the class that you wrote in the last assignment. All the requirements from that class

Requirements:

This week you'll be making the following refinements to the class that you wrote in the last assignment. All the requirements from that class are still in force. For example, all MyStrings must always be stored in a dynamic array that is exactly the correct size to store the string. Your score on this assignment will take into consideration your work on both the previous assignment and this assignment.

1. Extraction Operator

Just like the >> operator that reads C-strings, your >> operator should skip any leading spaces and then read characters into the string up to the first whitespace character.

For reasons of convenience, we will impose a limit of 127 on the number of characters this function will read. This is so you can temporarily read into a non-dynamic array and then copy what you need into your data member, which will be a dynamic array. Note that this does not mean that all MyStrings will always have a maximum of 127 characters. For example, you might get a MyString with more than 127 characters by using the MyString constructor or by concatenating two MyStrings.

Hint: Don't try to read character by character in a loop. Use the extraction operator to do the reading of the input into a non-dynamic array, then use strcpy() to copy it into your data member. Make sure to allocate the correct amount of memory.

Hint: if you use the extraction operator as suggested above, you will not have to skip leading whitespace, because the extraction operator does that for you.

2. A read() function

The read() function will allow the client programmer to specify the delimiting character (the one to stop at instead of the first space). This will be a void function that will take two arguments, a stream and the delimiting character. It should not skip leading spaces. The limit of 127 characters imposed on the >> function above also applies to this function.

Hint: Don't try to read character by character in a loop. Use the in.getline() function to do the reading of the input into a non-dynamic array, then use strcpy() to copy it into your data member.

3. Concatenation Operator

Overload the + operator to do MyString concatenation. The operator must be able to handle either MyString objects or C-strings on either side of the operator. Be careful with the memory management here. You'll have to allocate enough memory to hold the new MyString. I suggest using strcpy() to get the left operand into the result MyString, and then strcat() to append the right operand. Both strcpy() and strcat() should be used as if they are void, even though they do have return values.

4. Combined Concatenation/Assignment Operator

Overload the shorthand += to combine concatenation and assignment. Only MyStrings can be on the left-hand side of a += operation, but either MyStrings or C-strings may appear on the right side. If you pay close attention to the += operator from the feetInches class, these may be the easiest points of the semester.

5. Add Documentation

Hints about reading input files:

I suggest that you copy the text from the input file webpage(s) and paste it into a file that you have created using your IDE. The files you create when you type in your IDE are always text files (even if they don't end with .txt). If you're using Windows, you could also use Notepad, but there's no reason to open another application when you are already working in your IDE. I strongly suggest that you don't use TextEdit (Mac) or Word, because these do not store files as text files by default.

Client File:

/* * ------------------- * These functions are designed to help you test your MyString objects, * as well as show the client usage of the class. * * The BasicTest function builds an array of strings using various * constructor options and prints them out. It also uses the String * stream operations to read some strings from a data file. * * The RelationTest function checks out the basic relational operations * (==, !=, <, etc) on Strings and char *s. * * The ConcatTest functions checks the overloaded + and += operators that * do string concatenation. * * The CopyTest tries out the copy constructor and assignment operators * to make sure they do a true deep copy. * * Although not exhaustive, these tests will help you to exercise the basic * functionality of the class and show you how a client might use it. * * While you are developing your MyString class, you might find it * easier to comment out functions you are ready for, so that you don't * get lots of compile/link complaints. */ #include "mystring.h" #include  #include  // for toupper() #include  #include  #include  using namespace std; using namespace cs_mystring; bool eof(istream& in); void BasicTest(); void RelationTest(); void ConcatTest(); void CopyTest(); MyString AppendTest(const MyString& ref, MyString val); string boolString(bool convertMe); int main() { BasicTest(); RelationTest(); ConcatTest(); CopyTest(); } bool eof(istream& in) { char ch; in >> ch; in.putback(ch); return !in; } string boolString(bool convertMe) { if (convertMe) { return "true"; } else { return "false"; } } void BasicTest() { MyString s; cout << "----- Testing basic String creation & printing" << endl; const MyString strs[] = {MyString("Wow"), MyString("C++ is neat!"), MyString(""), MyString("a-z")}; for (int i = 0; i < 4; i++){ cout << "string [" << i <<"] = " << strs[i] << endl; } cout << endl << "----- Now reading MyStrings from file" << endl; cout << endl << "----- first, word by word" << endl; ifstream in("mystring.txt"); assert(in); while (in.peek() == '#'){ in.ignore(128, ' '); } in >> s; while (in) { cout << "Read string = " << s << endl; in >> s; } in.close(); cout << endl << "----- now, line by line" << endl; ifstream in2("mystring.txt"); assert(in2); while (in2.peek() == '#'){ in2.ignore(128, ' '); } s.read(in2, ' '); while (in2) { cout << "Read string = " << s << endl; s.read(in2, ' '); } cout << endl << "----- Testing access to characters (using const)" << endl; const MyString s1("abcdefghijklmnopqsrtuvwxyz"); cout << "Whole string is " << s1 << endl; cout << "now char by char: "; for (int i = 0; i < s1.length(); i++){ cout << s1[i]; } cout << endl << "----- Testing access to characters (using non-const)" << endl; MyString s2("abcdefghijklmnopqsrtuvwxyz"); cout << "Start with " << s2; for (int i = 0; i < s2.length(); i++){ s2[i] = toupper(s2[i]); } cout << " and convert to " << s2 << endl; } void RelationTest() { cout << " ----- Testing relational operators between MyStrings "; const MyString strs[] = {MyString("app"), MyString("apple"), MyString(""), MyString("Banana"), MyString("Banana")}; for (int i = 0; i < 4; i++) { cout << "Comparing " << strs[i] << " to " << strs[i+1] << endl; cout << "\tIs left < right? " << boolString(strs[i] < strs[i+1]) << endl; cout << "\tIs left <= right? " << boolString(strs[i] <= strs[i+1]) << endl; cout << "\tIs left > right? " << boolString(strs[i] > strs[i+1]) << endl; cout << "\tIs left >= right? " << boolString(strs[i] >= strs[i+1]) << endl; cout << "\tDoes left == right? " << boolString(strs[i] == strs[i+1]) << endl; cout << "\tDoes left != right ? " << boolString(strs[i] != strs[i+1]) << endl; } cout << " ----- Testing relations between MyStrings and char * "; MyString s("he"); const char *t = "hello"; cout << "Comparing " << s << " to " << t << endl; cout << "\tIs left < right? " << boolString(s < t) << endl; cout << "\tIs left <= right? " << boolString(s <= t) << endl; cout << "\tIs left > right? " << boolString(s > t) << endl; cout << "\tIs left >= right? " << boolString(s >= t) << endl; cout << "\tDoes left == right? " << boolString(s == t) << endl; cout << "\tDoes left != right ? " << boolString(s != t) << endl; MyString u("wackity"); const char *v = "why"; cout << "Comparing " << v << " to " << u << endl; cout << "\tIs left < right? " << boolString(v < u) << endl; cout << "\tIs left <= right? " << boolString(v <= u) << endl; cout << "\tIs left > right? " << boolString(v > u) << endl; cout << "\tIs left >= right? " << boolString(v >= u) << endl; cout << "\tDoes left == right? " << boolString(v == u) << endl; cout << "\tDoes left != right ? " << boolString(v != u) << endl; } void ConcatTest() { cout << " ----- Testing concatentation on MyStrings "; const MyString s[] = {MyString("outrageous"), MyString("milk"), MyString(""), MyString("cow"), MyString("bell")}; for (int i = 0; i < 4; i++) { cout << s[i] << " + " << s[i+1] << " = " << s[i] + s[i+1] << endl; } cout << " ----- Testing concatentation between MyString and char * "; const MyString a("abcde"); const char *b = "XYZ"; cout << a << " + " << b << " = " << a + b << endl; cout << b << " + " << a << " = " << b + a << endl; cout << " ----- Testing shorthand concat/assign on MyStrings "; MyString s2[] = {MyString("who"), MyString("what"), MyString("WHEN"), MyString("Where"), MyString("why")}; for (int i = 0; i < 4; i++) { cout << s2[i] << " += " << s2[i+1] << " = "; cout << (s2[i] += s2[i+1]) << "and"; cout << s2[i] << endl; } cout << " ----- Testing shorthand concat/assign using char * "; MyString u("I love "); const char *v = "programming"; cout << u << " += " << v << " = "; cout << (u += v) << endl; } MyString AppendTest(const MyString& ref, MyString val) { val[0] = 'B'; return val + ref; } void CopyTest() { cout << " ----- Testing copy constructor and operator= on MyStrings "; MyString orig("cake"); MyString copy(orig); // invoke copy constructor copy[0] = 'f'; // change first letter of the *copy* cout << "original is " << orig << ", copy is " << copy << endl; MyString copy2; // makes an empty string copy2 = orig; // invoke operator= copy2[0] = 'f'; // change first letter of the *copy* cout << "original is " << orig << ", copy is " << copy2 << endl; copy2 = "Copy Cat"; copy2 = copy2; // copy onto self and see what happens cout << "after self assignment, copy is " << copy2 << endl; cout << "Testing pass & return MyStrings by value and ref" << endl; MyString val = "winky"; MyString sum = AppendTest("Boo", val); cout << "after calling Append, sum is " << sum << endl; cout << "val is " << val << endl; val = sum; cout << "after assign, val is " << val << endl; } 

Data File:

# This file has some strings that are used in the string test to check # reading strings from files. The default overloaded >> of your string # class should skip over any leading spaces and read characters into # the string object, stopping at the first whitespace character (this is # similar to the behavior of >> on char *). The read method of the # string class is a little fancier. It allows client to restrict # how many characters at max to read and what character to use as # delimiter, so you can stop at newline instead of space, for example. # Reading consumes the delimiting character, so the next read starts # after that. # The first time we will read individual words, next we read whole lines 

Correct Output:

----- Testing basic String creation & printing string [0] = Wow string [1] = C++ is neat! string [2] = string [3] = a-z ----- Now reading myStrings from file ----- first, word by word Read string = The Read string = first Read string = time Read string = we Read string = will Read string = read Read string = individual Read string = words, Read string = next Read string = we Read string = read Read string = whole Read string = lines ----- now, line by line Read string = The first time we will Read string = read individual words, next Read string = we read whole lines ----- Testing access to characters (using const) Whole string is abcdefghijklmnopqsrtuvwxyz now char by char: abcdefghijklmnopqsrtuvwxyz ----- Testing access to characters (using non-const) Start with abcdefghijklmnopqsrtuvwxyz and convert to ABCDEFGHIJKLMNOPQSRTUVWXYZ ----- Testing relational operators between myStrings Comparing app to apple Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true Comparing apple to Is left < right? false Is left <= right? false Is left > right? true Is left >= right? true Does left == right? false Does left != right ? true Comparing to Banana Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true Comparing Banana to Banana Is left < right? false Is left <= right? true Is left > right? false Is left >= right? true Does left == right? true Does left != right ? false ----- Testing relations between myStrings and char * Comparing he to hello Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true Comparing why to wackity Is left < right? false Is left <= right? false Is left > right? true Is left >= right? true Does left == right? false Does left != right ? true ----- Testing concatentation on myStrings outrageous + milk = outrageousmilk milk + = milk + cow = cow cow + bell = cowbell ----- Testing concatentation between myString and char * abcde + XYZ = abcdeXYZ XYZ + abcde = XYZabcde ----- Testing shorthand concat/assign on myStrings who += what = whowhatandwhowhat what += WHEN = whatWHENandwhatWHEN WHEN += Where = WHENWhereandWHENWhere Where += why = WherewhyandWherewhy ----- Testing shorthand concat/assign using char * I love += programming = I love programming ----- Testing copy constructor and operator= on myStrings original is cake, copy is fake original is cake, copy is fake after self assignment, copy is Copy Cat Testing pass & return myStrings by value and ref after calling Append, sum is BinkyBoo val is winky after assign, val is BinkyBoo 

//mystring.h file to edit

//myString.h

#include

namespace cs_mystring {

class myString {

char* m_str;

public:

myString(const myString& src);

myString(const char* src);

myString();

~myString();

const char* c_str() const;

int length() const;

myString& operator = (const myString& src);

char operator [] (int index) const;

char& operator [] (int index);

bool operator < (const myString& src) const;

bool operator <= (const myString& src) const;

bool operator > (const myString& src) const;

bool operator >= (const myString& src) const;

bool operator == (const myString& src) const;

bool operator != (const myString& src) const;

bool myString::operator < (const char* src) const;

bool myString::operator <= (const char* src) const;

bool myString::operator > (const char* src) const;

bool myString::operator >= (const char* src) const;

bool myString::operator == (const char* src) const;

bool myString::operator != (const char* src) const;

void read(std::ifstream& in, char endOfCharh);

myString operator+(const myString &rhs)const;

myString operator+(const char *rhs)const;

myString& operator+=(const myString &rhs);

myString& operator+=(const char *rhs);

};

bool operator < (const char* s1, const myString& s2);

bool operator <= (const char* s1, const myString& s2);

bool operator > (const char* s1, const myString& s2);

bool operator >= (const char* s1, const myString& s2);

bool operator == (const char* s1, const myString& s2);

bool operator != (const char* s1, const myString& s2);

std::ostream& operator << (std::ostream& strm, const myString& str);

std::ifstream& operator >> (std::ifstream& in, myString& str);

char* operator+(const char* lhs, const myString &rhs);

}

//mystring.cpp to edit:

//myString.cpp

#define _CRT_SECURE_NO_WARNINGS

#include "myString.h"

#include

#include

#include

#include

using namespace std;

using namespace cs_mystring;

myString::myString(const myString& src)

: m_str(new char[src.length() + 1])

{

strcpy(m_str, src.c_str());

}

myString::myString(const char* src)

{

if (src)

{

m_str = new char[strlen(src) + 1];

strcpy(m_str, src);

}

else

{

m_str = 0;

}

}

myString::myString()

: M_str (0)

{

}

myString::~myString()

{

delete[] m_str;

}

const char* myString::c_str() const

{

return m_str;

}

int myString::length() const

{

if (m_str == 0) {

return 0;

}

return (int)strlen(m_str);

}

myString& myString::operator = (const myString& src)

{

if (src != *this)

{

delete m_str;

m_str = new char[src.length() + 1];

strcpy(m_str, src.c_str());

}

return *this;

}

char myString::operator [] (int index) const

{

assert(0 <= index && index < length());

return m_str[index];

}

char& myString::operator [] (int index)

{

assert(0 <= index && index < length());

return m_str[index];

}

bool myString::operator < (const myString& src) const { return strcmp(m_str, src.c_str()) < 0; }

bool myString::operator <= (const myString& src) const { return strcmp(m_str, src.c_str()) <= 0; }

bool myString::operator > (const myString& src) const { return strcmp(m_str, src.c_str()) > 0; }

bool myString::operator >= (const myString& src) const { return strcmp(m_str, src.c_str()) >= 0; }

bool myString::operator == (const myString& src) const

{

if (!m_str || !src.c_str()) {

return false;

}

return strcmp(m_str, src.c_str()) == 0;

}

bool myString::operator != (const myString& src) const

{

return !(*this==src);

}

bool myString::operator < (const char* src) const { return strcmp(m_str, src) < 0; }

bool myString::operator <= (const char* src) const { return strcmp(m_str, src) <= 0; }

bool myString::operator > (const char* src) const { return strcmp(m_str, src) > 0; }

bool myString::operator >= (const char* src) const { return strcmp(m_str, src) >= 0; }

bool myString::operator == (const char* src) const { return strcmp(m_str, src) == 0; }

bool myString::operator != (const char* src) const { return strcmp(m_str, src) != 0; }

bool cs_mystring::operator < (const char* s1, const cs_mystring::myString& s2) { return strcmp(s1, s2.c_str()) < 0; }

bool cs_mystring::operator <= (const char* s1, const cs_mystring::myString& s2) { return strcmp(s1, s2.c_str()) <= 0; }

bool cs_mystring::operator > (const char* s1, const cs_mystring::myString& s2) { return strcmp(s1, s2.c_str()) > 0; }

bool cs_mystring::operator >= (const char* s1, const cs_mystring::myString& s2) { return strcmp(s1, s2.c_str()) >= 0; }

bool cs_mystring::operator == (const char* s1, const cs_mystring::myString& s2) { return strcmp(s1, s2.c_str()) == 0; }

bool cs_mystring::operator != (const char* s1, const cs_mystring::myString& s2) { return strcmp(s1, s2.c_str()) != 0; }

std::ostream& cs_mystring::operator << (std::ostream& strm, const cs_mystring::myString& str)

{

return strm.write (str.c_str () str.length ());

}

ifstream& cs_mystring::operator >> (std::ifstream& in, myString& str) {

str.read(in, ' ');

return in;

}

void myString::read(std::ifstream& in, char ch) {

char readStr[127];

while (ch != ' ' && (in.peek() == ' ' || in.peek() == ' ')) {

in.ignore(1, ' ');

}

in.getline(readStr, 127, ch);

if (m_str != NULL) {

delete[]m_str;

}

int len = strlen(readStr);

if (readStr[len - 1] == ' ') {

readStr[len - 1] = 0;

}

m_str = new char[len+1];

strcpy(m_str, readStr);

}

myString myString::operator+(const myString &ref)const

{

int size = this->length() + ref.length();

char* str = new char[size + 1];

strcpy(str, c_str());

strcat(str, ref.c_str());

return myString(str);

}

myString myString::operator+(const char *s)const

{

int size = this->length() + strlen(s);

char* str = new char[size + 1];

strcpy(str, c_str());

strcat(str, s);

return myString(str);

}

char* cs_mystring::operator+(const char* s, const myString &ref)

{

int size = strlen(s) + ref.length();

char* str = new char[size + 1];

strcpy(str, s);

strcat(str, ref.c_str());

return str;

}

myString& myString::operator+=(const myString &ref)

{

int size = length() + ref.length();

char* str = new char[size + 1];

strcpy(str, m_str);

strcat(str, ref.c_str());

delete[] m_str;

m_str = new char[size + 1];

strcpy(m_str, str);

delete[] str;

return *this;

}

myString& myString::operator+=(const char *ref)

{

int size = length() + strlen(ref);

char* str = new char[size + 1];

strcpy(str, m_str);

strcat(str, ref);

delete[] m_str;

m_str = new char[size + 1];

strcpy(m_str, str);

delete[] str;

return *this; }

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

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions

Question

Define organisational structure

Answered: 1 week ago

Question

Define line and staff authority

Answered: 1 week ago

Question

Define the process of communication

Answered: 1 week ago

Question

Explain the importance of effective communication

Answered: 1 week ago

Question

* What is the importance of soil testing in civil engineering?

Answered: 1 week ago