Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help cleaning my c++ code for mystring.cpp. I was told my boolean function could be simplified to basically 1 line of strcmp function,

I need help cleaning my c++ code for mystring.cpp. I was told my boolean function could be simplified to basically 1 line of strcmp function, but I'm not sure how to do that. Also I need help with my mystring::replace function simplification. Thank you!

=====================================================================

#include "mystring.h"

// default constructor mystring::mystring() { ptr_buffer = new char[1]; *ptr_buffer = '\0'; buf_size = 1; len = 0; }

// constructor from c-style string mystring::mystring(const char * s) { len = strlen(s); buf_size = len + 1; ptr_buffer = new char[buf_size]; strcpy(ptr_buffer, s);

}

// copy constructor mystring::mystring(const mystring& orig) { len = orig.length(); ptr_buffer = new char[len+1]; buf_size = len+1; strcpy(ptr_buffer, orig.ptr_buffer); }

//Overload operators mystring objects can be manipulated as 1st class objects. mystring& mystring::operator=(const mystring& orig) { delete [] ptr_buffer; len = orig.length(); ptr_buffer = new char[len+1]; buf_size = len+1; strcpy(ptr_buffer, orig.ptr_buffer); }

mystring& mystring::operator=(const char* orig) { delete [] ptr_buffer; len = strlen(orig); buf_size = len + 1; ptr_buffer = new char[buf_size]; strcpy(ptr_buffer, orig); }

char mystring::operator[](size_type pos) const { return ptr_buffer[pos]; }

char& mystring::operator[](size_type pos) { return ptr_buffer[pos]; }

mystring& mystring::operator+=(const mystring& str) { this->append(str); return *this; }

mystring& mystring::operator+=(const char* str) { this->append(str); return *this; }

//Methods for modifying mystring objects with other mystring objects and c-style strings. void mystring::clear() { len = 0; ptr_buffer[0] = '\0'; }

void mystring::push_back(char c) { reserve(len + 1); ptr_buffer[len] = c; ptr_buffer[len + 1] = '\0'; len++; }

mystring& mystring::append(const mystring& str) { return append(str.c_str()); }

mystring& mystring::append(const char* str) { int size = strlen(str); reserve(size + len); strcat(ptr_buffer, str); len += size; return *this; }

mystring& mystring::insert(size_type pos, const mystring& str) { return insert(pos, str.c_str()); }

mystring& mystring::insert(size_type pos, const char* str) { int size = strlen(str); reserve(len + size); mystring b(ptr_buffer + pos); for (int i = 0; i <= size; ++i) { ptr_buffer[i + len] = str[i]; } len = (pos + size); append(b); return *this; }

mystring& mystring::replace(size_type start, size_type span, const mystring& str) { char* temp = new char[buf_size]; for(int c = 0; c < buf_size; c++) { if(c >= start && (c-start) < span && str[c-start] != '\0') temp[c] = str[c-start]; if(c < start || c >= (start + span)) temp[c] = ptr_buffer[c]; } temp[buf_size-1] = '\0'; delete [] ptr_buffer; ptr_buffer = temp; temp = NULL; }

mystring& mystring::replace(size_type start, size_type span, const char* str) { char* temp = new char[buf_size]; for(int c = 0; c < buf_size; c++) { if(c >= start && (c-start) < span && str[c-start] != '\0') temp[c] = str[c-start]; if(c < start || c >= (start + span)) temp[c] = ptr_buffer[c]; } temp[buf_size-1] = '\0'; delete [] ptr_buffer; ptr_buffer = temp; temp = NULL; }

void mystring::reserve(size_type n) { if(n < (len + 1)) return; buf_size = n; len = buf_size - 1; char* temp = new char[buf_size]; strcpy(temp, ptr_buffer); delete [] ptr_buffer; ptr_buffer = temp; temp = NULL; }

//Methods to view properties of mystring objects. mystring::size_type mystring::size() const { return len; } mystring::size_type mystring::length() const { return len; } mystring::size_type mystring::capacity() const { return buf_size; } mystring::size_type mystring::max_size() const { return (int)pow(2,30) -4 ; }

bool mystring::empty() const { if(len == 0) return true; else return false; }

//Destructor mystring::~mystring() { delete [] ptr_buffer; }

//Boolean operators to compare mystring objectss and c-style strings. // I need to clean this up with basically one strcmp function bool operator==(const mystring& lhs, const mystring& rhs) { if(lhs.length() != rhs.length()) return false; int i = 0; while(lhs[i] != '\0') { if(lhs[i] != rhs[i]) return false; i++; } return true; }

bool operator==(const char* lhs, const mystring& rhs) { if(strlen(lhs) != rhs.length()) return false; int r = 0; while(lhs[r] != '\0') { if(lhs[r] != rhs[r]) return false; r++; } return true; }

bool operator==(const mystring& lhs, const char* rhs) { if(lhs.length() != strlen(rhs)) return false; int r = 0; while(lhs[r] != '\0') { if(lhs[r] != rhs[r]) return false; r++; } return true; }

bool operator!=(const mystring& lhs, const mystring& rhs) { if(lhs.length() != rhs.length()) return true; int i = 0; while(lhs[i] != '\0') { if(lhs[i] != rhs[i]) return true; i++; } return false; }

bool operator!=(const char* lhs, const mystring& rhs) { if(strlen(lhs) != rhs.length()) return true; int r = 0; while(lhs[r] != '\0') { if(lhs[r] != rhs[r]) return true; r++; } return false; }

bool operator!=(const mystring& lhs, const char* rhs) { if(lhs.length() != strlen(rhs)) return true; int r = 0; while(lhs[r] != '\0') { if(lhs[r] != rhs[r]) return true; r++; } return false; }

mystring operator+(const mystring& lhs, const mystring& rhs) { mystring concat = mystring(lhs); concat.append(rhs); return concat; }

//Overload output operator to simplify displaying mystring objects. ostream& operator<<(ostream& out, const mystring& str) { out << str.c_str(); return out; }

//Method that returns c-style string of mystring object. const char * mystring::c_str() const { return ptr_buffer; }

mystring::iterator mystring::begin() { return ptr_buffer; }

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_2

Step: 3

blur-text-image_3

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

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions

Question

What is organizational culture and how can leaders influence it?

Answered: 1 week ago