Hi. I need help in this c++ assignement. I also provided the code Which needs re
ID: 3694756 • Letter: H
Question
Hi. I need help in this c++ assignement. I also provided the code Which needs refinements made to below along with the correct output needed.Thanks!
Code to be refined:
//main.cpp
#include "mystring.h"
#include <cctype> // for toupper()
#include <iostream>
#include <string>
using namespace std;
using namespace cs_mystring;
void BasicTest();
void RelationTest();
void CopyTest();
myString AppendTest(const myString& ref, myString val);
string boolString(bool convertMe);
int main()
{
BasicTest();
RelationTest();
CopyTest();
}
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 << "----- 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;
}
string boolString(bool convertMe) {
if (convertMe) {
return "true";
}
else {
return "false";
}
}
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;
}
myString AppendTest(const myString& ref, myString val)
{
val[0] = 'B';
return val;
}
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;
}
//mystring.cpp
#include "myString.h"
#include <cstring>
using namespace cs_mystring;
myString::myString() {
str = new char;
str[0] = '';
}
myString::myString(const char *s) {
int len = strlen(s);
str = new char[len + 1];
for (int i = 0; i < len; ++i) {
str[i] = s[i];
}
str[len] = '';
}
myString::myString(const myString &s) {
int len = strlen(s.str);
str = new char[len + 1];
for (int i = 0; i < len; ++i) {
str[i] = s.str[i];
}
str[len] = '';
}
void myString::operator = (myString &s) {
s.str = new char[length() + 1];
for (int i = 0; i < length(); ++i) {
s.str[i] = str[i];
}
s.str[length()] = '';
}
void myString::operator = (const char *cArr) {
int len = strlen(cArr);
str = new char[len + 1];
for (int i = 0; i < len; ++i) {
str[i] = cArr[i];
}
str[len] = '';
}
int myString::length() const {
return strlen(str);
}
ostream &cs_mystring::operator <<(ostream &out, const myString &s) {
out << s.str;
return out;
}
istream &cs_mystring::operator >> (istream &in, myString &s) {
in >> s.str;
return in;
}
bool myString::operator < (const myString &s) const {
return strcmp(str, s.str) < 0;
}
bool myString::operator <= (const myString &s) const {
return strcmp(str, s.str) <= 0;
}
bool myString::operator > (const myString &s) const {
return strcmp(str, s.str) > 0;
}
bool myString::operator >= (const myString &s) const {
return strcmp(str, s.str) >= 0;
}
bool myString::operator == (const myString &s) const {
return strcmp(str, s.str) == 0;
}
bool myString::operator != (const myString &s) const {
return strcmp(str, s.str) != 0;
}
bool cs_mystring::operator <(const char *cArr, const myString &s) {
return strcmp(cArr, s.str) < 0;
}
bool cs_mystring::operator <=(const char *cArr, const myString &s) {
return strcmp(cArr, s.str) <= 0;
}
bool cs_mystring::operator >(const char *cArr, const myString &s) {
return strcmp(cArr, s.str) > 0;
}
bool cs_mystring::operator >=(const char *cArr, const myString &s) {
return strcmp(cArr, s.str) >= 0;
}
bool cs_mystring::operator ==(const char *cArr, const myString &s) {
return strcmp(cArr, s.str) == 0;
}
bool cs_mystring::operator !=(const char *cArr, const myString &s) {
return strcmp(cArr, s.str) != 0;
}
char &myString::operator [](int i) const {
return str[i];
}
//Mystring.h
#include <iostream>
using namespace std;
namespace cs_mystring {
#ifndef _MYSTRING_H_
#define _MYSTRING_H_
class myString {
private:
char *str;
public:
myString();
myString(const char *s);
myString(const myString &s);
void operator = (myString &s);
void operator = (const char *cArr);
int length() const;
friend ostream &operator <<(ostream &out, const myString &s);
friend istream &operator >> (istream &out, myString &s);
bool operator <(const myString &s) const;
bool operator <=(const myString &s) const;
bool operator >(const myString &s) const;
bool operator >=(const myString &s) const;
bool operator ==(const myString &s) const;
bool operator !=(const myString &s) const;
friend bool operator <(const char *cArr, const myString &s);
friend bool operator <=(const char *cArr, const myString &s);
friend bool operator >(const char *cArr, const myString &s);
friend bool operator >=(const char *cArr, const myString &s);
friend bool operator ==(const char *cArr, const myString &s);
friend bool operator !=(const char *cArr, const myString &s);
char &operator [](int i) const;
};
#endif
}
Correct output:
data file:
Assignment 14.1 [45 points] See also client program, data file, and correct output. This week you'll be making the following refinements to the class that you wrote in the last assignment. All the requirements fronm that class still apply. For example, all myStrings must always be stored in a dynamic array that is exactly the correct size to store the string Extraction Operator [10 points] ust 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 strcpy0 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. A read) function [10 points] 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 Concatenation Operator [10 points] 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.Explanation / Answer
main.cpp
#include "mystring.h"
#include <cctype> // for toupper()
#include <iostream>
#include <string>
using namespace std;
using namespace cs_mystring;
void BasicTest();
void RelationTest();
void CopyTest();
myString AppendTest(const myString& ref, myString val);
string boolString(bool convertMe);
int main()
{
BasicTest();
RelationTest();
CopyTest();
}
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 << "----- 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;
}
string boolString(bool convertMe) {
if (convertMe) {
return "true";
} else {
return "false";
}
}
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;
}
myString AppendTest(const myString& ref, myString val)
{
val[0] = 'B';
return val;
}
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;
}
myString.cpp
#include <iostream>
#include <cassert>
#include "mystring.h"
using namespace std;
namespace cs_mystring
{
myString::myString()
{
string = new char[1];
strcpy(string, "");
}
myString::myString(const char *inString)
{
string = new char[strlen(inString) + 1];
strcpy(string, inString);
}
myString::myString(const myString& right)
{
string = new char[strlen(right.string) + 1];
strcpy(string, right.string);
}
myString::~myString()
{
delete [] string;
}
myString myString::operator=(const myString &right)
{
if (this != &right){
delete [] string;
string = new char[strlen(right.string) + 1];
strcpy(string, right.string);
}
return *this;
}
ostream& operator<<(ostream& out, const myString& right)
{
out << right.string;
return out;
}
istream& operator>>(istream& in, myString& right)
{
while (isspace(in.peek())){
in.ignore();
}
char temp[128];
in.getline(temp, 127);
delete [] right.string;
right.string = new char[strlen(temp) + 1];
strcpy(right.string, temp);
return in;
}
// []overload (value)
char myString::operator[](int index) const
{
assert(index >= 0 && index < strlen(string));
return string[index];
}
// []overload (reference)
char& myString::operator[](int index)
{
assert(index >= 0 && index < strlen(string));
return string[index];
}
bool operator<(const myString& left,
const myString& right)
{
if(strcmp(left.string, right.string) < 0)
{
return true;
} else {
return false;
}
}
bool operator<=(const myString& left,
const myString& right)
{
if(strcmp(left.string, right.string) < 0 || strcmp(left.string, right.string) == 0)
{
return true;
} else
return false;
}
bool operator>(const myString& left,
const myString& right)
{
if(strcmp(left.string, right.string) > 0)
{
return true;
} else {
return false;
}
}
bool operator>=(const myString& left,
const myString& right)
{
if(strcmp(left.string, right.string) > 0 || strcmp(left.string, right.string) == 0)
{
return true;
} else
return false;
}
bool operator!=(const myString& left,
const myString& right)
{
if(strcmp(left.string, right.string) == 0)
{
return false;
} else {
return true;
}
}
bool operator==(const myString& left,
const myString& right)
{
if(strcmp(left.string, right.string) == 0)
{
return true;
} else {
return false;
}
}
int myString::length() const
{
return strlen(string);
}
}
mystring.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
using namespace std;
namespace cs_mystring
{
class myString
{
public:
myString();
myString(const char *inString); // default constructor
myString(const myString& right); // copy constructor
~myString(); // destructor
myString operator=(const myString& right); // assignment opperator
friend ostream& operator<<(ostream& out, const myString& right);
friend istream& operator>>(istream& in, myString& right);
char operator[](int index) const; // []overload (value)
char& operator[](int index); // []overload (reference)
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);
int length() const;
private:
char *string;
};
}
#endif
SAMPLE OUTPUT
----- Testing basic String creation & printing
string [0] = Wow
string [1] = C++ is neat!
string [2] =
string [3] = a-z
----- 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 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 Binky
val is winky
after assign, val is Binky
Program ended with exit code: 0