您的位置:首页 > 编程语言 > C语言/C++

C++实现String类

2015-11-22 12:24 489 查看
// MyString.cpp : 定义控制台应用程序的入口点。

//

//-----------2015-11-20--------艾文-------------------

//

/*1、完善MyString类,完成形如:

MyString str("Hello "), str1("World!");

MyString str2 = str + str1;

MyString str3 = str + "temp";

str > str1

str == str1

char chart = str[0];

*/

//----------------------------------------------------------

#include "stdafx.h"

#include <assert.h>

#include<iomanip>

#include<iostream>

using namespace std;

class MyString

{

friend ostream& operator <<(ostream& os, MyString& str); //重载'<<'操作符

friend ostream& operator >>(ostream& os, MyString& str); //重载'>>'操作符

public:

MyString(const char* str=NULL);//默认构造函数

MyString(const MyString &str);//拷贝构造函数

~MyString();
//析构函数

MyString& operator =(const MyString& str);//重载'='操作符,对MyString类的对象进行赋值

MyString operator+(const MyString& str); //重载'+'操作符

bool operator ==(const MyString& str); //重载'=='操作符

bool operator >(const MyString& str);//重载'>'操作符

char operator [](int index);//重载'[]'操作符

const char*Pointer()

{

return m_String;

}

int GetLength();//获取字符串长度

MyString Left(int n);//获取最左边的n个字符//

MyString Mid(int pos, int n);//从第pos个字符开始获取n个字符

private:

int m_length;
//字符串长度

char* m_String;//用来保存字符串

};

MyString::MyString(const char* str) //默认构造函数

{

if (!str)

{

this->m_String = 0;

}

else

{

this->m_String = new char[strlen(str) + 1];

strcpy(this->m_String, str);

}

}

MyString::MyString(const MyString& str)

{

if (!str.m_String)

{

this->m_String = 0;

}

else

{

this->m_String = new char[strlen(str.m_String) + 1];

strcpy(this->m_String, str.m_String);

}

}

MyString::~MyString() //析构函数

{

delete[]m_String;

m_length = 0;

}

MyString& MyString::operator=(const MyString& str)
//重载'='操作符,对MyString类的对象进行赋值

{

if (this != &str)

{

delete[]this->m_String;

if (!str.m_String)

{

this->m_String = 0;

}

else

{

this->m_String = new char[strlen(str.m_String) + 1];

strcpy(this->m_String, str.m_String);

}

}

return *this;

}

MyString MyString::operator+(const MyString& str) //重载+

{

MyString NewString;

if (!str.m_String)

{

NewString = *this;

}

else if (!this->m_String)

{

NewString = str;

}

else

{

NewString.m_String = new char[strlen(this->m_String) + 1];

strcpy(NewString.m_String, this->m_String);

strcat(NewString.m_String, str.m_String);

}

return NewString;

}

bool MyString::operator==(const MyString& str)
//重载'=='操作符

{

if (strlen(this->m_String) == strlen(str.m_String)) return false;

else

{

return strcmp(this->m_String, str.m_String) ? false : true;

}

}

bool MyString::operator>(const MyString& str)
//重载'>'操作符

{

if (strlen(this->m_String) > strlen(str.m_String)) /* return true;*/

return false;

return true;

}

char MyString::operator[](int index)

{

if (index > 0 && index < strlen(this->m_String))

return this->m_String[index];

}

ostream& operator <<(ostream& os, MyString& str) //重载'<<'操作符

{

os << str.m_String;

return os;

}

istream& operator >>(istream& is, MyString& str) //重载'<<'操作符

{

char tempstr[255];//存储输入流

is >> setw(255) >> tempstr;

str = tempstr;

return is;
//可连续使用>>运算符

}

int MyString::GetLength()

{

//return strlen(this->m_String);

int lenth = 0;

assert(m_String != NULL);

while ((*m_String++) != '\0') /*&& (*(++m_String) != '\0'))*/

{

lenth++;

}

return lenth;

}

MyString MyString::Left(int n) //获取最左边的n个字符//

{

char *p = new char[ n + 1];

int l = 0;

for (int i = 0; l < n; i++)

{

p[l++] = m_String[i];

}

p[l] = '\0';

MyString temp(p);

temp.m_length = n;

return temp;

}

MyString MyString::Right(int n) //获取最后边的n个字符

{

int len = m_length;

int pos = len - n;

char *p = new char[n + 1];

int l = 0;

for (int i = 0; l < n,pos<len; i++)

{

p[l++] = m_String[pos++];

}

p[l] = '\0';

MyString temp(p);

temp.m_length = n;

return temp;

}

MyString MyString::Mid(int pos, int n) //从第m个字符开始获取n个字符

{

char *p = new char[n + 1];

for (int i = 0; i < n;i++)

{

p[i] = m_String[pos];

pos++;

}

p
= '\0';

MyString temp(p);

temp.m_length = n;

return temp;

}

int _tmain(int argc, _TCHAR* argv[])

{

MyString str1;

MyString str2;

MyString str;

MyString str3;

MyString str4;

MyString str5;

str1 = "hello world";

//cin >> str1;

str2 = "MyString";

//str3 = str1 + " Hello";

str3 = str1.Mid(2, 3);

str4 = str1.Left(3);

//str5 = str2.Right(3);

str = str1 + str2;

int len = str1.GetLength();

int len1 = str2.GetLength();

//cout << "str1和str2的长度为:" << len <<" "<< len1<<endl;

cout << "str2的长度为" << len1 << endl;

//cout << str1 << endl;

cout << "获取从str3中第2个开始的3个字符" << endl;

cout << str3 << endl;

cout << str3.GetLength() << endl;

cout << "获取从str1左边的3个字符字符" << endl;

cout << str4 << endl;

//cout << "获取从str2由边的3个字符字符" << endl;

//cout << str5 << endl;

/*cout << str << endl;

cout << str[2] << endl;*/

if (str1>str2)

{

cout << "str1的长度比str2长" << endl;

}

else

{

cout << "str1的长度比str2短" << endl;

}

system("pause");

return 0;

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: