9.24C++(day5)

头文件

#ifndef MY_STRING_H
#define MY_STRING_H

#include <iostream>
using namespace std;

class My_string {
private:
    char *ptr;         // 指向字符数组的指针
    int size;          // 字符串的最大容量
    int len;           // 字符串当前长度

public:
    // 无参构造
    My_string();

    // 有参构造
    My_string(const char* src);
    // 有参构造:根据字符和数量创建字符串
    My_string(int num, char value);
    // 拷贝构造
    My_string(const My_string& other);
    // 拷贝赋值
    My_string& operator=(const My_string& other);
    // 析构函数
    ~My_string();
    // 判空
    bool is_empty() const;
    // 尾插
    void push_back(char value);
    // 尾删
    void pop_back();
    // at函数实现
    char& at(int index);
    // 清空函数
    void clear();
    // 返回C风格字符串
    char* data();
    // 返回实际长度
    int get_length()const;
    // 返回当前最大容量
    int get_size() const;
    // 扩容函数:二倍扩容
    void resize(int new_size);
    //展示函数
    void show();
    My_string operator+(const My_string& other) const;
    char& operator[](int index);

    bool operator>(const My_string& other) const;

    bool operator<(const My_string& other) const;
    bool operator==(const My_string& other) const;

    bool operator>=(const My_string& other) const;

    bool operator<=(const My_string& other) const;

    bool operator!=(const My_string& other) const;

    My_string& operator+=(const My_string& other);

    My_string& operator+=(char value);
        // 输入输出运算符重载
    friend ostream& operator<<(ostream& os, const My_string& str);

    friend istream& operator>>(istream& is, My_string& str);
};
#endif // MY_STRING_H

源文件

#include "my_string.h"
#include <cstring>

    // 无参构造
    My_string::My_string() : size(15), len(0) {
        this->ptr = new char[size];
        this->ptr[0] = '\0'; // 表示串为空串
    }

    // 有参构造
    My_string::My_string(const char* src) {
        len = strlen(src);
        size = len + 1; // +1 for null terminator
        ptr = new char[size];
        strcpy(ptr, src);
        ptr[len] = '\0'; // 添加结束符
    }

    // 有参构造:根据字符和数量创建字符串
    My_string::My_string(int num, char value) {
        size = num + 1;
        len = num;
        ptr = new char[size];
        for (int i = 0; i < num; ++i) {
            ptr[i] = value;
        }
        ptr[len] = '\0'; // 添加结束符
    }

    // 拷贝构造
    My_string::My_string(const My_string& other) {
        size = other.size;
        len = other.len;
        ptr = new char[size];
        memcpy(ptr, other.ptr, len);
        ptr[len] = '\0'; // 添加结束符
    }

    // 拷贝赋值
    My_string & My_string::operator=(const My_string& other) {
        if (this != &other) {
            delete[] ptr; // 释放原有内存
            size = other.size;
            len = other.len;
            ptr = new char[size];
            memcpy(ptr, other.ptr, len);
            ptr[len] = '\0'; // 添加结束符
        }
        return *this;
    }

    // 析构函数
    My_string::~My_string() {
        delete[] ptr; // 释放分配的内存
        cout<<"My_string::析构函数,this = "<<this<<endl;
    }

    // 判空
    bool My_string::is_empty() const {
        return len == 0;
    }

    // 尾插
    void My_string::push_back(char value) {
        size = this->size+1;
        ptr[len] = value;
        len++;
        ptr[len] = '\0'; // 添加结束符
    }

    // 尾删
    void My_string::pop_back() {
        size = this->size-1;
        if (len > 0) {
            len--;
            ptr[len] = '\0'; // 更新结束符
        }
    }

    // at函数实现
    char& My_string::at(int index) {
        if (index < 0 || index >= len) {
            cout<<"失败"<<endl;
        }
        return ptr[index];
    }

    // 清空函数
    void My_string::clear() {
        len = 0;
        ptr[0] = '\0'; // 重新设置为空串
    }

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

    // 返回实际长度
    int My_string::get_length() const {
        return len;
    }

    // 返回当前最大容量
    int My_string::get_size() const {
        return size;
    }


    // 扩容函数:二倍扩容
    void My_string::resize(int new_size) {
        char* new_ptr = new char[new_size];
        memcpy(new_ptr, ptr, len);
        new_ptr[len] = '\0'; // 添加结束符
        delete[] ptr; // 释放旧内存
        ptr = new_ptr; // 更新指针
        size = new_size; // 更新大小
    }
    //展示函数
    void My_string::show()
    {
        cout<<"ptr = "<<ptr<<endl;
        cout<<"size = "<<size<<endl;
        cout<<"len = "<<len<<endl;
    }

    My_string My_string::operator+(const My_string& other) const {
        My_string temp;
        temp.ptr = strcat(this->ptr,other.ptr);
        temp.len = this->len + other.len; // 更新长度
        temp.ptr[temp.len] = '\0';
        return temp;
    }

    char& My_string::operator[](int index) {
        return at(index);
    }

    bool My_string::operator>(const My_string& other) const {
        return strcmp(ptr, other.ptr) > 0; // 字符串比较
    }

    bool My_string::operator<(const My_string& other) const {
        return strcmp(ptr, other.ptr) < 0; // 字符串比较
    }
    bool My_string::operator==(const My_string& other) const {
        return strcmp(ptr, other.ptr) == 0; // 字符串比较
    }

    bool My_string::operator>=(const My_string& other) const {
        return !(*this < other); // 使用 < 运算符
    }

    bool My_string::operator<=(const My_string& other) const {
        return !(*this > other); // 使用 > 运算符
    }

    bool My_string::operator!=(const My_string& other) const {
        return !(*this == other); // 使用 == 运算符
    }

    My_string& My_string::operator+=(const My_string& other) {
        push_back('\0');
        for (int i = 0; i < other.len; ++i) {
            push_back(other.ptr[i]);
        }
        return *this;
    }

    My_string& My_string::operator+=(char value) {
        push_back(value); // 尾插字符
        return *this;
    }

        // 输入输出运算符重载
    ostream & operator<<(ostream& os, const My_string& str) {
        os << str.ptr; // 输出字符串
        return os;
    }

    istream& operator>>(istream& is, My_string& str) {
        char buffer[1000]; // 假设最大输入长度为999
        is >> buffer; // 读取输入
        str = My_string(buffer); // 使用有参构造
        return is;
    }

主程序

#include <iostream>
#include <cstring>

#include "my_string.h"
int main() {
    My_string s1;
    s1.show();
    if(s1.is_empty())
    {
        cout<<"yes"<<endl;
    }else
    {
        cout<<"no"<<endl;
    }
    My_string s2("HelloWorld");
    s2.show();
    My_string s3(4,'A');
    s3.show();
    My_string s4 = s2;
    s4.show();
    s1 = s3;
    s1.show();
    if(s1.is_empty())
    {
        cout<<"yes"<<endl;
    }else
    {
        cout<<"no"<<endl;
    }
    s1.push_back('W');
    s1.show();
    s1.pop_back();
    s1.show();
    cout <<s1.at(2)<<endl;
    cout<<s1+s2<<endl;


    cout<<"***********************************************"<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值