程设作业:MyString类

直接贴代码:

这个是第一题,直接写的那个。

#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
class MyString
{
public:
	MyString();
	MyString(char*);
	MyString(const MyString&);
	int getlen() ;
	char& operator [] (int);
	char* operator () (int, int);
	MyString operator += (const char*);
	friend ostream& operator << (ostream& output, const MyString&);
	friend MyString operator + (const MyString&, const MyString&);
	friend bool operator > (MyString&, MyString&);
	friend bool operator < (MyString&, MyString&);
	friend bool operator == (MyString&, MyString&);
private:
	char* str;
};
int CompareString( const void * e1, const void * e2) 
{ 
	MyString * s1 = (MyString * ) e1; 
	MyString * s2 = (MyString * ) e2; 
	if( * s1 < *s2 ) return -1; 
	else if( *s1 == *s2) return 0; 
	else if( *s1 > *s2 ) return 1; 
}
int main() 
{ 
	MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); 
	MyString SArray[4] = {"big","me","about","take"}; 
	cout << "1. " << s1 << s2 << s3<< s4<< endl;
	s4 = s3; 
	s3 = s1 + s3; 
	cout << "2. " << s1 << endl; 
	cout << "3. " << s2 << endl; 
	cout << "4. " << s3 << endl; 
	cout << "5. " << s4 << endl; 
	cout << "6. " << s1[2] << endl;
	s2 = s1; 
	s1 = "ijkl-"; 
	s1[2] = 'A' ; 
	cout << "7. " << s2 << endl; 
	cout << "8. " << s1 << endl; 
	s1 += "mnop"; 
	cout << "9. " << s1 << endl; 
	s4 = "qrst-" + s2; 
	cout << "10. " << s4 << endl; 
	s1 = s2 + s4 + " uvw " + "xyz"; 
	cout << "11. " << s1 << endl; 
	qsort(SArray,4,sizeof(MyString),CompareString); 
	for( int i = 0;i < 4;i ++ ) 
		cout << SArray[i] << endl;
	//输出s1的下标为0的字符开始长度为4的子串 
	cout << s1(0,4) << endl; 
	//输出s1的下标为5的字符开始长度为10的子串 
	cout << s1(5,10) << endl; 
}
MyString::MyString()
{
	if(str) delete str;
	str = new char [1];
	str[0] = 0;
}
MyString::MyString(char* x_)
{
	int len = strlen(x_);
	if(str) delete str;
	str = new char [len + 1];
	strcpy(str, x_);
}
MyString::MyString(const MyString& x_)
{
	int len = strlen(x_.str);
	if(str) delete str;
	str = new char [len + 1];
	strcpy(str, x_.str);
}
int MyString::getlen()
{
	return strlen(str);
}
ostream& operator << (ostream& output, const MyString& x_)
{
	output << x_.str;
	return output;
}
MyString operator + (const MyString& x_, const MyString& y_)
{
	int i;
	MyString res;
	int lena = strlen(x_.str), lenb = strlen(y_.str);
	if(res.str) delete res.str;
	res.str = new char [lena + lenb + 1];
	for(i = 0; i != lena; ++i)
	{
		res.str[i] = x_.str[i];
	}
	for(i = 0; i != lenb; ++i)
	{
		res.str[lena + i] = y_.str[i];
	}
	res.str[lena + lenb] = 0;
	return res;
}
char& MyString::operator [] (int x_)
{
	return str[x_];
}
MyString MyString::operator += (const char* x_)
{
	int i;
	int lena = strlen(str), lenb = strlen(x_);
	char *tmp = new char [lena + 1];
	for(i = 0; i != lena; ++i)
	{
		tmp[i] = str[i];
	}
	if(str) delete str;
	str = new char [lena + lenb + 1];
	for(i = 0; i != lena; ++i)
	{
		str[i] = tmp[i];
	}
	for(i = 0; i != lenb; ++i)
	{
		str[lena + i] = x_[i];
	}
	str[lena + lenb] = 0;
	return *this;
}
char* MyString::operator () (int x_, int y_)
{
	int i;
	char* tmp = new char [y_];
	for(i = x_; i != x_ + y_; ++i)
	{
		tmp[i - x_] = str[i];
	}
	tmp[y_] = 0;
	return tmp;
}
bool operator > (MyString& x_, MyString& y_)
{
	int i;
	int lena = x_.getlen(), lenb = y_.getlen();
	int len;
	if(lena < lenb) len = lena;
	else len = lenb;
	for(i = 0; i != len; ++i)
	{
		if(x_.str[i] > y_.str[i])
		{
			return true;
		}
		if(x_.str[i] < y_.str[i])
		{
			return false;
		}
	}
	if(lena < lenb)
	{
		return true;
	}
	return false;
}
bool operator < (MyString& x_, MyString& y_)
{
	if((x_ > y_) || (x_ == y_))
	{
		return false;
	}
	return true;
}
bool operator == (MyString& x_, MyString& y_)
{
	int i;
	int lena = x_.getlen(), lenb = y_.getlen();
	if(lena != lenb)
	{
		return false;
	}
	for(i = 0; i != lena; ++i)
	{
		if(x_.str[i] != y_.str[i])
		{
			return false;
		}
	}
	return true;
}

这个是第二题,继承了string类。

#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
class MyString : public string
{
public:
	MyString() : string() { };
	MyString(const char* str) : string(str) { };
	MyString(string str) : string(str) { };
	MyString operator () (int x_, int y_)
	{
		return substr(x_, y_);
	}
};
int CompareString( const void * e1, const void * e2) 
{ 
	MyString * s1 = (MyString * ) e1; 
	MyString * s2 = (MyString * ) e2; 
	if( * s1 < *s2 ) return -1; 
	else if( *s1 == *s2) return 0; 
	else if( *s1 > *s2 ) return 1; 
}
int main() 
{ 
	MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); 
	MyString SArray[4] = {"big","me","about","take"}; 
	cout << "1. " << s1 << s2 << s3<< s4<< endl;
	s4 = s3; 
	s3 = s1 + s3; 
	cout << "2. " << s1 << endl; 
	cout << "3. " << s2 << endl; 
	cout << "4. " << s3 << endl; 
	cout << "5. " << s4 << endl; 
	cout << "6. " << s1[2] << endl;
	s2 = s1; 
	s1 = "ijkl-"; 
	s1[2] = 'A' ; 
	cout << "7. " << s2 << endl; 
	cout << "8. " << s1 << endl; 
	s1 += "mnop"; 
	cout << "9. " << s1 << endl; 
	s4 = "qrst-" + s2; 
	cout << "10. " << s4 << endl; 
	s1 = s2 + s4 + " uvw " + "xyz"; 
	cout << "11. " << s1 << endl; 
	qsort(SArray,4,sizeof(MyString),CompareString); 
	for( int i = 0;i < 4;i ++ ) 
		cout << SArray[i] << endl;
	//输出s1的下标为0的字符开始长度为4的子串 
	cout << s1(0,4) << endl; 
	//输出s1的下标为5的字符开始长度为10的子串 
	cout << s1(5,10) << endl; 
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值