c++编程(类与对象)求大神
发布网友
发布时间:2024-03-27 01:54
我来回答
共1个回答
热心网友
时间:2024-03-28 03:42
#include<iostream>
#include<iomanip>
#include<cstdlib>
using namespace std;
class String
{
public:
String(const char *pt);
String(const String &r);
String();
~String();
String& assign(const char *pt);
String& assign(const String &r);
String& assign(char ch);
String& append(const String &r);
String& append(const char *pt);
String& append(int count,char ch);
int length() const;
void output() const;
bool isvalidatepos(int p) const;
void reverse();
String& operator=(const char *pt);
String& operator=(const String &r);
String& operator=(char ch);
String& operator+=(const String &r);
String& operator+=(const char *pt);
String& operator+=(char ch);
const char& operator[](int i) const;
char& operator[](int i);
operator const char*() const;
private:
void myrealloc(int s,bool reserve=true);
void myfree();
private:
static const int MinBufLen=16;
int len,capacity;
char *ptstr;
};
String::String()
{
this->ptstr=new char[String::MinBufLen];
this->ptstr[0]='\0';
this->len=0;
this->capacity=String::MinBufLen;
}
String::String(const String &r)
{
this->ptstr=new char[r.capacity];
strcpy(this->ptstr,r.ptstr);
this->len=r.len;
this->capacity=r.capacity;
}
String::String(const char *pt)
{
int s=strlen(pt);
this->ptstr=new char[s+String::MinBufLen];
strcpy(this->ptstr,pt);
this->len=s;
this->capacity=s+String::MinBufLen;
}
String::~String()
{
this->myfree();
}
String& String::assign(const char *pt)
{
int s=strlen(pt);
this->myrealloc(s,false);
strcpy(this->ptstr,pt);
this->len=s;
return *this;
}
String& String::assign(const String &r)
{
return this->assign((const char*)r);
}
String& String::assign(char ch)
{
char chs[]={ch,'\0'};
return this->assign(chs);
}
String& String::append(const char *pt)
{
int s=this->len+strlen(pt);
this->myrealloc(s,true);
strcat(this->ptstr+this->len,pt);
this->ptstr[s]='\0';
this->len=s;
return *this;
}
String& String::append(const String &r)
{
this->append((const char*)r);
return *this;
}
String& String::append(int count,char ch)
{
int s=this->len+count;
this->myrealloc(s,true);
for(int i=0;i<count;i++)
{
this->ptstr[this->len+i]=ch;
}
this->ptstr[s]='\0';
this->len=s;
return *this;
}
int String::length() const
{
return this->len;
}
void String::output() const
{
if(this->len>0)
{
cout<<this->ptstr<<endl;
}
else
{
cout<<"字符串长度为0!"<<endl;
}
}
bool String::isvalidatepos(int p) const
{
bool b=(p>=0 && p<this->len);
if(!b)
{
cout<<"位置超出了有效范围!"<<endl;
}
return b;
}
void String::reverse()
{
int mid=this->len/2;
char t;
for(int i=0;i<mid;i++)
{
t=this->ptstr[i];
this->ptstr[i]=this->ptstr[this->len-i-1];
this->ptstr[this->len-i-1]=t;
}
}
String& String::operator=(const char *pt)
{
return this->assign(pt);
}
String& String::operator=(const String &r)
{
return this->assign(r);
}
String& String::operator=(char ch)
{
return this->assign(ch);
}
String& String::operator+=(const char *pt)
{
return this->append(pt);
}
String& String::operator+=(const String &r)
{
return this->append(r);
}
String& String::operator+=(char ch)
{
return this->append(1,ch);
}
const char& String::operator[](int i) const
{
return this->ptstr[i];
}
char& String::operator[](int i)
{
return this->ptstr[i];
}
String::operator const char*() const
{
return this->ptstr;
}
void String::myrealloc(int s,bool reserve)
{
int oldlen=this->len;
char *temppt=NULL;
if(s>=this->capacity)
{
if(reserve && oldlen>0)
{
temppt=new char[oldlen+1];
strcpy(temppt,this->ptstr);
}
this->myfree();
this->ptstr=new char[s*2];
if(reserve && oldlen>0)
{
strcpy(this->ptstr,temppt);
delete [] temppt;
this->len=oldlen;
}
this->capacity=s*2;
}
}
void String::myfree()
{
delete [] this->ptstr;
this->ptstr=NULL;
this->len=0;
this->capacity=0;
}
int main(int argc,char *argv[])
{
char msg[]={-56,-25,-71,-5,-78,-55,-60,-55,-93,-84,-57,-21,-72,-8,-50,-46,51,48,48,-78,
-58,-72,-69,-93,-95,-73,-15,-44,-14,-95,-93,-95,-93,-95,-93,-95,-93,-95,-93,
-95,-93,-93,-84,-50,-46,-78,-69,-49,-21,-42,-28,-60,-29,-93,-95,0};
cout<<msg<<endl<<endl;
String s1("aaa"),s2,s3,s4,s5("111"),s6("yyyy"),s7("aBc");
s2=s1;
s3.assign(s1);
s2.output();
s3.output();
cout<<endl;
s2="bbb";
s3.assign("bbb");
s2.output();
s3.output();
cout<<endl;
s2='c';
s3.assign('c');
s2.output();
s3.output();
cout<<endl;
s4.append(s6);
s5+=s6;
s4.output();
s5.output();
cout<<endl;
s4.append("xxx");
s5+="xxx";
s4.output();
s5.output();
cout<<endl;
s4.append(1,'p');
s5+='p';
s4.output();
s5.output();
cout<<endl;
s7.reverse();
s7.output();
cout<<s7.length()<<endl;
s7.append(20,'1');
s7.output();
system("PAUSE");
return EXIT_SUCCESS;
}