My_String完善

#include "my_string_ok.h"

My_string_Ok::My_string_Ok():size(20)

{

len = 0;

ptr = new char[size];

ptr[len] = '\0';

}

My_string_Ok::My_string_Ok(int num,char c)

{

cout<<"有参构造"<<endl;

ptr = new char [20] ;

len = 0;

for(int i=0; i<num; i++)

{

// cout<<"i ="<<i<<endl;

*(ptr+i) = c;

len++;

}

*(ptr+num)='\0';

// cout<<data()<<endl;

// cout<<len<<endl;

}

My_string_Ok::My_string_Ok(const char* arr)

{

cout<<"有参构造"<<endl;

if(if_empty(strlen(arr)))

{

len = 0;

ptr = new char[20];

ptr[len] = '\0';

return ;

}

/*if(strlen(arr)>size)

{

cout<<"输入数据过大"<<endl;

return;

}*/

ptr = new char [20];

strcpy(ptr,arr);

len = strlen(arr);

//cout<<data()<<endl;

}

My_string_Ok::My_string_Ok(const My_string_Ok & other)

{

cout<<"拷贝构造函数"<<endl;

this->ptr = new char[20];

strcpy(this->ptr,other.ptr);

this->len = other.len;

// cout<<data()<<endl;

}

My_string_Ok&My_string_Ok ::operator=(const My_string_Ok &other)

{

cout<<"拷贝赋值函数"<<endl;

if(this == &other)

{

return *this;

}

if(!if_empty(other.len))

{

delete ptr;

}

ptr = new char[20];

strcpy(ptr,other.ptr);

len = other.len;

// cout<<data()<<endl;

return *this;

}

My_string_Ok&My_string_Ok ::operator=(const char* arr)

{

cout<<"拷贝赋值函数2"<<endl;

delete ptr;

ptr = new char[20];

strcpy(ptr,arr);

len = strlen(arr);

// cout<<data()<<endl;

return *this;

}

My_string_Ok&My_string_Ok :: operator+(const char c)

{

cout<<"+函数"<<endl;

// My_string_Ok s1;

// ptr = new char[20];

//s1.len = this->len;

*(this->ptr+this->len) = c;

this->len++;

*(this->ptr + len) = '\0';

// strcpy(s1.ptr,this->ptr);

return *this;

}

char My_string_Ok :: operator[](int num)

{

return ptr[num];

}

bool My_string_Ok :: operator>(const My_string_Ok &other)

{

int i =0 ;

while(1)

{

if(this->ptr[i] == other.ptr[i] && this->ptr[i]!='\0' && other.ptr[i]!='\0')

{

i++;

}

else

break;

}

if(this->ptr[i]>other.ptr[i])

return 1;

else

return 0;

}

bool My_string_Ok :: operator<(const My_string_Ok &other)

{

int i =0 ;

while(1)

{

if(this->ptr[i] == other.ptr[i] && this->ptr[i]!='\0' && other.ptr[i]!='\0')

{

i++;

}

else

break;

}

if(this->ptr[i]<other.ptr[i])

return 1;

else

return 0;

}

bool My_string_Ok :: operator == (const My_string_Ok &other)

{

int i =0 ;

while(1)

{

if(this->ptr[i] == other.ptr[i] && this->ptr[i]!='\0' && other.ptr[i]!='\0')

{

i++;

}

else

break;

}

if(this->ptr[i] == other.ptr[i])

return 1;

else

return 0;

}

bool My_string_Ok :: operator >= (const My_string_Ok &other)

{

int i =0 ;

while(1)

{

if(this->ptr[i] == other.ptr[i] && this->ptr[i]!='\0' && other.ptr[i]!='\0')

{

i++;

}

else

break;

}

if(this->ptr[i] >= other.ptr[i])

return 1;

else

return 0;

}

bool My_string_Ok :: operator <= (const My_string_Ok &other)

{

int i =0 ;

while(1)

{

if(this->ptr[i] == other.ptr[i] && this->ptr[i]!='\0' && other.ptr[i]!='\0')

{

i++;

}

else

break;

}

if(this->ptr[i] <= other.ptr[i])

return 1;

else

return 0;

}

bool My_string_Ok :: operator != (const My_string_Ok &other)

{

int i =0 ;

while(1)

{

if(this->ptr[i] == other.ptr[i] && this->ptr[i]!='\0' && other.ptr[i]!='\0')

{

i++;

}

else

break;

}

if(this->ptr[i] != other.ptr[i])

return 1;

else

return 0;

}

My_string_Ok&My_string_Ok :: operator+=(const char *arr)

{

len = len + strlen(arr);

if(len>size)

{

cout<<"超出堆区"<<endl;

len = len - strlen(arr);

}

else

{

ptr = strcat(ptr,arr);;

}

return *this;

}

My_string_Ok&My_string_Ok :: operator+=(const char c)

{

if(len+1>size)

cout<<"超出堆区"<<endl;

else

{

*(ptr+len)=c;

len+=1;

*(ptr+len)='\0';

}

return *this;

}

My_string_Ok::~My_string_Ok()

{

delete ptr;

/* len = 0;

ptr = NULL;*/

}

char *My_string_Ok::data()

{

return ptr;

}

bool My_string_Ok::if_empty(int num)

{

return num==0;

}

void My_string_Ok :: show()

{

cout<<data()<<endl;

}

int My_string_Ok :: get_len()

{

int i=0;

while(1)

{

if(ptr[i]!='\0')

i++;

else

break;

}

return i;

}

std::ostream & operator<<(ostream & out,const My_string_Ok &other)

{

out<<"该内容是:"<<other.ptr<<endl;

return out;

}

std::istream & operator>>(istream &in,My_string_Ok &other)

{

delete other.ptr;

other.ptr = new char [20];

in.getline(other.ptr,20);

//other.len = strlen(other.len);

return in;

}

mian()

#include "my_string_ok.h"

int main()

{

My_string_Ok s1(3,'b');

s1.show();

My_string_Ok s2("abcdefg");

s2.show();

My_string_Ok s3 (s1);

s3.show();

My_string_Ok s4;

s4 = s2;

s4.show();

My_string_Ok s5;

s5 = "ABCDEF";

s5.show();

s5+'G';

s5.show();

cout<<s5.data()[6]<<endl;

cout<<s5.get_len()<<"**"<<s1.get_len()<<endl;

if(s5>s1)

cout<<"s5>s1"<<endl;

else

cout<<"s5<s1"<<endl;

if(s1<s2)

cout<<"s2>s1"<<endl;

else

cout<<"s2<s1"<<endl;

My_string_Ok s6;

s6 = s2;

if(s6 == s2)

{

cout<<"s6=s2"<<endl;

}

else

{

cout<<"s6!=s2"<<endl;

}

cout<<s5.get_len()<<endl;

s6 += "123456";

s6.show();

s6 += 'X';

s6.show();

cout<<s1;

My_string_Ok s7;

cout<<"请输入值"<<endl;

cin>>s7;

cout<<s7;

cout<<s7.get_len()<<endl;

//cout << "Hello World!" << endl;

return 0;

}

相关推荐
Theodore_10223 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
网易独家音乐人Mike Zhou3 小时前
【卡尔曼滤波】数据预测Prediction观测器的理论推导及应用 C语言、Python实现(Kalman Filter)
c语言·python·单片机·物联网·算法·嵌入式·iot
‘’林花谢了春红‘’4 小时前
C++ list (链表)容器
c++·链表·list
----云烟----5 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024065 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
开心工作室_kaic5 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it5 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
武子康5 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神6 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
机器视觉知识推荐、就业指导6 小时前
C++设计模式:建造者模式(Builder) 房屋建造案例
c++