C++day05

头文件

#ifndef MYSTRING_H
#define MYSTRING_H
 
#include <iostream>
 
class mystring {
private:
    char *ptr;  // 动态分配的字符数组指针
    int size;   // 当前的容量
    int len;    // 实际字符串长度
 
public:
    // 默认构造函数,初始化为空字符串,容量为15
    mystring();
 
    // 使用 C 风格字符串初始化
    mystring(const char *src);
 
    // 使用指定字符重复初始化字符串
    mystring(int num, char value);
 
    // 拷贝构造函数,深拷贝
    mystring(const mystring &src);
 
    // 拷贝赋值运算符,深拷贝
    mystring& operator=(const mystring &src);
 
    // 析构函数,释放动态分配的内存
    ~mystring();
 
    // 判空函数,检查字符串是否为空
    bool empty() const;
 
    // 向字符串末尾添加一个字符
    void push_back(char value);
 
    // 删除字符串末尾的一个字符
    void pop_back();
 
    // 获取指定位置的字符,带边界检查
    char &at(int index);
 
    // 清空字符串
    void clear();
 
    // 返回 C 风格字符串
    char *data();
 
    // 返回字符串的实际长度
    int get_length() const;
 
    // 返回当前最大容量
    int get_size() const;
 
    // 重载加法运算符
    mystring operator+(const mystring &other) const;
 
    // 重载下标运算符
    char& operator[](int index);
 
    // 重载比较运算符
    bool operator>(const mystring &other) const;
    bool operator<(const mystring &other) const;
    bool operator==(const mystring &other) const;
    bool operator>=(const mystring &other) const;
    bool operator<=(const mystring &other) const;
    bool operator!=(const mystring &other) const;
 
    // 重载加等运算符
    mystring& operator+=(const mystring &other);
    mystring& operator+=(char value);
 
};
 
#endif

源文件

#include "mystring.h"
#include <cstring> // 包含 C 风格字符串操作的库
 
// 默认构造函数
mystring::mystring() : size(15), len(0) {
    ptr = new char[size];
    ptr[0] = '\0';
}
 
// 使用 C 风格字符串初始化
mystring::mystring(const char *src) {
    len = strlen(src);
    size = len + 1;
    ptr = new char[size];
    strcpy(ptr, src);
}
 
// 使用指定字符重复初始化字符串
mystring::mystring(int num, char value) {
    size = num + 1;
    len = num;
    ptr = new char[size];
    memset(ptr, value, num);
    ptr[len] = '\0';
}
 
// 拷贝构造函数
mystring::mystring(const mystring &src) {
    len = src.len;
    size = src.size;
    ptr = new char[size];
    strcpy(ptr, src.ptr);
}
 
// 拷贝赋值运算符
mystring& mystring::operator=(const mystring &src) {
    if (this == &src) return *this;
 
    delete[] ptr;
 
    len = src.len;
    size = src.size;
    ptr = new char[size];
    strcpy(ptr, src.ptr);
 
    return *this;
}
 
// 析构函数
mystring::~mystring() 
{
    delete[] ptr;
}
 
// 判空函数
bool mystring::empty() const 
{
    return len == 0;
}
 
// 向字符串末尾添加一个字符
void mystring::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 mystring::pop_back() 
{
    if (len > 0) 
    {
        ptr[--len] = '\0';
    }
}
 
// 获取指定位置的字符,带边界检查
char &mystring::at(int index) 
{
    if (index < 0 || index >= len) 
    {
        throw std::out_of_range("Index out of range");
    }
    return ptr[index];
}
 
// 清空字符串
void mystring::clear() 
{
    len = 0;
    ptr[0] = '\0';
}
 
// 返回 C 风格字符串
char *mystring::data() 
{
    return ptr;
}
 
// 返回字符串的实际长度
int mystring::get_length() const 
{
    return len;
}
 
// 返回当前最大容量
int mystring::get_size() const 
{
    return size;
}
 
// 重载加法运算符
mystring mystring::operator+(const mystring &other) const 
{
    mystring r;
    r.size = len + other.len + 1; // 为结束符 '\0' 预留空间
    r.ptr = new char[r.size];
    strcpy(r.ptr, ptr);
    strcat(r.ptr, other.ptr);
    r.len = len + other.len;
    return r;
}
 
// 重载下标运算符
char& mystring::operator[](int index) 
{
    if (index < 0 || index >= len) 
    {
        throw std::out_of_range("Index out of range");
    }
    return ptr[index];
}
 
// 重载比较运算符
bool mystring::operator>(const mystring &other) const 
 
    return strcmp(ptr, other.ptr) > 0;
}
 
bool mystring::operator<(const mystring &other) const 
{
    return strcmp(ptr, other.ptr) < 0;
}
 
bool mystring::operator==(const mystring &other) const 
{
    return strcmp(ptr, other.ptr) == 0;
}
 
bool mystring::operator>=(const mystring &other) const 
{
    return strcmp(ptr, other.ptr) >= 0;
}
 
bool mystring::operator<=(const mystring &other) const 
{
    return strcmp(ptr, other.ptr) <= 0;
}
 
bool mystring::operator!=(const mystring &other) const 
{
    return strcmp(ptr, other.ptr) != 0;
}
 
// 重载加等运算符
mystring& mystring::operator+=(const mystring &other) 
{
    this->push_back('\0'); // 预留空间
    for (int i = 0; i < other.len; i++) 
    {
        this->push_back(other.ptr[i]);
    }
    return *this;
}
 
mystring& mystring::operator+= (const mystring &R)
    {
        this->real += R.real;
        this->vir += R.vir;
 
 
        return *this;
    }

主文件

#include "mystring.h"
#include <iostream>
 
using namespace std;
 
int main() {
    // 测试无参构造函数
    mystring s1;
    cout << "s1 (空字符串): " << s1.data() << endl;
 
    // 测试有参构造函数(字符串初始化)
    mystring s2("hello");
    cout << "s2 (初始化为'hello'): " << s2.data() << endl;
 
    // 测试有参构造函数(重复字符初始化)
    mystring s3(5, 'a');
    cout << "s3 (5个a): " << s3.data() << endl;
 
    // 测试push_back和pop_back
    s1.push_back('b');
    cout << "s1 after push_back('b'): " << s1.data() << endl;
 
    s1.pop_back();
    cout << "s1 after pop_back(): " << s1.data() << endl;
 
    // 测试at函数
    try
    {
        cout << "s2 at(1): " << s2.at(1) << endl;
    }
    catch (const out_of_range &e)
    {
        cout << e.what() << endl;
    }
 
    // 测试拷贝构造
    mystring s4 = s2;
    cout << "s4 (拷贝构造): " << s4.data() << endl;
 
    // 测试拷贝赋值
    s4 = s3;
    cout << "s4 (拷贝赋值s3): " << s4.data() << endl;
 
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值