题目:
使用动态内存分配,自己实现一个字符串类,并编写main函数测试该类。
class String {
char *ptr;
public:
String()//构造函数
{
ptr = NULL;
}
String(const char * s)//构造函数,用字符串s初始化
String(const String& s)//拷贝构造函数
void concat(const String &s)//将字符串s连接到当前字符串后
String substring(int begin, int len);//返回当前字符串中从下标begin开始,长度为len的子字符串
String& operator=(const String &s);//赋值运算符
int length()const; //返回字符串长度
void print()const; //输出字符串
~String();//析构函数
};
这里是main函数
void main()
{
String s1("hello");
String s2(s1);
String s3;
s1.print();
s2.print();
s3.print();
s1 = "good morning ";
s2 = "beijing";
(s3=s2)=s1;
cout << s1.length() << endl;
cout << s2.length() << endl;
cout << s3.length() << endl;
s1.print();
s2.print();
s3.print();
s1.concat(s2);
s1.print();
s2.print();
String s4;
s4 = s2.substring(2,3);
s4.print();
s4 = s2.substring(2,15);
s4.print();
}
程序运行结果如下:
hello
hello
13
7
13
good morning
beijing
good morning
good morning beijing
beijing
iji
ijing
请按任意键继续. . .
实现代码:
#include<iostream>
#include<cstring>
using namespace std;
#define NULL 0
class String
{
char* ptr;
public:
String()//创建一个空的字符串
{
ptr=NULL;
}
String(const char* s)//使用字符串s初始化
{
if(s==NULL)
{
ptr=NULL;
ptr=new char[1];
if(ptr==NULL)
{
std::cout<<"申请内存失败!"<<std::endl;
exit(1);
}
ptr[0]='\0';
}
else
{
int leng=strlen(s);
ptr=new char[leng+1];
if(ptr==NULL)
{
std::cout<<"申请内存失败!"<<std::endl;
exit(1);
}
strcpy(ptr,s);
}
}
String(const String &s)//拷贝构造函数
{
int lengs=strlen(s.ptr);
ptr=new char[lengs+1];
if (s.ptr==NULL)
{
std::cout<<"申请内存失败!"<<std::endl;
exit(1);
}
strcpy(ptr,s.ptr);
}
void concat(const String& s)//将字符串s连接在当前字符串其后
//String& assign(const char *s,int n); //将字符串s的前n个输出
{
int c,b=0;
c = strlen(ptr);
char*p = new char[c + strlen(s.ptr)];
for(int i=0;i<c;i++)
{
p[i]=ptr[i];
}
for(b=0;b<strlen(s.ptr);)
{
p[c] = s.ptr[b];
b++;
c++;
}
delete []ptr;
ptr=p;
}
String substring(int begin, int len)//字符串从begin到len结束
{
if(strlen(ptr)>=(len+begin))
{
char*ph=new char[len];
for(int i=0,j=begin;i<len;)
{
ph[i]=ptr[j];
j++;
i++;
}
return ph;
}
else
{
char*ph=new char[strlen(ptr)-begin];
for(int i=0,j=begin;j<strlen(ptr);)
{
ph[i]=ptr[j];
i++;
j++;
}
delete []ptr;
return ptr=ph;
}
}
String& operator=(const String& s)//等号赋值,使用一个s对象初始化另外一个ptr对象
{
if (this ==&s)
{
return *this;
}
else
{
int lengss=strlen(s.ptr);
delete []ptr;
ptr=new char[lengss+1];
strcpy(ptr,s.ptr);
return *this;
}
}
int length()const
{
int i=0,count=0;
while (ptr[i] != NULL)
{
count++;
i++;
}
return count;
}//返回字符串长度
void print()const
{
int i = 0;
if(ptr)
{
for(i=0;i<strlen(ptr);i++)
{
cout<<ptr[i];
}
cout<<endl;
}
// while (ptr[i] != '\0'||ptr[i]!=NULL)
// {
// cout << ptr[i];
// i++;
// }
}//输出字符串
~String()
{
delete [] ptr;
}//析构函数
};
int main()
{
String s1("hello");
String s2(s1);
String s3;
s1.print();
s2.print();
s3.print();
s1 = "good morning ";
s2 = "beijing";
(s3 = s2) = s1;
cout << s1.length() << endl;
cout << s2.length() << endl;
cout << s3.length() << endl;
s1.print();
s2.print();
s3.print();
s1.concat(s2);
s1.print();
s2.print();
String s4;
s4 = s2.substring(2, 3);
s4.print();
s4 =s2.substring(2, 15);
s4.print();
return 0;
}