⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string.cpp

📁 小型编译器的课程设计实例哦 值得一看的
💻 CPP
字号:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "String.h"
String::String()
{
	this->pBuffer = NULL;
	this->length = 0;
}
String::~String()
{
	delete this->pBuffer; //释放内存

	this->pBuffer = NULL;//清空数据成员
	this->length = 0;//清空数据成员
}
String::String(char ch, int nRepeat)
{
	this->length = nRepeat;//取得长度
	this->pBuffer = new char[this->length + 1];//申请内存
	assert(this->pBuffer != NULL);//确保申请成功
	_strnset(this->pBuffer, ch, nRepeat);
	*(this->pBuffer + this->length) = '\0';//设置字符串结束符
}
String::String(const char* lpch)
{
	this->length = strlen(lpch);//首先取得长度
	this->pBuffer = new char[this->length + 1];//然后申请内存
	assert(this->pBuffer != NULL);//确定申请成功
	strcpy(this->pBuffer, lpch);//把内容复制进来
}

String::String(const String& src)
{
	if (this == &src)//防止自复制
		exit(1);

	this->length = strlen(src.pBuffer);//取得长度
	this->pBuffer = new char[this->length + 1];//申请内存
	assert(this->pBuffer != NULL);//确保申请成功
	strcpy(this->pBuffer, src.pBuffer);//复制内容
}

int String::GetLength() const
{
	return this->length;
}

bool String::IsEmpty() const
{
	return this->pBuffer == NULL || this->length == 0;
}

void String::Empty()
{
	*this->pBuffer = '\0';
	this->length = 0;
}

char String::GetAt(int nIndex) const
{
	if (nIndex >= 0 && nIndex < this->length)//如果下标有效
		return *(this->pBuffer + nIndex);//返回字符
	return char();//无效的下标
}

//同上
char String::operator [] (int nIndex) const
{
	return this->GetAt(nIndex);
}

//把下标为nIndex的字符设置为ch
void String::SetAt(int nIndex, char ch)
{
	if (nIndex >= 0 && nIndex < this->length)//如果下标有效
		*(this->pBuffer + nIndex) = ch;//改变字符
}

//转换构造函数:把String类的对象转换成C风格的字符串
String::operator char* ( ) const
{
	return this->pBuffer;
}

//判断当前对象是否为空
bool String::operator ! () const
{
	//两者都为空时,则空
	return this->pBuffer != NULL && this->length != 0;
}

//重载"=",实现String类对象的相互复制
const String& String::operator = (const String& src)
{
	if (this == &src)//防止自赋值,即a=a
		exit(1);

	if (this->length != src.length) {//如果两者长度不相等
		delete this->pBuffer;//先释放已经占有的内存
		this->length = strlen(src.pBuffer);//与右操作数的长度一致
		this->pBuffer = new char[this->length + 1];//申请内存
		assert(this->pBuffer != NULL);//确保申请成功
	}
	//如果两者长度相等,则无需上面的内存重新申请,直接复制
	strcpy(this->pBuffer, src.pBuffer);

	//返回当前对象
	return *this;
}

//实现a='X'
/*const String& String::operator = (char ch)
{
	//先调用构造函数把ch构造成一个String类的对象
	//然后重用=
	return *this = String(ch);
}*/

//实现a="Hello"
const String& String::operator = (const char* psz)
{
	//先调用构造函数把psz构造成一个String类的对象
	//然后重用=
	return *this = String(psz);
}
	
//实现字符串的连接
String String::operator + (const String& string)
{
	if (string.IsEmpty())//右操作数为空
		return *this;//返回左操作数
	if (this->IsEmpty())//左操作数为空
		return string;//返回右操作数

	//两者都不为空
	int len = this->length + string.length;//新长度为两者之和
	char* pstr = new char[len + 1];//申请内存
	assert(pstr != NULL);//确保申请成功

	strcpy(pstr, *this);//先复制左操作数的内容
	strcat(pstr, string);//再连接右操作数的内容

	String temp(pstr);//用这个字符串构造一个临时对象
	delete pstr; //返还内存
	return temp;
}

//实现a+'X'
/*String String::operator + (char ch)
{
	//先调用构造函数把ch构造成一个String类的对象
	//然后重用+
	return *this + String(ch);
}*/

String String::operator + (const char* lpsz)
{
	//先调用构造函数把lpsz构造成一个String类的对象
	//然后重用+
	return *this + String(lpsz);
}

//实现'X'+a
/*String operator + (char ch, const String& string)
{
	//先调用构造函数把ch构造成一个String类的对象
	//然后重用+
	return String(ch) + string;
}*/

//实现"Hello"+a
String operator + (const char* lpsz, const String& string)
{
	return String(lpsz) + string;
}

//实现a+=b
const String& String::operator += (const String& string)
{
	//重用+和=
	return *this = *this + string;
}

/*const String& String::operator += (char ch)
{
	//重用+=
	return *this += String(ch);
}*/

const String& String::operator += (const char* lpsz)
{
	return *this += String(lpsz);
}
		
bool String::operator == (const String& s)
{
	if (this->IsEmpty() || s.IsEmpty()) { //若两者有一为空,则退出
		cout << "Error: comparing empty string!" << endl;
		exit(1);
	}
	return strcmp(*this, s) == 0;
}

bool String::operator != (const String& s)
{
	if (this->IsEmpty() || s.IsEmpty()) {
		cout << "Error: comparing empty string!" << endl;
		exit(1);
	}
	return strcmp(*this, s) != 0;
}

bool String::operator < (const String& s)
{
	if (this->IsEmpty() || s.IsEmpty()) {
		cout << "Error: comparing empty string!" << endl;
		exit(1);
	}
	return strcmp(*this, s) < 0;
}

bool String::operator > (const String& s)
{
	if (this->IsEmpty() || s.IsEmpty()) {
		cout << "Error: comparing empty string!" << endl;
		exit(1);
	}
	return strcmp(*this, s) > 0;
}

bool String::operator <= (const String& s)
{
	if (this->IsEmpty() || s.IsEmpty()) {
		cout << "Error: comparing empty string!" << endl;
		exit(1);
	}
	return strcmp(*this, s) <= 0;
}

bool String::operator >= (const String& s)
{
	if (this->IsEmpty() || s.IsEmpty()) {
		cout << "Error: comparing empty string!" << endl;
		exit(1);
	}
	return strcmp(*this, s) >= 0;
}

bool String::operator == (const char* pstr)
{
	//重用==
	return *this == String(pstr);
}

bool String::operator != (const char* pstr)
{
	//重用!=
	return *this != String(pstr);
}

bool String::operator < (const char* pstr)
{
	//重用<
	return *this < String(pstr);
}

bool String::operator > (const char* pstr)
{
	//重用>
	return *this > String(pstr);
}

bool String::operator <= (const char* pstr)
{
	//重用<=
	return *this <= String(pstr);
}

bool String::operator >= (const char* pstr)
{
	//重用>=
	return *this >= String(pstr);
}

bool operator == (const char* pstr, const String& s)
{
	return String(pstr) == s;
}

bool operator != (const char* pstr, const String& s)
{
	return String(pstr) != s;
}

bool operator < (const char* pstr, const String& s)
{
	return String(pstr) < s;
}

bool operator > (const char* pstr, const String& s)
{
	return String(pstr) > s;
}

bool operator <= (const char* pstr, const String& s)
{
	return String(pstr) <= s;
}

bool operator >= (const char* pstr, const String& s)
{
	return String(pstr) >= s;
}

ostream& operator << (ostream& os, const String& s)
{
	if (s.length == 0)
		os << "Error: empty buffer!" << endl;
	else
		os << s.pBuffer;
	return os;
}

istream& operator >> (istream& is, String& s)
{
	char tmp[256];
    
    if (is >> tmp) { // eof?
		delete s.pBuffer;
		s.length = strlen(tmp);
		s.pBuffer = new char [s.length + 1];
		assert(s.pBuffer != NULL);
		strcpy(s.pBuffer, tmp);
    }
    return is;
}

int String::ReadString(istream& is, char delimiter)
{
    char tmp[256];

    // if not eof, read line of up to 255 characters
    if (is.getline(tmp, 256, delimiter)) {
        delete this->pBuffer;
		this->length = strlen(tmp);
		this->pBuffer = new char [this->length + 1];
		assert(this->pBuffer != NULL);
		strcpy(this->pBuffer, tmp);
		return this->length;
    }
    else
        return -1;  // return -1 on end of file
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -