1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
#include <iostream>
#include <string>
#include <fstream>
#include <utility>
constexpr size_t MAX_INPUT_SIZE {127};
namespace cs_mystring {
class MyString {
public:
MyString();
MyString(const char* inString);
MyString(const MyString& copyMe);
MyString(MyString&& moveMe) noexcept;
~MyString();
MyString& operator+= (const MyString& rightOp);
char operator[](size_t index) const;
char& operator[](size_t index);
MyString& operator=(const MyString right) noexcept;
std::istream& readline(std::istream& in, char delimiter = '\n');
size_t length() const;
friend std::ostream& operator << (std::ostream& out, const MyString& printMe);
friend std::istream& operator >> (std::istream& in, MyString& readMe);
friend MyString operator+(const MyString& leftOp, const MyString& rightOp);
friend bool operator <(const MyString& leftOp, const MyString& rightOp);
friend bool operator <=(const MyString& leftOp, const MyString& rightOp);
friend bool operator >(const MyString& leftOp, const MyString& rightOp);
friend bool operator >=(const MyString& leftOp, const MyString& rightOp);
friend bool operator ==(const MyString& leftOp, const MyString& rightOp);
friend bool operator !=(const MyString& leftOp, const MyString& rightOp);
private:
size_t length(const char* myArray) const;
char* charArray {};
};
}
namespace cs_mystring {
MyString::MyString() : charArray {new char[5]} { }
MyString::MyString(const char* inString) : charArray {new char[strlen(inString) + 1]} {
strcpy(charArray, inString);
}
MyString::MyString(const MyString& copyMe) : MyString(copyMe.charArray) {}
MyString::MyString(MyString&& moveMe) noexcept {
std::swap(moveMe.charArray, charArray);
}
MyString::~MyString() {
delete[] charArray;
}
MyString& MyString::operator+=(const MyString& rightOp) {
return *this;
}
char MyString::operator[](size_t index) const {
return charArray[index];
}
char& MyString::operator[](size_t index) {
return charArray[index];
}
std::ostream& operator<<(std::ostream& out, const MyString& printMe) {
return out << printMe.charArray;
}
std::istream& operator>>(std::istream& in, MyString& newString) {
while (std::isspace(in.peek()))
in.ignore();
char temp[MAX_INPUT_SIZE + 1] {};
size_t cnt {};
for (; cnt < MAX_INPUT_SIZE && !std::isspace(in.peek()) && in.get(temp[cnt]); ++cnt);
temp[cnt] = 0;
newString = MyString(temp);
return in;
}
MyString operator+(const MyString& leftOp, const MyString& rightOp) {
return MyString();
}
bool operator<(const MyString& leftOp, const MyString& rightOp) {
return false;
}
bool operator<=(const MyString& leftOp, const MyString& rightOp) {
return false;
}
bool operator>(const MyString& leftOp, const MyString& rightOp) {
return false;
}
bool operator>=(const MyString& leftOp, const MyString& rightOp) {
return false;
}
bool operator==(const MyString& leftOp, const MyString& rightOp) {
return false;
}
bool operator!=(const MyString& leftOp, const MyString& rightOp) {
return false;
}
MyString& MyString::operator=(MyString right) noexcept {
std::swap(right.charArray, charArray);
return *this;
}
std::istream& MyString::readline(std::istream& in, char delimiter) {
char temp[MAX_INPUT_SIZE + 1] {};
in.getline(temp, MAX_INPUT_SIZE, delimiter);
*this = MyString(temp);
return in;
}
size_t MyString::length() const {
return length(charArray);
}
size_t MyString::length(const char* myArray) const {
auto s {myArray};
for (; *s; ++s);
return s - myArray;
}
}
std::string boolString(bool convertMe) {
if (convertMe) {
return "true";
} else {
return "false";
}
}
void BasicTest() {
using namespace cs_mystring;
std::cout << "***********************************************************************\n";
std::cout << "* Basic Test: Testing various member constructor options and nonmember*\n";
std::cout << "* friend ostream << operator for basic MyString object creation & *\n";
std::cout << "* printing *\n";
std::cout << "***********************************************************************\n";
const MyString strs[] {MyString("Wow"), MyString("C++ is neat!"), MyString(""), MyString("a-z")};
for (size_t i {}; i < 4; ++i)
std::cout << "string [" << i << "] = " << strs[i] << '\n';
std::cout << "\n***********************************************************************\n";
std::cout << "* Basic Test: Testing nonmember friend istream >> and ostream << *\n";
std::cout << "* operators for reading and display of MyString objects from data file*\n";
std::cout << "***********************************************************************\n";
std::cout << "\n----- first, word by word\n";
std::ifstream in("mystring_data.txt");
//assert(in);
while (in.peek() == '#')
in.ignore(128, '\n');
for (MyString s; in >> s; )
std::cout << "Read string = " << s << '\n';
in.close();
std::cout << "\n*************************************************************************\n";
std::cout << "* Basic Test: Testing member function readline read, a nonmember friend *\n";
std::cout << "* istream >> and ostream <<operators for reading and display of MyString*\n";
std::cout << "* objects from data file *\n";
std::cout << "* ***********************************************************************\n";
std::cout << "\n----- now, line by line\n";
std::ifstream in2("mystring_data.txt");
//assert(in2);
while (in2.peek() == '#')
in2.ignore(128, '\n');
for (MyString s; s.readline(in2); )
std::cout << "Read string = " << s << '\n';
in2.close();
std::cout << "\n****************************************************************************\n";
std::cout << "* Basic Test: Testing access to characters (using const and non const) *\n";
std::cout << "* using constructors, member function length that returns a cstring length,*\n";
std::cout << "* the square bracket [] overloaded operator and a nonmember friend *\n";
std::cout << "* ostream << for display of MyString objects content *\n";
std::cout << "* **************************************************************************\n";
std::cout << "\n----- Testing access to characters (using const)\n";
const MyString s1("abcdefghijklmnopqsrtuvwxyz");
const auto stringLength1 {s1.length()};
std::cout << "Whole string is " << s1 << '\n';
std::cout << "now char by char: ";
for (size_t i {}; i < stringLength1; ++i)
std::cout << s1[i];
std::cout << "\n\n----- Testing access to characters (using non-const)\n";
MyString s2("abcdefghijklmnopqsrtuvwxyz");
const auto stringLength2 {s2.length()};
std::cout << "Start with " << s2 << '\n';
for (size_t i {}; i < stringLength2; ++i)
s2[i] = toupper(s2[i]);
std::cout << " and convert to " << s2 << '\n';
}
int main() {
BasicTest();
}
| |