9.24每日作业

news2024/9/24 22:05:21

1> 思维导图

2> 将昨天的My_string类中的所有能重载的运算符全部进行重载

+、[] 、>、=、>)

3> 仿照stack类实现my_stack,实现一个栈的操作

text.h

#ifndef LIST_H
#define LIST_H
#include <iostream>
#include <string.h>

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

    // 重载加法运算符
    My_string operator+(const My_string& other);
    // 重载下标运算符
    char& operator[](size_t index);
    // 重载大于运算符
    bool operator>(const My_string& other);
    // 重载小于运算符
    bool operator<(const My_string& other);
    // 重载等于运算符
    bool operator==(const My_string& other);
    // 重载大于等于运算符
    bool operator>=(const My_string& other);
    // 重载小于等于运算符
    bool operator<=(const My_string& other);
    // 重载不等于运算符
    bool operator!=(const My_string& other);
    // 重载加等运算符
    My_string& operator+=(const My_string& other);
    // 重载加等运算符(字符)
    My_string& operator+=(char c);
    // 重载输出运算符
    friend ostream& operator<<(ostream& os, const My_string& myStr);
    // 重载输入运算符
    friend istream& operator>>(istream& is, My_string& myStr);
};
#endif // LIST_H

text.c

#include "text.h"
using namespace std;
My_string::My_string():size(15)
{
    this->ptr = new char[15];
    this->ptr[0] = '\0';
    this->len = 0;
}
//有参构造
My_string::My_string(const char * src)
{
    this->len = 0;
    for(int i=0;src[i] != 0;i++)
    {
        this->len++;
    }
    this->size = this->len+1;
    this->ptr = new char[this->size];
    for(int i = 0;src[i] != 0;i++)
    {
        this->ptr[i] = src[i];
    }
}
My_string::My_string(int num, char value)
{
    this->len = num;
    this->size = this->len+1;
    this->ptr = new char[this->size];
    for (int i = 0; i < this->len; i++)
    {
        this->ptr[i] = value; // 用value填充数组
    }
    this->ptr[this->len] = '\0'; // 添加结束符
}
//拷贝构造
My_string::My_string(const My_string& other):ptr(new char[other.len + 1]),size(other.size), len(other.len)
{
    for(int i = 0;other.ptr[i] != 0;i++) // 复制字符串内容
    {
        this->ptr[i] = other.ptr[i];
    }
}
//拷贝赋值
My_string & My_string::operator= (const My_string &other)
{
    if(this != &other)               //防止自己给自己赋值
    {
        this->size = other.size;
        this->len = other.len;
        //this->ptr = other.ptr;                 //浅拷贝
        for(int i = 0;other.ptr[i] != 0;i++)     //深拷贝
        {
            this->ptr[i] = other.ptr[i];
        }
    }
    return *this;                  //返回值自身的引用
}
//析构构造
My_string::~My_string()
{
    delete[] ptr;     //需要显性定义析构函数,在析构函数的函数体内释放堆区空间
    cout<<"Person::析构函数,this = "<<this<<endl;
}
//判空
bool My_string::MYempty() const
{
    return this->ptr == nullptr || this->len == 0; // 如果指针为空或长度为0,则认为是空
}
//尾插
void My_string::push_back(char value)
{

    this->size++;
    char *Newptr=new char[this->size];
    for(int i = 0;this->ptr[i] != 0;i++)     //深拷贝
    {
        Newptr[i] = this->ptr[i];
    }
    delete[] this->ptr;
    this->ptr=Newptr;
    this->ptr[this->len]=value;
    this->len++;
    this->ptr[this->len]='\0';

}
//尾删
void My_string::pop_back()
{
    this->ptr[len-1] = 0;
    this->size--;
    this->len--;
}
//at函数实现
char & My_string::at(int index)
{
    static char num;
    num = this->ptr[index-1];
    return num;
}
//清空函数
void My_string::clear()
{
    this->size = 15;
    delete[] ptr;
    this->ptr = new char[1];
    this->ptr[0] = '\0';
    this->len = 0;
}
//返回C风格字符串
char *My_string::data()
{
    return ptr;
}
//返回实际长度
int My_string::get_length()
{
    return this->len;
}
//返回当前最大容量
int My_string::get_size()
{
    return this->size;
}
//君子函数:二倍扩容
void My_string::resize()
{
    size *= 2; // 容量翻倍
    char* newptr = new char[size]; // 分配新的内存
    for(int i = 0;this->ptr[i] != 0;i++)     //深拷贝
    {
        newptr[i] = this->ptr[i];
    }
    delete[] ptr; // 释放旧内存
    this->ptr = newptr; // 更新指针
}
void My_string::show()
{
    cout<< "ptr = " << this->ptr << endl;
    cout<< "size = " << this->size << endl;
    cout<< "len = " << this->len << endl;
}



// 重载加法运算符
My_string My_string::operator+(const My_string& other)
{
    My_string result;
    result.size = len + other.len + 1;
    result.ptr = new char[result.size];
    strcpy(result.ptr, ptr);
    strcat(result.ptr, other.ptr);
    result.len = len + other.len;
    return result;
}

// 重载下标运算符
char& My_string::operator[](size_t index)
{
    return at(index);
}

// 重载大于运算符
bool My_string::operator>(const My_string& other)
{
    return strcmp(ptr, other.ptr) > 0;
}

// 重载小于运算符
bool My_string::operator<(const My_string& other)
{
    return strcmp(ptr, other.ptr) < 0;
}

// 重载等于运算符
bool My_string::operator==(const My_string& other)
{
    return strcmp(ptr, other.ptr) == 0;
}

// 重载大于等于运算符
bool My_string::operator>=(const My_string& other)
{
    return !(*this < other);
}

// 重载小于等于运算符
bool My_string::operator<=(const My_string& other)
{
    return !(*this > other);
}

// 重载不等于运算符
bool My_string::operator!=(const My_string& other)
{
    return !(*this == other);
}

// 重载加等运算符
My_string& My_string::operator+=(const My_string& other)
{
    if (len + other.len >= size)
    {
        resize();
    }
    strcat(ptr, other.ptr);
    len += other.len;
    return *this;
}

// 重载加等运算符(字符)
My_string& My_string::operator+=(char c)
{
    push_back(c);
    return *this;
}

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

// 重载输入运算符
istream& operator>>(istream& is, My_string& myStr)
{
    char buffer[1000]; // 假设最大输入长度为999
    is >> buffer;
    myStr.clear(); // 清空当前字符串
    myStr.size = strlen(buffer) + 1;
    myStr.ptr = new char[myStr.size];
    strcpy(myStr.ptr, buffer);
    myStr.len = strlen(buffer);
    return is;
}

main.c

#include "text.h"

using namespace std;

int main()
{
    My_string s;
    s.show();
    if(s.MYempty()==1)
    {
        cout << "s" << "为空" << endl;
    }
    My_string s1("hello world");
    s1.show();

    My_string s2(5,'A');
    s2.show();

    My_string s3(s1);
    s3.show();
    s.operator=(s2);
    s.show();

    My_string s4 = s1;
    s4.push_back('B');
    s4.show();

    s4.pop_back();
    s4.show();

    cout << s4.at(3) << endl;

    s4.clear();
    if(s4.MYempty()==1)
    {
        cout << "s4" << "为空" << endl;
    }

    s3.data();
    s3.show();

    cout << s2.get_length() << endl;
    cout << s2.get_size() << endl;

    s1.resize();
    s1.show();

    My_string str1("hello");
    My_string str2(" world");

    str1.show();
    str2.show();

    My_string str3 = str1 + str2;
    str3.show();

    cout << "str3[1]: " << str3[1] << endl;

    if (s1 < str3)
    {
        cout << "s1 < str3" << endl;
    }
    else if(s1 == str3)
    {
        cout << "s1 = str3" <<endl;
    }
    else if(s1 > str3)
    {
        cout << "s1 > str3" <<endl;
    }
    s1.show();
    str3.show();
    str1 += '!';
    str1.show();

    My_string str4;
    cout << "请输入: ";
    cin >> str4;
    str4.show();

    return 0;
}

2、

main.c

#include <iostream>
using namespace std;


class my_stack
{
private:
    char* data;        // 存储栈元素的动态数组
    int capacity;   // 栈的容量
    int top;        // 栈顶元素的索引

public:
    // 构造函数
    my_stack(int size = 10) : capacity(size), top(-1)
    {
        data = new char[capacity];
    }

    // 析构函数
    ~my_stack()
    {
        delete[] data;
    }

    // 入栈操作
    void push(const char& value)
    {
        if (top + 1 >= capacity)
        {
            resize();
        }
        data[++top] = value;
    }

    // 出栈操作
    void pop()
    {
        if (!empty())
        {
            --top;
        }
        else
        {
            cout << "Stack is empty. Cannot pop." << endl;
        }
    }

    // 查看栈顶元素
    char& peek()
    {
        if (!empty())
        {
            return data[top];
        }
        else
        {
            throw out_of_range("Stack is empty. Cannot peek.");
        }
    }

    // 检查栈是否为空
    bool empty() const
    {
        return top == -1;
    }

    // 获取栈的大小
    int size() const
    {
        return top + 1;
    }

private:
    // 扩容函数
    void resize()
    {
        capacity *= 2;
        char* newData = new char[capacity];
        for (int i = 0; i <= top; ++i)
        {
            newData[i] = data[i];
        }
        delete[] data;
        data = newData;
    }
};

// 主函数示例
int main()
{
    my_stack stack;

    // 入栈操作
    stack.push(10);
    stack.push(20);
    stack.push(30);

    cout << "栈顶元素:"<< stack.peek() << endl; // 输出: 30
    cout << "栈大小:" << stack.size() << endl;  // 输出: 3

    // 出栈操作
    stack.pop();
    cout << "弹出操作后的顶部元素:" << stack.peek() << endl; // 输出: 20

    // 检查栈是否为空
    stack.pop();
    stack.pop();
    cout << "栈是否为空 " << (stack.empty() ? "是" : "否") << endl; 

    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2161574.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Java项目: 基于SpringBoot+mybatis+maven医疗病历交互系统(含源码+数据库+毕业论文)

一、项目简介 本项目是一套基于SpringBootmybatismaven医疗病历交互系统 包含&#xff1a;项目源码、数据库脚本等&#xff0c;该项目附带全部源码可作为毕设使用。 项目都经过严格调试&#xff0c;eclipse或者idea 确保可以运行&#xff01; 该系统功能完善、界面美观、操作简…

力扣 简单 206.反转链表

文章目录 题目介绍题解 题目介绍 题解 法一&#xff1a;双指针 在遍历链表时&#xff0c;将当前节点的 next 改为指向前一个节点。由于节点没有引用其前一个节点&#xff0c;因此必须事先存储其前一个节点。在更改引用之前&#xff0c;还需要存储后一个节点。最后返回新的头引…

不靠学历,不拼年资,怎么才能月入2W?

之前统计局发布了《2023年城镇单位就业人员年平均工资情况》&#xff0c;2023年全国城镇非私营单位和私营单位就业人员年平均工资分别为120698元和68340元。也就是说在去年非私营单位就业人员平均月薪1W&#xff0c;而私营单位就业人员平均月薪只有5.7K左右。 图源&#xff1a;…

1. 值、类型与运算符

在计算机的世界里&#xff0c;只有数据。你可以读取数据、修改数据、创建新数据&#xff0c;但不能提及非数据的内容。所有这些数据都存储为长位序列&#xff0c;因此本质上是相似的。 位&#xff08;比特&#xff09;是任何类型的二值事物&#xff0c;通常被描述为0和1。在计算…

Maya学习笔记:软选择

文章目录 打开软选择调整软选择范围衰减模式 软选择可以很好的进行渐变修改 打开软选择 方法1&#xff1a; 进入点线面模式&#xff0c;按B键进入软选择模式&#xff0c;再按B取消 方法2&#xff1a;双击左侧的选择按钮打开选择面板&#xff0c;勾选软选择 调整软选择范围 …

hadoop大数据平台操作笔记(上)

Hadoop介绍 Hadoop是一个开源的分布式系统框架&#xff0c;专为处理和分析大规模数据而设计。它由Apache基金会开发&#xff0c;并通过其高可靠性、高扩展性、高效性和高容错性等特性&#xff0c;在大数据领域发挥着重要作用。以下是对Hadoop的详细解释及其用途的概述&#xf…

分享6个icon在线生成网站,支持AI生成

在这个数字化的时代&#xff0c;创意和视觉标识在产品推广中可谓是愈发重要。提到图标&#xff0c;我们就不能不聊聊“Icon”这个小家伙。它不仅仅是个简单的视觉元素&#xff0c;简直是品牌信息的超级传递者。因此&#xff0c;图标生成器成了设计界的“万金油”&#xff0c;帮…

【赵渝强老师】K8s的DaemonSets控制器

DaemonSet控制器相当于在节点上启动了一个守护进程。通过使用DaemonSet可以确保一个Pod的副本运行在 Node节点上。如果有新的Node节点加入集群&#xff0c;DaemonSet也会自动给新加入的节点增加一个Pod的副本&#xff1b;反之&#xff0c;当有Node节点从集群中移除时&#xff0…

1.4 边界值分析法

欢迎大家订阅【软件测试】 专栏&#xff0c;开启你的软件测试学习之旅&#xff01; 文章目录 前言1 定义2 选取3 具体步骤4 案例分析 本篇文章参考黑马程序员 前言 边界值分析法是一种广泛应用于软件测试中的技术&#xff0c;旨在识别输入值范围内的潜在缺陷。本文将详细探讨…

一.python入门

gyp的读研日记&#xff0c;哈哈哈哈&#xff0c;&#x1f642;&#xff0c;从复习python开始&#xff0c; 目录 1.python入门 1.1 Python说明书 1.2 Python具备的功能 1.3 学习前提 1.4 何为Python 1.5 编程语言 2.Python环境搭建 2.1 开发环境概述 2.2 Python的安装与…

离线一机一码验证和网络验证的区别以及使用场景

本文主要介绍离线一机一码验证和网络验证的区别及其各自的特点和优势。通过对比这两种验证方式的工作原理、优缺点&#xff0c;使开发人员更好地理解并选择适合自己需求的验证方案。接下来&#xff0c;我们将详细探讨每种验证方式的具体实现和应用场景。 离线一机一码验证 和 网…

告别旋转手机:SLAM过程中的磁力计标定

1.论文信息 论文标题&#xff1a;Saying goodbyes to rotating your phone: Magnetometer calibration during SLAM 作者&#xff1a;Ilari Vallivaara, Yinhuan Dong, Tughrul Arslan 作者单位&#xff1a;爱丁堡大学 论文链接&#xff1a;https://arxiv.org/pdf/2409.0124…

虚拟机环境控制中心显示未激活问题

统信服务器操作系统【免费授权版】虚拟机环境控制中心显示未激活问题解决方案 文章目录 问题现象问题原因解决方案相关建议 问题现象 正常情况下服务器免费授权版系统中应该会显示“免费授权”&#xff0c;并且不会出现未激活的提示。 问题现场发现系统显示未激活&#xff0c…

船只类型识别系统源码分享

船只类型识别检测系统源码分享 [一条龙教学YOLOV8标注好的数据集一键训练_70全套改进创新点发刊_Web前端展示] 1.研究背景与意义 项目参考AAAI Association for the Advancement of Artificial Intelligence 项目来源AACV Association for the Advancement of Computer Vis…

ubuntu安装SFML库+QT使用SFML库播放声音

(1)ubuntu安装SFML库 sudo apt-get install libsfml-dev (2)QT使用SFML库播放声音 在.pro文件中添加头文件路径和库文件路径 INCLUDEPATH /usr/include/SFML LIBS /usr/lib/x86_64-linux-gnu/libsfml*.so UI界面中创建一个pushbutton按钮&#xff0c;并且创建槽函数 加载…

大数据新视界 --大数据大厂之大数据存储技术大比拼:选择最适合你的方案

&#x1f496;&#x1f496;&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎你们来到 青云交的博客&#xff01;能与你们在此邂逅&#xff0c;我满心欢喜&#xff0c;深感无比荣幸。在这个瞬息万变的时代&#xff0c;我们每个人都在苦苦追寻一处能让心灵安然栖息的港湾。而 我的…

企业如何通过加密软件保护敏感信息和客户数据?

1、数据加密&#xff1a;采用先进的加密算法&#xff0c;如AES-256&#xff0c;对敏感信息和客户数据进行加密处理。这样&#xff0c;即使数据被非法获取&#xff0c;未授权人员也无法解密和访问数据内容。 访问控制&#xff1a;设置严格的访问控制策略&#xff0c;确保只有授…

视频汇聚EasyCVR视频监控平台调取接口提示“认证过期”是什么原因?

视频汇聚EasyCVR视频监控平台&#xff0c;作为一款智能视频监控综合管理平台&#xff0c;凭借其强大的视频融合汇聚能力和灵活的视频能力&#xff0c;在各行各业的应用中发挥着越来越重要的作用。EasyCVR平台具备强大的拓展性和灵活性&#xff0c;支持多种视频流的外部分发&…

RT_Thread内核源码分析(二)——链表和对象管理

实时操作系统基本上都是通过一些链表进行线程、信号、队列的管理&#xff0c;RT_Thread也不例外&#xff0c;本章主要讲解RT_Thread的链表结构和对象管理。 本章基于RT_Thread Nano V3.1.5版本分析 1、链表 RT_Thread使用的链表非常简单&#xff0c;链表节点只有节点指针&#…

随机梯度下降的学习

梯度下降&#xff08;Gradient-Descent&#xff09; 在机器学习的旅途中&#xff0c;不可避免需要与它打交道&#xff0c;那么该如何初步理解它的用途呢&#xff1f; 好的&#xff0c;想象你在一个山谷中&#xff0c;想要找到最低点&#xff08;山谷的底部&#xff09;。你现…