要求:
使用C++仿照String类实现一个简易的my_string类
my_string的功能:
无参构造
有参构造
析构
拷贝构造
拷贝赋值
c_str
size
empty
at
append
算数运算符+
赋值运算符+=
下标运算符[]
关系运算符 >,<,==,!=,>=,<=
插入提取运算符 <<,>>
代码实现:
my_String.h
#ifndef MY_STRING_H
#define MY_STRING_H
#include<iostream>
using namespace std;
const int MAX = 500;
class my_String
{
private:
char* data;
int ssize;
int now_capacity;
public:
//无参构造
my_String();
//有参构造
my_String(const char* str);
//有参构造
my_String(int n,char c);
//析构
~my_String();
//拷贝构造
my_String(const my_String & other);
//拷贝赋值
my_String & operator=(const my_String & other);
//拷贝赋值
my_String & operator=(const char* str);
//显示函数
void show();
//c风格字符串
char* c_str();
//大小
int size();
//清空
bool empty();
//at访问
char& at(int x);
//capacity
int capacity();
//compare
int compare(const char* str);
//append
my_String & append(const char* str);
//operator+
my_String operator+(const my_String &R)const;
//operator+=
my_String & operator+=(const my_String &R);
//operator[]
char & operator[](const int x)const;
//operator>
bool operator>(const my_String &R)const;
//operator<
bool operator<(const my_String &R)const;
//operator==
bool operator==(const my_String &R)const;
//operator!=
bool operator!=(const my_String &R)const;
//operator>=
bool operator>=(const my_String &R)const;
//operator<=
bool operator<=(const my_String &R)const;
//operator<<
friend ostream & operator<<(ostream &L,const my_String &R);
//operator>>
friend istream & operator>>(istream &L,my_String &R);
};
#endif // MY_STRING_H
my_String.c
//#include <iostream>
#include<cstring>
#include <my_String.h>
//using namespace std;
//无参构造
my_String::my_String():now_capacity(15){
data = new char[now_capacity];
data[0] = '0';
}
//有参构造
my_String::my_String(const char* str):now_capacity(15){
ssize = strlen(str);
if(ssize < now_capacity)
data = new char [now_capacity];
else{
data = new char [ssize*2];
now_capacity = ssize*2;
}
strcpy(data,str);
cout << "有参构造" << endl;
}
//有参构造
my_String::my_String(int n,char c):now_capacity(15){
ssize = n;
if(n<now_capacity)
data = new char[now_capacity];
else{
data = new char[ssize*2];
now_capacity = ssize*2;
}
int i = 0;
for(i=0;i<n;i++)
data[i]=c;
data[i]='\0';
}
//析构
my_String::~my_String(){
cout << this << "析构" << endl;
delete []data;
data = nullptr;
}
//拷贝构造
my_String::my_String(const my_String & other):ssize(other.ssize),now_capacity(15){
this->ssize= other.ssize;
this->now_capacity = other.now_capacity;
this->data = new char [this->now_capacity];
strcpy(this->data,other.data);
cout << "拷贝构造" << endl;
}
//拷贝赋值
my_String & my_String::operator=(const my_String & other){
this->ssize=other.ssize;
this->now_capacity = other.now_capacity;
delete []data;
this->data = new char [this->now_capacity];
strcpy(this->data,other.data);
return *this;
}
//显示函数
void my_String::show(){
cout << data << endl;
}
//c风格字符串
char* my_String::c_str(){
return data;
}
//大小
int my_String::size(){
return ssize;
}
//判空
bool my_String::empty(){
return ssize == 0;
}
//at访问
char& my_String::at(int x){
if(x < 0 || x>ssize-1){
cout << "越界访问" << endl;
return data[-1];
}
return data[x];
}
//capacity
int my_String::capacity(){
return now_capacity;
}
//compare
int my_String::compare(const char* str){
int tep;
char *temp = data;
while((tep=*temp-*str)==0 && *temp!='\0')//当两个字符相减为0时,并且没有到最后'\0',循环继续
{
temp++;//指针+1
str++;//指针+1
}
return tep;
}
//append
my_String & my_String::append(const char* str){
// cout << "data = " << data << endl;
int cat_size = strlen(str);
if((ssize+cat_size)>=now_capacity)
now_capacity = (ssize+cat_size)*2;
char* temp = new char[now_capacity]{0};
// cout << "data = " << data << endl;
strcpy(temp,data);
// cout << temp << endl;
delete []data;
strcat(temp,str);
data = new char [now_capacity];
strcpy(data,temp);
delete []temp;
return *this;
}
//operator+
my_String my_String::operator+(const my_String &R)const{
int psize = strlen(R.data);
my_String temp;
temp.ssize = this->ssize+psize;
if(temp.ssize>=now_capacity)
temp.now_capacity = (this->ssize+psize)*2;
temp.data = new char[temp.now_capacity];
strcpy(temp.data,this->data);
strcat(temp.data,R.data);
// cout << "temp.data = " << temp.data << endl;
return temp;
}
//operator+=
my_String & my_String::operator+=(const my_String &R){
// cout << "data = " << data << endl;
int cat_size = R.ssize;
if((ssize+cat_size)>=this->now_capacity)
this->now_capacity = (ssize+cat_size)*2;
char* temp = new char[this->now_capacity]{0};
// cout << "data = " << data << endl;
strcpy(temp,this->data);
// cout << temp << endl;
delete []this->data;
strcat(temp,R.data);
this->data = new char [this->now_capacity];
strcpy(this->data,temp);
delete []temp;
// cout << this->data << endl;
return *this;
}
//operator[]
char & my_String::operator[](const int x)const{
return data[x];
}
//operator>
bool my_String::operator>(const my_String &R)const{
int tep;
char *temp = data;
char *str = R.data;
while((tep=*temp-*str)==0 && *temp!='\0')//当两个字符相减为0时,并且没有到最后'\0',循环继续
{
temp++;//指针+1
str++;//指针+1
}
return tep>0;
}
//operator<
bool my_String::operator<(const my_String &R)const{
int tep;
char *temp = data;
char *str = R.data;
while((tep=*temp-*str)==0 && *temp!='\0')//当两个字符相减为0时,并且没有到最后'\0',循环继续
{
temp++;//指针+1
str++;//指针+1
}
return tep<0;
}
//operator==
bool my_String::operator==(const my_String &R)const{
int tep;
char *temp = data;
char *str = R.data;
while((tep=*temp-*str)==0 && *temp!='\0')//当两个字符相减为0时,并且没有到最后'\0',循环继续
{
temp++;//指针+1
str++;//指针+1
}
return tep==0;
}
//operator!=
bool my_String::operator!=(const my_String &R)const{
int tep;
char *temp = data;
char *str = R.data;
while((tep=*temp-*str)==0 && *temp!='\0')//当两个字符相减为0时,并且没有到最后'\0',循环继续
{
temp++;//指针+1
str++;//指针+1
}
return tep!=0;
}
//operator>=
bool my_String::operator>=(const my_String &R)const{
int tep;
char *temp = data;
char *str = R.data;
while((tep=*temp-*str)==0 && *temp!='\0')//当两个字符相减为0时,并且没有到最后'\0',循环继续
{
temp++;//指针+1
str++;//指针+1
}
return tep>=0;
}
//operator<=
bool my_String::operator<=(const my_String &R)const{
int tep;
char *temp = data;
char *str = R.data;
while((tep=*temp-*str)==0 && *temp!='\0')//当两个字符相减为0时,并且没有到最后'\0',循环继续
{
temp++;//指针+1
str++;//指针+1
}
return tep<=0;
}
//operator<<
ostream & operator<<(ostream &L,const my_String &R){
L << R.data;
return L;
}
//operator>>
istream & operator>>(istream &L,my_String &R){
char *str = new char[MAX];
L>>str;
int t_size = strlen(str);
R.ssize = t_size;
if(t_size > R.now_capacity)
R.now_capacity = t_size*2;
delete []R.data;
R.data = new char[R.now_capacity];
strcpy(R.data,str);
delete []str;
return L;
}
main.c
//#include <iostream>
#include<cstring>
#include <my_String.h>
//using namespace std;
int main()
{
//append
//operator+
my_String s1 = "hello";
my_String s2 = " world";
//s1+s2; //success
//oeprator+=
my_String s3=s1;
s3+=s2;
s3.show();
//operator[]
s3[5]='X';
s3.show();
//operator>
if(s1>s2)
cout<<"line : "<<__LINE__<<":"<<"s1大"<<endl;
else
cout<<"line : "<<__LINE__<<":"<<"s1小"<<endl;
if(s1<s2)
cout<<"line : "<<__LINE__<<":"<<"s1小"<<endl;
else
cout<<"line : "<<__LINE__<<":"<<"s1大"<<endl;
//oeprator==
s3=s1;
if(s1==s2)
cout<<"line : "<<__LINE__<<":"<<"相等"<<endl;
else
cout<<"line : "<<__LINE__<<":"<<"不相等"<<endl;
if(s1==s3)
cout<<"line : "<<__LINE__<<":"<<"相等"<<endl;
else
cout<<"line : "<<__LINE__<<":"<<"不相等"<<endl;
//oeprator!=
s3=s1;
if(s1!=s2)
cout<<"line : "<<__LINE__<<":"<<" 不相等"<<endl;
else
cout<<"line : "<<__LINE__<<":"<<"相等"<<endl;
if(s1!=s3)
cout<<"line : "<<__LINE__<<":"<<"不相等"<<endl;
else
cout<<"line : "<<__LINE__<<":"<<"相等"<<endl;
//oeprator>=
s3=s1;
if(s1>=s2)
cout<<"line : "<<__LINE__<<":"<<"s1>=s2"<<endl;
else
cout<<"line : "<<__LINE__<<":"<<"s1<s2"<<endl;
if(s1>=s3)
cout<<"line : "<<__LINE__<<":"<<"s1>=s3"<<endl;
else
cout<<"line : "<<__LINE__<<":"<<"s1<s3"<<endl;
//oeprator<=
s3=s1;
if(s1<=s2)
cout<<"line : "<<__LINE__<<":"<<"s1<=s2"<<endl;
else
cout<<"line : "<<__LINE__<<":"<<"s1>s2"<<endl;
if(s1<=s3)
cout<<"line : "<<__LINE__<<":"<<"s1<=s3"<<endl;
else
cout<<"line : "<<__LINE__<<":"<<"s1>s3"<<endl;
//operator<<
cout << "s1 = " << s1 << " , s2 = " << s2 <<endl;
//operator>>
my_String s4;
cin >> s1;
s1.show();
cout << s1.capacity() << endl;
return 0;
}