待优化部分:1利用string中的函数,2利用辅助函数减少代码量(代码重用没有,重复挺多的)
3,添加模板和异常,4利用转换函数减少重载
#ifndef MYSTRING_H_
#define MYSTRING_H_
#include<iostream>
#include<cstring>
#include<new>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::istream;
namespace MYSTRING
{
class Mystring
{
public:
Mystring();
explicit Mystring(unsigned);
explicit Mystring(const char*);
Mystring(const Mystring*);
Mystring(const Mystring&);
~Mystring();
const Mystring operator+(const Mystring&)const;
const Mystring operator+(const char*)const;
char& operator[](const unsigned);
char& at(const unsigned);
friend ostream& operator<<(ostream&, const Mystring&);
friend istream& operator>>(istream&, Mystring&);
const bool operator==(const Mystring&)const;
friend const bool operator==(const char*, const Mystring&);
const bool operator!=(const Mystring&)const;
friend const bool operator!=(const char*, const Mystring&);
Mystring& operator=(const Mystring*);
Mystring& operator=(const Mystring&);
Mystring& operator=(const char*);
Mystring& operator+=(const Mystring&);
explicit operator char*()const;
void resize(const unsigned);
private:
char* m_baseP;
mutable int m_size;//有效字符数目
};
}
#endif // !MYSTRING_H_
namespace MYSTRING
{
//构造
MYSTRING::Mystring::Mystring()
{
this->m_size = 0;
this->m_baseP = new char[this->m_size+1]{0};
}
//自定义大小
MYSTRING::Mystring::Mystring(unsigned n)
{
if (n <= 0)
exit(EXIT_FAILURE);
this->m_size = n ;
this->m_baseP = new char[this->m_size+1] {0};
}
MYSTRING::Mystring::Mystring(const Mystring* str)
{
this->m_size = str->m_size;
this->m_baseP = new char[this->m_size + 1]{0};
for (int i = 0; i < (this->m_size); ++i)
(this->m_baseP)[i] = (str->m_baseP)[i];
}
MYSTRING::Mystring::Mystring(const Mystring& str)
{
this->m_size = str.m_size;
this->m_baseP = new char[this->m_size + 1]{0};
for (int i = 0; i < (this->m_size); ++i)
(this->m_baseP)[i] = (str.m_baseP)[i];
}
//自定义大小
void MYSTRING::Mystring::resize(const unsigned n)
{
if (n <= 0)
exit(EXIT_FAILURE);
Mystring temp (*this);
delete[]this->m_baseP;
this->m_size = n;
this->m_baseP = new char[this->m_size + 1] {0};
unsigned size = this->m_size > n ? n : this->m_size;
for (int i = 0; i < size; ++i)
(this->m_baseP)[i] = (temp.m_baseP)[i];
}
MYSTRING::Mystring::Mystring( const char* ch)
{
this->m_size=strlen(ch);
this->m_baseP = new char[this->m_size + 1]{0};
for (int i = 0; i < (this->m_size); ++i)
(this->m_baseP)[i] = ch[i];
}
//析构
MYSTRING::Mystring::~Mystring()
{
delete[]this->m_baseP;
this->m_size = 0;
}
//输入输出
ostream& MYSTRING::operator<<(ostream& mycout, const Mystring& str)
{
mycout << str.m_baseP;
return mycout;
}
istream& MYSTRING::operator>>(istream& mycin, Mystring& str)
{
char ch[21]{ 0 };
delete[]str.m_baseP;
str.m_size = 20;
str.m_baseP = new char[str.m_size + 1] {0};
mycin >> ch;
for (int i = 0; i < (str.m_size ); ++i)
(str.m_baseP)[i] = ch[i];
return mycin;
}
//索引
char& MYSTRING::Mystring::operator[](const unsigned n)
{
if (n < 0 || n >= this->m_size)
exit(EXIT_FAILURE);
return *(this->m_baseP + n);
}
char& MYSTRING::Mystring::at(const unsigned n)
{
return (* this)[n];
}
//赋值
Mystring& MYSTRING::Mystring::operator=(const Mystring* str)
{
delete[] this->m_baseP;
this->m_size = str->m_size;
this->m_baseP = new char[this->m_size + 1]{0};
for (int i = 0; i < (this->m_size ); ++i)
(this->m_baseP)[i] =( str->m_baseP)[i];
return *this;
}
Mystring& MYSTRING::Mystring::operator=(const Mystring& str)
{
delete[] this->m_baseP;
this->m_size = str.m_size;
this->m_baseP = new char[this->m_size + 1]{0};
for (int i = 0; i < (this->m_size); ++i)
(this->m_baseP)[i] = (str.m_baseP)[i];
return *this;
}
Mystring& MYSTRING::Mystring::operator=(const char* ch)
{
delete[]this->m_baseP;
this->m_size = strlen(ch);
this->m_baseP = new char[this->m_size + 1]{0};
for (int i = 0; i < (this->m_size); ++i)
(this->m_baseP)[i] = ch[i];
return *this;
}
Mystring& MYSTRING::Mystring::operator+=(const Mystring& str)
{
*this = *this + str;
return *this;
}
//运算
const Mystring MYSTRING::Mystring::operator+(const Mystring& str) const
{
Mystring temp(this->m_size + str.m_size);
int i;
for (i = 0; i < this->m_size; ++i)
(temp.m_baseP)[i] = (this->m_baseP)[i];
for (int j = 0; j < str.m_size; ++j)
(temp.m_baseP)[i++] = (str.m_baseP)[j];
return temp;
}
const Mystring MYSTRING::Mystring::operator+(const char*ch) const
{
Mystring temp(this->m_size + strlen(ch));
return temp;
}
//条件运算
const bool MYSTRING::Mystring::operator==(const Mystring& str) const
{
if (this->m_size != str.m_size)
return false;
else
{
for (unsigned i = 0; i < this->m_size; ++i)
if ((this->m_baseP)[i] != (str.m_baseP)[i])
return false;
return true;
}
}
const bool MYSTRING::operator==(const char*ch, const Mystring&str)
{
if (strlen(ch) != str.m_size)
return false;
else
{
for (unsigned i = 0; i < (strlen(ch) > str.m_size ? strlen(ch) : str.m_size); ++i)
if ((ch[i]) != (str.m_baseP)[i])
return false;
return true;
}
}
const bool MYSTRING::Mystring::operator!=(const Mystring& str) const
{
if (*this == str)
return false;
else
return true;
}
const bool MYSTRING::operator!=(const char* ch, const Mystring& str)
{
if (ch == str)
return false;
else
return true;
}
//转换函数
MYSTRING::Mystring::operator char* () const
{
char* temp = this->m_baseP;
return temp;
}
}