数据结构教程—串的顺序存储结构C++类实现

类定义:

#include

constexpr auto MaxSize = 50;

using namespace std;

class SqString {
     
private:
	char data[MaxSize];//存放串字符,这里假设分配的空间足够使用,即实现中不考虑空间不足的情况
	int length;//存放串长
	
public:
	SqString();

	void StrAssign(char str[]);//将字符串常量赋值给串
	void StrCopy(SqString t);//将串t复制给当前串
	bool StrEqual(SqString t);//判断串t与当前串是否相等
	int StrLength();//返回当前串的长度
	SqString Concat(SqString t);//连接当前串与串t
	SqString SubStr(int i, int j);//返回当前串从i开始的连续j个字符组成的子串
	SqString InsStr(int i, SqString t);//将串t插入到当前串的第i个字符后,返回产生的新串,参数不正确返回空串
	SqString DelStr(int i, int j);//删除第i个字符开始的长度为j的子串,返回新串,参数错误返回空串
	SqString RepStr(int i, int j, SqString t);//将第i个字符开始的j个字符构成的子串用串t代替
	void DispStr();//输出串中所有字符
};

类中各函数:


SqString::SqString() {
     
	
	this->length = 0;
}

//将字符串常量赋值给串
void SqString::StrAssign(char str[]) {
     
	int i;
	for (i = 0; str[i] != '\0'; i++)
		this->data[i] = str[i];
	this->length = i;//设置串的长度

}
//将串t复制给当前串
void SqString::StrCopy(SqString t) {
     
	int i;
	for (i = 0; i < t.length; i++)
		this->data[i] = t.data[i];
	this->length = t.length;
}
//判断串t与当前串是否相等
bool SqString::StrEqual(SqString t) {
     
	if (this->length != t.length)
		return false;
	for (int i = 0; i < t.length; i++) {
     
		if (this->data[i] != t.data[i])
			return false;
	}
	return true;
}
//返回当前串的长度
int SqString::StrLength() {
     
	return this->length;
}
//连接当前串与串t
SqString SqString::Concat(SqString t) {
     
	SqString result;
	int i;
	for (i = 0; i < this->length; i++)
		result.data[i] = this->data[i];
	for (int j = 0; j < t.length; j++)
		result.data[i + j] = t.data[j];
	result.length = this->length + t.length;
	return result;
}
//返回当前串从i开始的连续j个字符组成的子串
SqString SqString::SubStr(int i, int j) {
     
	SqString result ;
	
	if (i<0 ||i>this->length||j<0||i+j-1>this->length)
		return result;
	int r = 0;
	for (int k = i - 1; k < i + j; k++) {
     
		result.data[r++] = this->data[k];
	}
	return result;
}
//将串t插入到当前串的第i个字符后,返回产生的新串,参数不正确返回空串
SqString SqString::InsStr(int i, SqString t) {
     
	SqString result;
	if (i <= 0 || i>this->length)
		return result;
	int k;
	for (k = 0; k < i; k++)
		result.data[k] = this->data[k];
	for (; t.data[k - i] != '\0'; k++)
		result.data[k] = this->data[k - i];
	for (; k - i - t.length < this->length; k++)
		result.data[k] = this->data[k - i - t.length];
	result.length = this->length + t.length;
	return result;
}
//删除第i个字符开始的长度为j的子串,返回新串,参数错误返回空串
SqString SqString::DelStr(int i, int j) {
     
	SqString result;
	if (i <= 0 || i>this->length ||j<0|| i + j - 1 > this->length)
		return result;
	int k;
	for (k = 0; k < i-1; k++)//复制[0]到[i-1]的字符
		result.data[k] = this->data[k];
	for (; k + j  < this->length; k++)//复制[i+j]到[this.length]的字符
		result.data[k] = this->data[k + j];
	result.length = this->length - j;//新的长度
	return result;

}
//将第i个字符开始的j个字符构成的子串用串t代替
SqString SqString::RepStr(int i, int j, SqString t) {
     
	SqString result;
	if (i <= 0 || i > this->length || i + j - 1 > this->length)
		return result;
	int k;
	for(k = 0; k < i-1 ; k++) //将第0到[i-2]个字符复制到result中,从第[i]个到第[i+j-1]个将被删除
		result.data[k] = this->data[k];
	//此时k=i-1
	for (; k - i+1 < t.length; k++)//将t中所有字符复制到result中
		result.data[k] = t.data[k - i+1];
	//此时k=t.length+i-1
	//下面的循环对this应该从i+j开始
	for (; k+j - t.length+1<this->length; k++)//将this的[i+j]到[this.length-1]复制到result中
		result.data[k] = this->data[k + j - t.length + 1];

	result.length = this->length + t.length - j-1;
	return result;

}
//输出串中所有字符
void SqString::DispStr() {
     
	for (int k = 0; k < this->length; k++)
		cout << this->data[k] << " ";
	cout << endl;
}

主函数测试及运行结果:

int main() {
     

	SqString string1;
	SqString string2;

	char str[] = {
      "abcdefghijk" };
	string1.StrAssign(str);//使用赋值函数
	string1.DispStr();//使用输出函数
	cout << "string1:" << string1.StrLength() << endl;//使用求长度函数

	string2 = string1.DelStr(4, 3);//使用删除函数删除从第4个字符开始的后3个字符
	string2.DispStr();
	cout << "string2:" << string2.StrLength() << endl;

	string1 = string1.Concat(string2);//使用连接函数
	string1.DispStr();
	cout <<"string1:"<< string1.StrLength() << endl;

	string1 = string1.RepStr(4, 3, string2);
	string1.DispStr();
	cout << "string1:" << string1.StrLength() << endl;

	return 0;
}

数据结构教程—串的顺序存储结构C++类实现_第1张图片

你可能感兴趣的:(C++,数据结构,字符串,数据结构,c++)