C++封装(9.24)

list.cpp

#include "list.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
MY_string::MY_string():size(15)
{

    this->ptr = new char[size];
    memset(ptr,'\0',size);
    this->len=0;
}
//有参构造
MY_string::MY_string(const char *src)
{
    this->len=strlen(src);
    this->size=strlen(src)+1;
    if(src)
    {
        this->ptr = new char[strlen(src)+1];
        strcpy(ptr,src);
    }
    else
    {
        this->ptr=new char[1];
        this->ptr[0]='\0';
    }

}

//析构函数
MY_string::~MY_string()
{
    delete[] ptr;
    cout<<"析构函数"<<this<<endl;
}

MY_string::MY_string(int num ,char value)
{
    if(num>0)
    {
        this->ptr=new char[num+1];
        this->len=num;
        this->size=num+1;
        for(int i=0;i<num;i++)
        {
            ptr[i]=value;
        }
    }
    else
    {
        printf("输入错误\n");
    }
}

//拷贝构造
MY_string::MY_string(const MY_string &other)
{
    delete[] ptr;                       // 深拷贝
    this->ptr=new char[other.size];
    this->len=sizeof(other)-1;
    this->size=sizeof(other);
    strcpy(ptr,other.ptr);
}

//拷贝赋值
 MY_string & MY_string::operator=(const MY_string &other)
{
    this->ptr=new char[other.size];
    strcpy(this->ptr,other.ptr);
    this->len=other.len;
    this->size=other.size;
    return *this;
}

//判空
void MY_string::MY_empty()
{
    if(this->len==this->size||this->size==1||*(this->ptr)=='\0')
    {
        cout<<"字符串为空"<<endl;
    }
}
void MY_string::MY_full()
{
    if(this->len==((this->size)-1))
    {
        cout<<"字符串已满"<<endl;
    }
}

//尾插    //君子函数
void MY_string::push_back(char p)
{


    if((len+1)%size==0||len+1==size)
    {
        (this->size)=size*2;
        char buff[this->size];
        strcpy(buff,this->ptr);
        delete[] this->ptr;
        this->ptr=new char[this->size];
        memset(this->ptr,'\0',this->size);
        strcpy(this->ptr,buff);
    }
    (this->len)++;
    this->ptr[(this->len)-1]=p;
    return;
}
//尾删
void MY_string::pop_back()
{
    (this->ptr)[(this->len)-1]='\0';
    this->size=(this->size)-1;
    this->len=(this->len)-1;

    return;
}

//at函数实现
char &MY_string::at(int index)
{
    cout<<ptr[index-1]<<endl;
    return ptr[index-1];
}

//清空函数
void MY_string::clear()
{
    len=0;
    memset(ptr,0,sizeof(size));
    return;
}

//返回C风格字符串
void *MY_string::data()
{
    return ptr;
}

//返回实际长度
int MY_string::get_length()
{
    return this->len;
}

//返回当前最大容量
int MY_string::get_size()
{
    return this->size;
}
//+
const MY_string MY_string::operator+(const MY_string &R)
{
    MY_string temp;
    temp.size=len+R.len+1;
    temp.ptr=new char [size] ;
    memset(temp.ptr,'\0',size);
    strcpy(temp.ptr,ptr);
    strcat(temp.ptr,R.ptr);
    temp.len=strlen(temp.ptr);
    return temp;
}
//[]
char &MY_string::operator[](int num)
{
    return ptr[num-1];
}
//>  <
bool  MY_string::operator>(const MY_string &R)const
{
    if(strcmp(ptr,R.ptr)>0)
        return 1;
    else
        return 0;

}

bool  MY_string::operator<(const MY_string &R)const
{
    if(strcmp(R.ptr,ptr)>0)
       return 1;
    else
        return 0;
}
//==
bool  MY_string::operator==(const MY_string &R)const
{
    if(strcmp(ptr,R.ptr)==0)
        return 1;
    else
        return 0;
}
//>=
bool  MY_string::operator>=(const MY_string &R)const
{
    if(strcmp(ptr,R.ptr)==0||strcmp(ptr,R.ptr)>0)
    {
    return 1;
    }
    else
    return 0;
}
//<=
bool  MY_string::operator<=(const MY_string &R)const
{
    if(strcmp(ptr,R.ptr)==0||strcmp(ptr,R.ptr)<0)
    {
    return 1;
    }
    else
    return 0;
}
//!=
bool  MY_string::operator!=(const MY_string &R)const
{
    if(strcmp(ptr,R.ptr)==0)
    {
    return 0;
    }
    else
        return 1;
}


//+=(可以加等于一个字符串,也可以单字符)
MY_string &MY_string::operator+=(const MY_string &R)
{
    char buff[size];
    strcpy(buff,ptr);
    len=strlen(buff)+R.len;
    size=len+1;
    delete[] ptr;
    ptr=new char [this->size];

    memset(ptr,0,size);
    strcpy(ptr,buff);
    strcat(ptr,R.ptr);
    ptr[len]='\0';
    return *this;
}
MY_string &MY_string::operator+=(const char &R )
{
    if((len+1)%size==0||len+1==size)
    {
        (this->size)=size*2;
        char buff[this->size];
        strcpy(buff,this->ptr);
        delete[] this->ptr;
        this->ptr=new char[this->size];
        memset(this->ptr,'\0',this->size);
        strcpy(this->ptr,buff);
    }
    ptr[strlen(ptr)]=R;
    cout<<strlen(ptr)<<endl;

    this->push_back(R);
    return *this;
}
//<<,>>输入输出
ostream & operator<<(ostream &L,const MY_string &R)
{
    for(int i=0;i<R.len;i++)
    {
        cout<<R.ptr[i];
    }
    cout<<endl;
    return L;
}

istream & operator>>(istream &L,MY_string &R)
{
    char buff[1024];
    L>>buff;
    R.len=strlen(buff);
    R.size=R.len+1;

    delete [] R.ptr;

    R.ptr=new char [R.size];

    memset(R.ptr,0,R.size);
    strcpy(R.ptr,buff);
    cout<<R.ptr<<endl;
    R.ptr[R.len]='\0';
    return L;
}

list.h 

#ifndef LIST_H
#define LIST_H
#include<iostream>
#include<cstring>
using namespace std;
class MY_string
{
public:
    char *ptr;
    int len;
    int size;

public:
#include "list.h"

MY_string();
//有参构造
MY_string(const char *src);
//析构函数
~MY_string();

MY_string(int num ,char value);

//拷贝构造
MY_string(const MY_string &other);
//拷贝赋值
MY_string & operator=(const MY_string &other);
//判空
void MY_empty();

void MY_full();

//尾插    //君子函数
void push_back(char p);
//尾删
void pop_back();
//at函数实现
char &at(int index);
//清空函数
void clear();
//返回C风格字符串
void *data();
//返回实际长度
int get_length();
//返回当前最大容量
int get_size() ;
//+
const MY_string operator+(const MY_string &R);
//[]
char &operator[](int num);
//>  <
bool  operator>(const MY_string &R)const;

bool  operator<(const MY_string &R)const;
//==
bool  operator==(const MY_string &R)const;
//>=
bool  operator>=(const MY_string &R)const;
//<=
bool  operator<=(const MY_string &R)const;
//!=
bool  operator!=(const MY_string &R)const;
//+=(可以加等于一个字符串,也可以单字符)
MY_string &operator+=(const MY_string &R);
MY_string &operator+=(const char &R );
//<<,>>输入输出
friend ostream & operator<<(ostream &L,const MY_string &R);

friend istream & operator>>(istream &L, MY_string &R);
};


#endif // LIST_H

main.c 

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"list.h"


int main()
{
    MY_string s1;
    MY_string s2;
    for(int i=0;i<5;i++)
    {
    s1+='A';
    s2+='B';
    }
    cout<<"s1 ="<<s1<<"  len="<<s1.len<<"  size="<<s1.size<<endl;
    cout<<"s2 ="<<s2<<"  len="<<s2.len<<"  size="<<s2.size<<endl;
    s1+=s2;
    cout<<"s1 ="<<s1<<endl;
//    MY_string s8;
//    cin>>s8;
//    cout<<"s8 ="<<s8<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值