9.23作业

仿照string类,自己手动实现 My_string

代码如下

MyString.h
cpp 复制代码
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <cstring>

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 Isvoid();
    //显示
    void show();
    //尾插
    void push_back(char value);
    //尾删
    void pop_back();
    //at函数实现
    char &at(int index);
    //清空函数
    void clear();
    //返回C风格字符串
    char *data();
    //返回实际长度
    int get_length();
    //返回当前最大容量
    int get_size();


    //君子函数:二倍扩容
    bool Add();

};
#endif // MYSTRING_H
MyString.cpp
cpp 复制代码
#include "MyString.h"

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

My_string::My_string(const char* src) : size(15) {
    ptr = new char[size];
    strcpy(ptr, src); // 复制字符串
    len = strlen(src);
    cout << "一个形参的有参构造" << endl;
}

My_string::My_string(int num, char value) : size(15), len(num) {
    if (num > 15) {
        cout << "超出默认长度" << endl;
        return;
    }
    ptr = new char[size];
    for (int i = 0; i < num; i++) {
        ptr[i] = value;
    }
    ptr[num] = '\0'; // 确保字符串以'\0'结尾
    cout << "部分形参的有参构造" << endl;
}

My_string::My_string(const My_string &other) : size(other.size), len(other.len) {
    ptr = new char[size];
    strcpy(ptr, other.ptr); // 复制字符串
    cout << "拷贝构造" << endl;
}

My_string& My_string::operator= (const My_string &other) {
    if (this != &other) {
        delete[] ptr; // 释放旧内存
        size = other.size;
        len = other.len;
        ptr = new char[size];
        strcpy(ptr, other.ptr); // 复制字符串
    }
    cout << "拷贝赋值" << endl;
    return *this;
}

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

bool My_string::Isvoid() {
    return len == 0;
}

void My_string::show() {
    cout << ptr << endl;
}

void My_string::push_back(char value) {
    if (len < size - 1) {
        ptr[len++] = value;
        ptr[len] = '\0'; // 确保字符串以'\0'结尾
    } else if (Add()) {
        ptr[len++] = value;
        ptr[len] = '\0'; // 确保字符串以'\0'结尾
    }
}

void My_string::pop_back() {
    if (len > 0) {
        len--;
        ptr[len] = '\0'; // 确保字符串以'\0'结尾
    }
}

char& My_string::at(int index) {
    if (index < len) {
        return ptr[index];
    } else {
        cout << "下标越界" << endl;
        exit(EXIT_FAILURE);
    }
}

void My_string::clear() {
    len = 0;
    ptr[0] = '\0'; // 确保字符串以'\0'结尾
}

char* My_string::data() {
    return ptr;
}

int My_string::get_length() {
    return len;
}

int My_string::get_size() {
    return size;
}

bool My_string::Add() {
    if (len == size - 1) {
        char *p = new char[size * 2];
        strcpy(p, ptr);
        delete[] ptr; // 释放旧内存
        ptr = p;
        size *= 2; // 更新容量
        return true;
    }
    return false;
}
main.cpp
cpp 复制代码
#include "MyString.h"

int main() {
    My_string s;
    cout << "s:";
    s.show();

    My_string s1("hello");
    cout << "s1:";
    s1.show();

    My_string s2(5, 'A');
    cout << "s2:";
    s2.show();

    My_string s3 = s2;
    cout << "s3:";
    s3.show();

    s3 = s1;
    cout << "s3:";
    s3.show();

    if (s3.Isvoid()) {
        cout << "s3空" << endl;
    } else {
        cout << "s3非空" << endl;
    }

    cout << "尾插:";
    s3.push_back('a');
    s3.show();

    cout << "尾删:";
    s3.pop_back();
    s3.show();

    cout << "查看下标4的值:";
    if (s3.get_length() > 4) {
        cout << s3.at(4) << endl;
    } else {
        cout << "下标越界" << endl;
    }

    cout << "清空s3函数" << endl;
    s3.clear();
    cout << "s3:";
    s3.show();

    cout << "s1的C风格字符串:" << s1.data() << endl;
    cout << "s1的实际长度:" << s1.get_length() << endl;
    cout << "s1当前最大容量:" << s1.get_size() << endl;

    cout << "s3的二倍扩容" << endl;
    cout << "请输入一串字符串:";
    char c = ' ';
    while (true) {
        cin >> c;
        if (c == '#') break; // 直接在这里判断并退出循环
        s3.push_back(c);
    }
    s3.show();

    return 0;
}

运行结果

思维导图

相关推荐
cpp_25011 分钟前
P1877 [HAOI2012] 音量调节
数据结构·c++·算法·动态规划·题解·洛谷·背包dp
dragen_light2 分钟前
1.ROS2-Install
c++·python·ros
Gary Studio9 分钟前
基于PMSM理论研究加实践
算法
AI人工智能+电脑小能手11 分钟前
【大白话说Java面试题】【Java基础篇】第9题:HashMap根据key查询元素的时间复杂度是多少
java·开发语言·数据结构·后端·面试·哈希算法·哈希表
不知名的老吴12 分钟前
编程初体验之句柄的概念及使用示例
c++
invicinble14 分钟前
对于java面向对象的知识
java·开发语言
2501_9307077814 分钟前
使用C#代码在 PowerPoint 中创建组合图表
开发语言·c#·powerpoint
木子墨51616 分钟前
LeetCode 热题 100 精讲 | 矩阵与图论进阶篇:矩阵置零 · 螺旋矩阵 · 旋转图像 · 搜索二维矩阵 II · 岛屿数量 · 腐烂的橘子
c++·算法·leetcode·矩阵·力扣·图论
干洋芋果果16 分钟前
前端学python
开发语言·前端·python
Ailan_Anjuxi18 分钟前
【附jupyter源码】使用长短期记忆网络(LSTM)实现一个小说写作AI——以训练《西游记》为例
人工智能·算法