自定义 mystring 类

自定义 mystring 类

#include<iostream>
using namespace std;

// 编程:仿照标准字符串类string,采用动态字符数组来自定义字符串类,可自动适应不同的串长度。
class mystring {
	char* str;
	int maxsize;//最多能储存的字符个数
	int last;//最后一个非'\0'元素的下标
public:
	mystring();
	mystring(const char *s); //要考虑串长
	mystring(mystring & st); //要考虑串长
	~mystring();
	void show();
	char& operator[](int i); //返回引用,可读可写
	mystring & operator=(const mystring &); //要考虑串长
	mystring & operator=(const char *); //这里是把C风格字符串赋给mystring,要考虑串长
	mystring operator+(const mystring &); //要考虑串长,在临时对象中进行
	mystring operator+=(const mystring &); //要考虑串长
	bool operator<(mystring &);
	bool operator==(mystring &);
};
mystring::mystring()
{
	str = NULL;
	maxsize = 0;
	last = -1;

}
//要考虑串长
mystring::mystring(const char *s)
{
	str = new char[strlen(s) + 1];
	maxsize = strlen(s)+1;
	//cout << maxsize << endl;
	last = maxsize - 1;
	strcpy_s(str, strlen(s) + 1, s);
}

//要考虑串长
mystring::mystring(mystring & st)
{
	str = new char[strlen(st.str) + 1];
	maxsize = strlen(st.str) + 1;
	last = maxsize - 1;
	strcpy_s(str, strlen(st.str) + 1, st.str);

}
mystring::~mystring()
{
	if (str != NULL)
	{
		delete[] str;
		str = NULL;
	}
}
void mystring::show()
{
	cout << str << endl;
}

//返回引用,可读可写
char& mystring::operator[](int i)
{
	return str[i];
}

//要考虑串长
mystring & mystring::operator=(const mystring & Be_Copied)
{
	last = Be_Copied.last;
	maxsize = Be_Copied.maxsize;
	if (str != NULL)
	{
		delete[] str;
		str = NULL;
	}
	str = new char[maxsize];
	strcpy_s(str, maxsize, Be_Copied.str);
	return *this;
}

//这里是把C风格字符串赋给mystring,要考虑串长
mystring & mystring::operator=(const char *C_string )
{
	if (str != NULL)
	{
		delete[] str;
		str = NULL;
	}
	maxsize = strlen(C_string) + 1;
	str = new char[maxsize];
	last = maxsize - 1;
	strcpy_s(str, maxsize, C_string);
	return *this;
}

 //要考虑串长,在临时对象中进行
mystring mystring::operator+(const mystring & Be_Added)
{
	mystring  temp;
	temp.maxsize = maxsize + Be_Added.maxsize-1;
	temp.last = temp.maxsize - 1;
	temp.str = new char[temp.maxsize];
	strcpy_s(temp.str, maxsize, str);
	strcat_s(temp.str, temp.maxsize,Be_Added.str);
	return temp;
}

//要考虑串长
mystring mystring::operator+=(const mystring & Be_added)
{
	mystring temp=*this;
	if (str != NULL)
	{
		delete[] str;
		str = NULL;
	}
	maxsize += (Be_added.maxsize - 1);
	last = maxsize - 1;
	str = new char[maxsize];
	strcpy_s(str, maxsize, temp.str);
	strcat_s(str, maxsize, Be_added.str);
	temp = *this;
	return temp;
}

bool mystring::operator<(mystring & Be_Judged)
{
	int i = 0;
	for (; i <= last; i++)
	{
		if(str[i]<Be_Judged.str[i])
		{
			return true;
		}
		else if (str[i] > Be_Judged.str[i])
		{
			return false;
		}
		else if (str[i] == Be_Judged.str[i])
		{
			continue;
		}
	}
	if (i >= last) 
	{
		return false;
	}
}

bool mystring::operator==(mystring & Be_Judged)
{
	if (Be_Judged.last != last)return false;
	int i = 0;
	for (; i <= last; i++)
	{
		if (str[i] != Be_Judged.str[i])
		{
			return false;
		}
		else if (str[i] == Be_Judged.str[i])
		{
			continue;
		}
	}
	if (i >= last)
	{
		return true;
	}

}

int main()
{
	mystring A_string("Good Job!");
	A_string.show();
	cout << A_string[0] << endl;
	A_string[3] = 'T';
	cout << "A_string is "; A_string.show();

	mystring Sec_string("Well Done!");
	Sec_string.show();
	mystring Friend("Hello My Friend!");
	Sec_string = Friend;
	cout << "Sec_string is "; Sec_string.show();

	mystring Thi_string("Well Done!");
	Thi_string = "Love You!";
	cout << "Thi_string is "; Thi_string.show();

	mystring For_string;
	For_string = (Friend + Thi_string);
	cout << "For_string is "; For_string.show();

	mystring Fif_string("Rival ");

	Fif_string += Thi_string;
	Fif_string.show();

	mystring Six_string="Oh My God!";
	mystring Sev_string = "oh My God!";
	if (Sev_string < Six_string)cout << "Yes!" << endl;
	else cout << "No!" << endl;

	mystring Eig_string = "Imagine Dragons!";
	mystring Nin_string = "Imagine Dragons!";
	if (Eig_string == Nin_string)cout << "Yes!" << endl;
	else cout << "No!" << endl;

	system("pause");
	return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值