c++ 实现string类

 

 

 

 

 

 

 

#pragma once
#define RELEASE_VLAUE(p) {if(p){delete p;p=NULL;}}
#define RELEASE_ARRAY(p) {if(p){delete[] p;p=NULL;}}

class CMyString
{
public:
	char * m_strBuf;
public:
	CMyString(char* buf);
	CMyString(CMyString& str);
	CMyString(char c, int n) {}  //使用n个c字符给buf赋值
	CMyString(char* buf, int s, int e) {}//取出buf中的s->e的所有字符,给buf赋值

	int Length();
	void Print();
	int Strcmp(CMyString str);//相等为0,str大为-1,否则为1;
	CMyString Strcat(CMyString str);
	int Find(char c);//找到就返回第一个位置,找不到返回-1;

public:
	//> < >= <= == !=
	//[1]  >>
	CMyString&operator=(CMyString&str);
	CMyString&operator+=(CMyString&str);
	bool  operator>(CMyString&str);
	bool  operator<(CMyString&str);
	bool  operator>=(CMyString&str);
	bool  operator<=(CMyString&str);
	bool  operator==(CMyString&str);
	char  operator[](int i);

	friend ostream& operator<<(ostream&os, CMyString&temp);
	friend istream& operator >> (istream&os, CMyString&temp);

public:
	CMyString();
	~CMyString();
};

 

 

 

 

 

 

 

 

#include "stdafx.h"
#include "MyString.h"


CMyString::CMyString()
{
	this->m_strBuf = new char('\0');
}
CMyString & CMyString::operator=(CMyString & temp)
{
	delete[]m_strBuf;
	int n = temp.Length();
	this->m_strBuf = new char[n + 1];
	for (int i = 0; i < n + 1; ++i)
		m_strBuf[i] = temp.m_strBuf[i];
	return *this;
	// TODO: 在此处插入 return 语句
}
CMyString & CMyString::operator+=(CMyString & str)
{
	int lenA = Length();
	int lenB = str.Length();
	char*buf = new char[lenB + lenA + 1];
	for (int i = 0; i < lenA; ++i)
		buf[i] = m_strBuf[i];
	for (int i = 0; i < lenB; ++i)
		buf[lenA + i] = str.m_strBuf[i];
	buf[lenA + lenB] = '\0';
	delete[]m_strBuf;
	m_strBuf = buf;
	return *this;
	// TODO: 在此处插入 return 语句
}
bool CMyString::operator>(CMyString & str)
{
	//相等为0,str大为-1,否则为1;
	int i = 0;
	int len = 0;
	while (!(len = ((unsigned char)*(m_strBuf + i) - (unsigned char)*(str.m_strBuf))) && *(m_strBuf + i))
	{
		i++;
	}
	if (len > 0)
		return true;
	else
		return false;

	// TODO: 在此处插入 return 语句
}
bool CMyString::operator<(CMyString & str)
{
	//相等为0,str大为-1,否则为1;
	int i = 0;
	int len = 0;
	while (!(len = ((unsigned char)*(m_strBuf + i) - (unsigned char)*(str.m_strBuf))) && *(m_strBuf + i))
	{
		i++;
	}
	if (len < 0)
		return true;
	else
		return false;

	// TODO: 在此处插入 return 语句
}
bool CMyString::operator>=(CMyString & str)
{
	//相等为0,str大为-1,否则为1;
	int i = 0;
	int len = 0;
	while (!(len = ((unsigned char)*(m_strBuf + i) - (unsigned char)*(str.m_strBuf))) && *(m_strBuf + i))
	{
		i++;
	}
	if (len >= 0)
		return true;
	else
		return false;
}
bool CMyString::operator<=(CMyString & str)
{
	//相等为0,str大为-1,否则为1;
	int i = 0;
	int len = 0;
	while (!(len = ((unsigned char)*(m_strBuf + i) - (unsigned char)*(str.m_strBuf))) && *(m_strBuf + i))
	{
		i++;
	}
	if (len <= 0)
		return true;
	else
		return false;
}
bool CMyString::operator==(CMyString & str)
{
	//相等为0,str大为-1,否则为1;
	int i = 0;
	int len = 0;
	while (!(len = ((unsigned char)*(m_strBuf + i) - (unsigned char)*(str.m_strBuf))) && *(m_strBuf + i))
	{
		i++;
	}
	if (len == 0)
		return true;
	else
		return false;
}
char CMyString::operator[](int i)
{
	if (i > Length())
		cout << "下标越界" << endl;
	else
		return m_strBuf[i];
	
}
ostream & operator<<(ostream & os, CMyString & temp)
{
	os << temp.m_strBuf << endl;
	return os;
	// TODO: 在此处插入 return 语句
}

istream & operator >> (istream & is, CMyString & temp)
{
	delete[]temp.m_strBuf;
	char*arr=new char[255];
	is >> arr;
	int len = 0;
	while (arr[len] != '\0')
		len++;
	temp.m_strBuf = new char[len + 1];
	for (int i = 0; i < len; ++i)
		temp.m_strBuf[i] = arr[i];
	temp.m_strBuf[len] = '\0';
	delete[]arr;
	return is;

	// TODO: 在此处插入 return 语句
}

























int CMyString::Length()
{
	int len = 0;
	while (m_strBuf[len] != '\0')
	{
		len++;
	}
	return len;
}

void CMyString::Print()
{
	cout << m_strBuf << endl;
}

int CMyString::Strcmp(CMyString str)
{
	//相等为0,str大为-1,否则为1;
	int i = 0;
	int len = 0;
	while (!(len = ((unsigned char)*(m_strBuf + i) - (unsigned char)*(str.m_strBuf+i))) && *(m_strBuf + i))
	{
		i++;
	}
	/*while (!(len = *(unsigned char*)m_strBuf - *(unsigned char*)str.m_strBuf) && *m_strBuf)
	{
		m_strBuf++;
		str.m_strBuf++;
	}*/
	if (len > 0)
		return 1;
	else if (len < 0)
		return-1;
	else if (len == 0)
		return 0;
}

CMyString CMyString::Strcat(CMyString str)
{
	/*CMyString temp ;
	int lenA = this->Length();
	int lenB = str.Length();
	int len = lenA + lenB;
	temp.m_strBuf = new char[len + 1];
	for (int i = 0; i < lenA; ++i)
	temp.m_strBuf[i] = this->m_strBuf[i];
	for (int i = 0; i < lenB; ++i)
	temp.m_strBuf[lenA+i] = str.m_strBuf[i];
	temp.m_strBuf[len] = '\0';
	return temp;*/


	CMyString temp;
	temp.m_strBuf = this->m_strBuf;
	int lenA = temp.Length();
	int lenB = str.Length();
	int len = lenA + lenB;
	this->m_strBuf = new char[len + 1];
	for (int i = 0; i < lenA; ++i)
		this->m_strBuf[i] = temp.m_strBuf[i];
	for (int i = 0; i < lenB; ++i)
		this->m_strBuf[lenA + i] = str.m_strBuf[i];
	this->m_strBuf[len] = '\0';
	RELEASE_ARRAY(temp.m_strBuf);//释放空间
	return *this;
}

int CMyString::Find(char c)
{
	int len = this->Length();
	for (int i = 0; i < len; ++i)
	{
		if (this->m_strBuf[i] == c)
			return i;
	}
	return -1;
}

CMyString::CMyString(char * buf)
{
	int len = 0;
	while (buf[len] != '\0')len++;
	this->m_strBuf = new char[len + 1];
	for (int i = 0; i < len; ++i)
		m_strBuf[i] = buf[i];
	m_strBuf[len] = '\0';
	Length();
}
CMyString::CMyString(CMyString & str)
{
	int len = str.Length();
	this->m_strBuf = new char[len + 1];
	for (int i = 0; i < len; ++i)
		this->m_strBuf[i] = str.m_strBuf[i];
	this->m_strBuf[len] = '\0';
}
CMyString::~CMyString()
{
	if (!(m_strBuf == NULL))
		RELEASE_ARRAY(m_strBuf);
	RELEASE_VLAUE(m_strBuf);

}


 

 

 

 

 

// zuoye_12_25.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"MyString.h"


int main()
{
	//> < >= <= == !=
	//[1]  >>
	CMyString MyString("12345");
	CMyString MyString1 = MyString;
	cout << MyString << MyString1;
	CMyString MyString2("Hello whorld!");
	MyString1 += MyString2;
	cout << MyString1 << MyString2;
	/*cin >> MyString;
	cout << MyString;*/
	cout << MyString[1] << endl;
	cout << MyString << MyString1;
	CMyString MyString3("12345");
	cout << MyString << MyString3;
	if (MyString == MyString3)
		cout << "相等" << endl;
	if (MyString.Strcmp(MyString3) == 0)
		cout << "相等" << endl;
	if (MyString.Strcmp(MyString3) > 0)
		cout << "大于" << endl;
	if (MyString.Strcmp(MyString3) < 0)
		cout << "小于" << endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值