Please help with this C++ code ASAP. The instructions and given data is listed b
ID: 3695635 • Letter: P
Question
Please help with this C++ code ASAP. The instructions and given data is listed below. THANK YOU!!!
Given Main.cpp/Client Program (StringTest.cpp) -
Given Data File -
requirements from that class still apply. For example, all myStrings must always be stored in a dynamic array that is exactly the correct size to stare the string. Extraction Operator [10 points] Just like the >>operator that reads C-strings, your >> aperator 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 nced into your data member, which will be a dynamic array. Note that this does not mean that al 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 strepy() to capy 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. A read) function [10 points] The read() function will allow the client programmer to speoify 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 Concatenation Operator [10 points] Overload the +operator to do myString concatenation. The operator must be able to handie 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 strepyoto 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 Combined Concatenation/Assignment Operator [S) Overload the shorthand +to combine concatenation and assignment. Only myStrings can be on the left-hand side of +-operation, but either myStrings or C-strings may appear on the right side. If you pay close attention to the +- operator from the feetInches elass, this may be the easiest 15 points of the semester Add Documentation [10 points]Explanation / Answer
mystring.cpp
#include <iostream>
#include <cassert>
#include "mystring.h"
using namespace std;
namespace cs2b_mystring {
myString::myString() {
myCString = new char[1];
strcpy(myCString, "");
}
myString::myString(const char *inMyCString) {
myCString = new char[strlen(inMyCString)+1];
strcpy(myCString, inMyCString);
}
myString::myString(const myString& right) {
myCString = new char[strlen(right.myCString)+1];
strcpy(myCString, right.myCString);
}
myString::~myString() {
delete [] myCString;
}
myString myString::operator= (const myString& right) {
if (this != &right) {
delete [] myCString;
myCString = new char[strlen(right.myCString)+1];
strcpy(myCString, right.myCString);
}
return *this;
}
ostream& operator<< (ostream& out, const myString& right) {
out << right.myCString;
return out;
}
istream& operator>> (istream& in, myString& right) {
char inBufferHolder[128];
in.width(128);
in >> inBufferHolder;
delete [] right.myCString;
right.myCString = new char [strlen(inBufferHolder)+1];
strcpy(right.myCString, inBufferHolder);
return in;
}
void myString::read(istream& in, char delim) {
char inBufferHolder[128];
in.getline(inBufferHolder, 128, delim);
delete [] myCString;
myCString = new char [strlen(inBufferHolder)];
strcpy(myCString, inBufferHolder);
}
long myString::length() const {
return (strlen(myCString));
}
bool operator< (const myString& left, const myString& right) {
return (strcmp(left.myCString,right.myCString) < 0);
}
bool operator<= (const myString& left, const myString& right) {
return (strcmp(left.myCString,right.myCString) <= 0);
}
bool operator> (const myString& left, const myString& right) {
return (strcmp(left.myCString,right.myCString) > 0);
}
bool operator>= (const myString& left, const myString& right) {
return (strcmp(left.myCString,right.myCString) >= 0);
}
bool operator== (const myString& left, const myString& right) {
return (strcmp(left.myCString,right.myCString) == 0);
}
bool operator!= (const myString& left, const myString& right) {
return (strcmp(left.myCString,right.myCString) != 0);
}
char myString::operator[](int index) const {
assert(index >= 0 && index < strlen(myCString));
return myCString[index];
}
char& myString::operator[](int index) {
assert(index >= 0 && index < strlen(myCString));
return myCString[index];
}
myString operator+ (const myString& left, const myString& right) {
char *Cstring = new char [strlen(left.myCString) + strlen(right.myCString) + 1];
myString result = myString(Cstring);
strcpy(result.myCString, left.myCString);
strcat(result.myCString, right.myCString);
return result;
}
myString myString::operator+= (const myString& right) {
*this = *this+right;
return *this;
}
} // namespace end
mystring.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
namespace cs2b_mystring {
class myString {
public:
myString();
myString(const char *inMyCString);
myString(const myString& right);
~myString();
myString operator= (const myString& right);
friend std::ostream& operator<< (std::ostream& out, const myString& right);
friend std::istream& operator>> (std::istream& in, myString& right);
void read(std::istream& in, char delim);
long length() const;
friend bool operator< (const myString& left, const myString& right);
friend bool operator<= (const myString& left, const myString& right);
friend bool operator> (const myString& left, const myString& right);
friend bool operator>= (const myString& left, const myString& right);
friend bool operator== (const myString& left, const myString& right);
friend bool operator!= (const myString& left, const myString& right);
char operator[](int index) const;
char& operator[](int index);
friend myString operator+ (const myString& left, const myString& right);
myString operator+= (const myString& right);
private:
char *myCString;
}; // class end
} // namespace end
#endif
main.cpp
#include "mystring.h"
#include <fstream>
#include <cctype> // for toupper()
#include <string>
#include <cassert>
#include <iostream>
using namespace std;
using namespace cs2b_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 << " Is left < right? " << boolString(strs[i] < strs[i+1]) << endl;
cout << " Is left <= right? " << boolString(strs[i] <= strs[i+1]) << endl;
cout << " Is left > right? " << boolString(strs[i] > strs[i+1]) << endl;
cout << " Is left >= right? " << boolString(strs[i] >= strs[i+1]) << endl;
cout << " Does left == right? " << boolString(strs[i] == strs[i+1]) << endl;
cout << " Does 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 << " Is left < right? " << boolString(s < t) << endl;
cout << " Is left <= right? " << boolString(s <= t) << endl;
cout << " Is left > right? " << boolString(s > t) << endl;
cout << " Is left >= right? " << boolString(s >= t) << endl;
cout << " Does left == right? " << boolString(s == t) << endl;
cout << " Does left != right ? " << boolString(s != t) << endl;
myString u("wackity");
const char *v = "why";
cout << "Comparing " << v << " to " << u << endl;
cout << " Is left < right? " << boolString(v < u) << endl;
cout << " Is left <= right? " << boolString(v <= u) << endl;
cout << " Is left > right? " << boolString(v > u) << endl;
cout << " Is left >= right? " << boolString(v >= u) << endl;
cout << " Does left == right? " << boolString(v == u) << endl;
cout << " Does 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;
}
sample 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