字符串

Posted on 2016-06-01 12:45  洪爵士  阅读(132)  评论(0编辑  收藏  举报

MyString.h

#include <iostream>
using namespace std;
class MyString 
{
	friend ostream& operator<<(ostream &out,MyString &mystring);
	friend istream& operator>>(istream &in,MyString &mystring);
public:
	MyString();
	MyString(int len);
	MyString(const char* str);
	MyString(const MyString &another);
	~MyString();
public:
    MyString& operator=(const char *str);
	MyString& operator=(const MyString &another);
	char& operator[](int index);
	bool operator==(const char *str) const;//这里的const表示指针所指向的内存空间不能变
	bool operator==(const MyString &another) const;
	bool operator!=(const char *str) const;
	bool operator!=(const MyString &another) const;
	bool operator>(const char *str);
	bool operator<(const char *str);
	bool operator>(const MyString &another);
	bool operator<(const MyString &another);
	MyString operator+(const char *str);
	MyString operator+(const MyString &another);
	MyString& operator+=(const char *str);
	MyString& operator+=(const MyString &another);
protected:
private:
	int len;
	char *str;

};

  MyString.cpp

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include "MyString.h"
ostream& operator<<(ostream &out,MyString &mystring)
{
	if(mystring.str==NULL)
	{
		cout<<"NULL"<<endl;
		return out;
	}
	out<<mystring.str;
	return out;
}
istream& operator>>(istream &in,MyString &mystring)
{
	char buf[1024]={0};//规定字符串的最大长度
	cin>>buf;
	mystring.len=strlen(buf);
	mystring.str=new char[mystring.len+1];
	strcpy(mystring.str,buf);
	return in;
}
MyString::MyString()
{
	this->len=0;
	this->str=NULL;
}
MyString::MyString(int len)
{
	if(len==0)
	{
		this->len=0;
		this->str=new char[len+1];
		strcpy(this->str,"");
	}
	else
	{
		this->len=len;
		this->str=new char[len+1];
		memset(this->str,0,len+1);
	}
}
MyString::MyString(const char *str)
{
	if(str==NULL)
	{
		this->len=0;
		this->str=new char[len+1];
		strcpy(this->str,"");
	}
	else
	{
		this->len=strlen(str);
		this->str=new char[len+1];
		strcpy(this->str,str);
	}
}
MyString::MyString(const MyString &another)//拷贝构造函数
{
	this->len=another.len;
	this->str=new char[this->len+1];
	strcpy(this->str,another.str);
}
MyString::~MyString()
{
	if(this->str!=NULL)
	{
		delete[] this->str;
		this->str=NULL;
		this->len=0;
	}
}
MyString& MyString::operator=(const char *str)
{
	if(this->str!=NULL)//释放旧的内存
	{
		delete[] this->str;
		this->str=NULL;
		this->len=0;
	}
	//根据p分配内存
	if(str==NULL)
	{
		len=0;
		str=new char[len+1];
		strcpy(this->str,"");
	}
	else
	{
		this->len=strlen(str);
		this->str=new char[len+1];
		strcpy(this->str,str);
	}
	return *this;  
}
MyString& MyString::operator=(const MyString &another)
{
	if(this==&another)//如果自己对自己赋值
	{
		return *this;
	}
	if(this->str!=NULL)//释放旧的内存
	{
		delete[] this->str;
		this->str=NULL;
		this->len=0;
	}
	//根据s分配内存
	this->len=another.len;
	this->str=new char[len+1];
	strcpy(str,another.str);
	return *this;  
}
char& MyString::operator[](int index)//[]运算符重载求出是s[index]的元素
{
	return this->str[index];
}
bool MyString::operator==(const char *str) const
{
	if(str==NULL)
	{
		if(len==0)
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
	else
	{
		if(strcmp(this->str,str)==0)
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
}
bool MyString::operator!=(const char *str) const
{
	return !(*this==str);
}
bool MyString::operator==(const MyString &another) const
{
	if(strcmp(this->str,another.str)==0)
	{
		return true;
	}
	else
	{
		return false;	
	}	
}
bool MyString::operator!=(const MyString &another) const
{
	return !(*this==another);
}
bool MyString::operator<(const char *str)
{
	if(strcmp(this->str,str)<0)
		return true;
	else
		return false;
}
bool MyString::operator>(const char *str)
{
	if(strcmp(this->str,str)>0)
		return true;
	else
		return false;
}
bool MyString::operator<(const MyString &another)
{
	if(strcmp(this->str,another.str)<0)
		return true;
	else 
		return false;
}
bool MyString::operator>(const MyString &another)
{
	if(strcmp(this->str,another.str)>0)
		return true;
	else 
		return false;
}
MyString MyString::operator+(const char *str)
{
	int tmp_len=this->len+strlen(str);
	MyString tmp(tmp_len);
	strcpy(tmp.str,this->str);
	strcat(tmp.str,str);
	return tmp;
}
MyString MyString::operator+(const MyString &another)
{
	int tmp_len=this->len+another.len;
	MyString tmp(tmp_len);
	strcpy(tmp.str,this->str);
	strcat(tmp.str,another.str);
	return tmp;
}
MyString& MyString::operator+=(const char *str)
{
	int tmp_len=this->len+strlen(str);
	MyString tmp(tmp_len);
	strcpy(tmp.str,this->str);
	strcat(tmp.str,str);
	if(this->str!=NULL)//释放旧的内存
	{
		delete[] this->str;
		this->str=NULL;
		this->len=0;
	}
	*this=tmp;////因为重载了=操作符所以这里可以直接用
	return *this;
}
MyString& MyString::operator+=(const MyString &another)
{
	int tmp_len=this->len+another.len;
	MyString tmp(tmp_len);
	strcpy(tmp.str,this->str);
	strcat(tmp.str,another.str);
	if(this->str!=NULL)//释放旧的内存
	{
		delete[] this->str;
		this->str=NULL;
		this->len=0;
	}
	*this=tmp;//因为重载了=操作符所以这里可以直接用
	return *this;
}

  TestMyString.cpp测试字符串类

#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
using namespace std;
#include "MyString.h"
#include <string>
int main()
{
	MyString s1="abc";
	MyString s2="aba";
	if(s1<s2)
	{
		cout<<"yoinggai"<<endl;
	}
	MyString s3=s1+"1233";//s1.operator+("123");
	cout<<s3<<endl;
	//s3=s1+s2;
	s1+=s2;
	cout<<s1<<endl;
	return 0;
}