当前位置: 首页 > article >正文

C++(9.24)

 

 头文件

#ifndef MY_STRING_H
#define MY_STRING_H

#include <iostream>

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

public:
    My_string();
    My_string(const char *src);
    My_string(const My_string &src);
    My_string& operator=(const My_string &src);
    ~My_string();

    void push_back(char value);
    void pop_back();
    char &at(int index);
    void clear();
    char *data() const;
    int get_length() const;
    int get_size() const;
    bool is_empty() const;

    // 运算符重载
    My_string operator+(const My_string &other) const;
    My_string operator+(char value) 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 std::ostream& operator<<(std::ostream &out, const My_string &str);
    friend std::istream& operator>>(std::istream &in, My_string &str);
};

#endif // MY_STRING_H

 源文件

#include "My_string.h"
#include <iostream>
#include <cstring>
#include <stdexcept> // 用于异常处理

using namespace std;

// 默认构造函数
My_string::My_string() : size(15), len(0)
{
    this->ptr = new char[size];
    this->ptr[0] = '\0';
}

// 带参数的构造函数
My_string::My_string(const char *src)
{
    this->len = strlen(src);
    this->size = this->len + 1;
    this->ptr = new char[size];
    strcpy(this->ptr, src);
}

// 拷贝构造函数
My_string::My_string(const My_string &src)
{
    this->len = src.len;
    this->size = src.size;
    this->ptr = new char[size];
    strcpy(this->ptr, src.ptr);
}

// 赋值运算符重载
My_string& My_string::operator=(const My_string &src)
{
    if (this == &src)
        return *this;

    delete[] this->ptr;
    this->len = src.len;
    this->size = src.size;
    this->ptr = new char[size];
    strcpy(this->ptr, src.ptr);
    return *this;
}

// 尾部插入
void My_string::push_back(char value)
{
    if (len + 1 >= size)
    {
        size *= 2;
        char *new_ptr = new char[size];
        strcpy(new_ptr, ptr);
        delete[] ptr;
        ptr = new_ptr;
    }
    ptr[len++] = value;
    ptr[len] = '\0';
}

// 尾部删除
void My_string::pop_back()
{
    if (len > 0)
    {
        len--;
        ptr[len] = '\0';
    }
}

// 通过索引访问字符
char &My_string::at(int index)
{
    if (index < 0 || index >= len)
    {
        throw out_of_range("Index out of range");
    }
    return ptr[index];
}

// 清空字符串
void My_string::clear()
{
    len = 0;
    ptr[0] = '\0';
}

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

// 返回字符串长度
int My_string::get_length() const
{
    return len;
}

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

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

// 运算符重载实现
My_string My_string::operator+(const My_string &other) const
{
    My_string result;
    result.size = len + other.len; 
    result.ptr = new char[result.size + 1];
    strcpy(result.ptr, ptr);        // 复制当前字符串
    strcat(result.ptr, other.ptr);  // 追加另一个字符串
    result.len = len + other.len;   // 更新长度
    return result;
}

My_string My_string::operator+(char value) const
{
    My_string result(*this); // 拷贝当前对象
    result.push_back(value); // 添加字符
    return result;
}

char &My_string::operator[](int index)
{
    return at(index); // 使用 at 函数检查边界
}

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)
{
    this->push_back('\0'); // 确保字符串末尾有结束符
    strcat(this->ptr, other.ptr); 
    len += other.len; // 更新长度
    return *this;
}

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

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

istream& operator>>(istream &in, My_string &str)
{
    char buffer[1024]; 
    in >> buffer;
    str.clear(); // 清空当前字符串
    str.push_back('\0'); // 先设置为结束符
    for (int i = 0; buffer[i] != '\0'; i++)
    {
        str.push_back(buffer[i]); 
    }
    return in;
}

// 析构函数
My_string::~My_string()
{
    delete ptr;
}

 主函数

#include "My_string.h"
#include <iostream>

using namespace std;

int main()
{
    My_string s1("hello");
    My_string s2(" world");

    
    My_string s3 = s1 + s2; // 字符串连接
    cout << "s3: " << s3 << endl; // 输出 "hello world"

    
    s1 += '!';
    cout << "s1 after +=: " << s1 << endl; // 输出 "hello!"

    // 测试 []
    cout << "s1[1]: " << s1[1] << endl; // 输出 'e'

    
    cout << "s1 > s2: " << (s1 > s2) << endl; // 输出 1 
    cout << "s1 < s2: " << (s1 < s2) << endl; // 输出 0 
    cout << "s1 == s2: " << (s1 == s2) << endl; // 输出 0 

    
    My_string s4;
    cout << "Enter a string: ";
    cin >> s4; // 输入字符串
    cout << "You entered: " << s4 << endl;

    return 0;
}


http://www.kler.cn/news/318906.html

相关文章:

  • Golang | Leetcode Golang题解之第420题强密码检验器
  • Android SystemUI组件(07)锁屏KeyguardViewMediator分析
  • echarts图表刷新
  • 与 CESS Network 共探去中心化创新:重塑数据基础设施,驱动未来变革
  • 数电学习基础(逻辑门电路+)
  • 羽毛球场馆预约系统,便捷管理预约
  • 【UE5】将2D切片图渲染为体积纹理,最终实现使用RT实时绘制体积纹理【第二篇-着色器制作】
  • 破解 oklink 网站加密数据(升级版)
  • Python中字典常用方法
  • Go版数据结构 -【序言】
  • 一,初始 MyBatis-Plus
  • 微信小程序公共样式:设计与实现指南
  • 智能听诊器宠物社区的新宠
  • Gnu Radio抓取WiFi信号,流程图中模块功能
  • 【Elasticsearch】-实现图片向量相似检索
  • 自然语言处理(NLP)实战项目
  • accelerate 分布式框架
  • C语言特殊字符串函数和字符函数
  • 从零开始学习Python
  • QT中各数据基础类型互转方式有哪些?
  • 面经宝典【1】-拼多多
  • [51单片机] 简单介绍 (一)
  • Vue ElemetUI table的行实现按住上下键高亮上下移动效果
  • Ansible部署openstack案例
  • MySQL表的增删改查
  • 如何修改音频的音量增益
  • 【算法】算法思想合集
  • Make breakpoint pending on future shared library load
  • mfc140u.dll引发的软件故障怎么破?mfc140u.dll文件损坏的解决办法全知道!
  • Linux 简易shell编写