9.24作业

news2025/4/7 14:18:29

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

+、[] 、>、<、==、>=、<=、!= 、+=(可以加等一个字符串,也可以加等一个字符)、输入输出(<< 、 >>)

代码如下

MyString.h

#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();
    //自定义 + 运算符重载函数
    My_string operator+ (const My_string &R);
    //自定义 [] 运算符重载函数
    char& operator[] (int index);
    //自定义 >< == >= <=运算符重载函数
    bool operator> (My_string &R);
    bool operator< (My_string &R);
    bool operator== (My_string &R);
    bool operator== (My_string &&s);
    bool operator!= (My_string &R);
    bool operator>= (My_string &R);
    bool operator<= (My_string &R);
    //自定义 += 运算符重载函数
    My_string operator+= (const My_string &R);
    My_string operator+= (const My_string &&R);
    //友元
    friend ostream & operator<< (ostream &L,const My_string &R);
    friend istream & operator>> (istream &L,const My_string &R);


};

//定义全局函数 << >>运算符重载
ostream & operator<< (ostream &L,const My_string &R);
istream & operator>> (istream &L,const My_string &R);
#endif // MYSTRING_H

MyString.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;
}

My_string My_string::operator+ (const My_string &R){
    My_string temp;
    temp.len = len + R.len;
    temp.size = size + R.size;
    temp.ptr = new char[temp.size];
    temp.ptr[0] = '\0';         // 确保以 '\0' 开头
    strcat(temp.ptr,this->ptr);
    strcat(temp.ptr,R.ptr);
    return temp;
}

char& My_string::operator[] (int index){
    return this->ptr[index];
}

bool My_string::operator> (My_string &R){
    return strcmp(this->ptr,R.ptr)>0 ? true:false;
}

bool My_string::operator< (My_string &R){
    return strcmp(R.ptr,this->ptr)>0 ? true:false;
}
bool My_string::operator== (My_string &R){
    return strcmp(R.ptr,this->ptr)==0 ? true:false;
}
bool My_string::operator== (My_string &&R){
    return strcmp(R.ptr,this->ptr)==0 ? true:false;
}
bool My_string::operator!= (My_string &R){
    return strcmp(R.ptr,this->ptr)!=0 ? true:false;
}
bool My_string::operator>= (My_string &R){
    return strcmp(R.ptr,this->ptr)>=0 ? true:false;
}
bool My_string::operator<= (My_string &R){
    return strcmp(R.ptr,this->ptr)<=0 ? true:false;
}
My_string My_string::operator+= (const My_string &R){
    this->len += R.len;
    this->size += R.size;
    strcat(this->ptr,R.ptr);
    return *this;
}
My_string My_string::operator+= (const My_string &&R){
    this->len += R.len;
    this->size += R.size;
    strcat(this->ptr,R.ptr);
    return *this;
}

ostream & operator<< (ostream &L,const My_string &R){
    L<<R.ptr<<endl;
    return L;
}

istream & operator>> (istream &L,const My_string &R){
    L>>R.ptr;
    return L;
}

main.cpp

#include "MyString.h"

int main() {
	My_string s1("hello");
    My_string s2 = s1 + " world";
    s2.show();
    My_string s3 = "nihao";
    if(s2>s3){
        cout<<"s2大"<<endl;
    }else cout<<"s3大"<<endl;
    if(s1==s3){
        cout<<"s1==s3"<<endl;
    }else cout<<"s1!=s3"<<endl;
    if(s1=="hello"){
        cout<<"s1==hello"<<endl;
    }
    s1 += s3;
    s1.show();
    s3 += " world";
    s3.show();
    My_string s4;
    cout<<"请输入一个字符串:"<<endl;
    cin>>s4;
    cout<<s4;
    return 0;
}

运行结果

在这里插入图片描述

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

代码如下

MyStack.h

#ifndef MYSTACK_H
#define MYSTACK_H
#include <iostream>

using namespace std;
class My_stack {
private:
    static const int MAX_SIZE = 10; // 定义栈的最大容量
    int data[MAX_SIZE];              // 固定大小的数组
    int topIndex;                    // 栈顶索引

public:
    // 构造函数
    My_stack();

    // 拷贝构造函数
    My_stack(const My_stack &other);

    // 赋值运算符
    My_stack& operator=(const My_stack &other);

    // 析构函数
    ~My_stack(){}

    // 返回栈顶元素
    int& top();

    // 返回栈是否为空
    bool empty() const;

    // 返回栈的大小
    int size() const;

    // 压入元素
    void push(int value);

    // 弹出元素
    void pop();

    // 交换两个栈的内容
    void swap(My_stack &other);
};

// 全局函数用于交换两个栈
void swap(My_stack &a, My_stack &b);
#endif

MyStack.cpp

#include "MyStack.h"
My_stack::My_stack() : topIndex(0) {}

// 拷贝构造函数
My_stack::My_stack(const My_stack &other) : topIndex(other.topIndex) {
    for (int i = 0; i < topIndex; ++i) {
        data[i] = other.data[i];
    }
}

// 赋值运算符
My_stack& My_stack::operator=(const My_stack &other) {
    if (this != &other) {
        topIndex = other.topIndex; // 更新栈顶索引
        for (int i = 0; i < topIndex; ++i) {
            data[i] = other.data[i]; // 复制元素
        }
    }
    return *this;
}

// 返回栈顶元素
int& My_stack::top() {
    if (empty()) {
        cout<< "栈空!" << endl;
        exit(EXIT_FAILURE); // 直接退出程序
    }
    return data[topIndex - 1];
}

// 返回栈是否为空
bool My_stack::empty() const {
    return topIndex == 0;
}

// 返回栈的大小
int My_stack::size() const {
    return topIndex;
}

// 压入元素
void My_stack::push(int value) {
    if (topIndex >= MAX_SIZE) {
        cout << "栈满!" << endl;
        exit(EXIT_FAILURE); // 直接退出程序
    }
    data[topIndex++] = value;
}

// 弹出元素
void My_stack::pop() {
    if (empty()) {
        cout<< "栈空!" << endl;
        exit(EXIT_FAILURE); // 直接退出程序
    }
    --topIndex;
}

// 交换两个栈的内容
void My_stack::swap(My_stack &other) {
    std::swap(topIndex, other.topIndex);
    for (int i = 0; i < MAX_SIZE; ++i) {
        std::swap(data[i], other.data[i]);
    }
}

// 全局函数用于交换两个栈
void swap(My_stack &a, My_stack &b) {
    a.swap(b);
}

main.cpp

#include "MyStack.h"

int main() {
    My_stack s;

    s.push(9);
    s.push(2);
    s.push(6);
    s.push(7);
    s.push(8);

    cout << "栈顶元素:" << s.top() << endl;
    cout << "栈的大小:" << s.size() << endl;

    s.pop();
    cout << "栈顶元素:" << s.top() << endl;

    My_stack s1;
    s1.push(1);
    s1.push(2);
    My_stack s2;
    s2 = s;
    swap(s2, s1); // 交换两个栈

    cout << "交换后的栈顶元素:" << s2.top() << endl;
    cout << "交换后另一个栈顶元素:" << s1.top() << endl;

    return 0;
}

运行结果

在这里插入图片描述

思维导图

在这里插入图片描述

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

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

相关文章

使用Docker和cpolar在Linux服务器上搭建DashDot监控面板

使用Docker和cpolar在Linux服务器上搭建DashDot监控面板 前言环境准备安装Docker下载Dashdot镜像 部署DashDot应用本地访问DashDot服务安装cpolar内网穿透固定DashDot公网地址结语 前言 在这个数字化飞速发展的时代&#xff0c;服务器作为支撑各种应用和服务的基础设施&#xf…

C++类和对象(中)【下篇】

&#x1f31f;个人主页&#xff1a;落叶 &#x1f31f;当前专栏: C专栏 目录 赋值运算符重载 运算符重载 赋值运算符重载 日期类实现 运算符重载<和运算符重载 运算符重载进行复用 运算符重载< 运算符重载> 运算符重载> 运算符重载! 获取某年某月的天数…

大模型算法岗常见面试题100道(值得收藏)非常详细收藏我这一篇就够了

大模型应该是目前当之无愧的最有影响力的AI技术&#xff0c;它正在革新各个行业&#xff0c;包括自然语言处理、机器翻译、内容创作和客户服务等等&#xff0c;正在成为未来商业环境的重要组成部分。 截至目前大模型已经超过200个&#xff0c;在大模型纵横的时代&#xff0c;不…

在Markdown中实现内部查询

markdown实现内部查询 在想要跳转到的位置添加 <a idxxx></a> 标签&#xff0c;如下图&#xff1a; 然后按如下格式添加目录 [跳转文字](#id)&#xff1a; 如上操作即可实现markdown内部查询。 具体实现效果如下&#xff1a;

八大排序——万字长文带你剖析八大排序(C语言)

本篇文章主要介绍八大排序的思想和具体实现&#xff0c;也会分析具体的时间复杂度和空间复杂度&#xff0c;提醒一些容易出现的坑和实现一些不同版本的排序&#xff0c;以及这些不同排序之间的效率分析 目录 1.插入排序 1.1直接插入排序 1.1.1 直接插入排序的思想&#xff…

PyTorch模型转ONNX量化模型

你是否发现模型太大&#xff0c;无法部署在你想要的云服务上&#xff1f;或者你是否发现 TensorFlow 和 PyTorch 等框架对于你的云服务来说太臃肿了&#xff1f;ONNX Runtime 可能是你的救星。 如果你的模型在 PyTorch 中&#xff0c;你可以轻松地在 Python 中将其转换为 ONNX…

关于YOLOX的一些优势

YOLOX 是旷视开源的高性能检测器。旷视的研究者将解耦头、数据增强、无锚点以及标签分类等目 标检测领域的优秀进展与 YOLO 进行了巧妙的集成组合&#xff0c;提出了 YOLOX&#xff0c;不仅实现了超越 YOLOv3、 YOLOv4 和 YOLOv5 的 AP&#xff0c;而且取得了极具竞争力的推理速…

FME学习笔记

读取数据 方法一&#xff1a;add reader 通过读模块来进行数据的读取 方法二&#xff1a;FeatureReader Parameters 通过转换器来进行数据的读取 可以通过空间范围进行筛选 在FME中&#xff0c;所有数据处理都要用到的&#xff0c;绝对的重点&#xff1a;转换器&#xff…

深圳某局联想SR850服务器黄灯 不开机维修

深圳 福田区1台Lenovo Thinksystem SR850 四路服务器黄灯问题现场处理。 1&#xff1a;型号&#xff1a;联想SR850 机架式2U服务器 2&#xff1a;故障&#xff1a;能通电&#xff0c;开机按钮快闪&#xff0c;随后叹号警告灯常亮 3&#xff1a;用户自行折腾无果后找到我们tech …

【推文制作】秀米简明教程 1.0

【前言】本文内容主要是针对一些常用的秀米操作进行介绍&#xff0c;并说明一些往年的经验要求。但是&#xff0c;最重要的是&#xff0c;请发挥你的艺术创造力&#xff0c;相信你一定可以做出更好看的推文。 一、秀米页面介绍 在使用秀米之前&#xff0c;我们会有一个通过浏览…

Maya学习笔记:项目设置和快捷键

文章目录 项目设置工程文件夹 快捷键 项目设置 工程文件夹 maya需要一个文件夹存放自己的工程内容 先指定一个文件夹 文件/项目窗口 选择一个文件夹&#xff0c;然后选择创建默认工作区 然后生成文件目录 在项目窗口里&#xff0c;选择要生成的子文件夹&#xff08;保持默认…

【ASE】第二课_溶解效果

今天我们一起来学习ASE插件&#xff0c;希望各位点个关注&#xff0c;一起跟随我的步伐 今天我们来学习溶解效果&#xff0c;通过渐变纹理达到好像燃烧效果的溶解效果 今天我们的效果很简单&#xff0c;但是其中包含没有学习的节点&#xff0c;所以还是要拿出来学习一下 最终…

ESP32异常报错2

出现这种情况 一般是缺少";"分号. 或者缺少, 仔细查找代码.查看是哪儿缺少了这些代码

【2024W35】肖恩技术周刊(第 13 期):肉,好次!

周刊内容: 对一周内阅读的资讯或技术内容精品&#xff08;个人向&#xff09;进行总结&#xff0c;分类大致包含“业界资讯”、“技术博客”、“开源项目”和“工具分享”等。为减少阅读负担提高记忆留存率&#xff0c;每类下内容数一般不超过3条。 更新时间: 星期天 历史收录:…

docker快速部署zabbix

两台主机 一台作为server 一台作为agent 安装好docker 并保证服务正常运行&#xff0c;镜像正常pull 分析&#xff1a; 部署 Zabbix 容器环境&#xff0c;通常会涉及几个主要组件&#xff1a; MySQL&#xff08;或 MariaDB 数据库&#xff09;、Zabbix Server 和 Zabbix Web I…

c++ 继承 和 组合

目录 一. 继承 1.1 继承的概念 1.2 继承定义 1.3 继承类模板 1.4. 继承中的作用域 二. 派生类&#xff08;子类&#xff09;的默认成员函数 2.1 概念&#xff1a; 2.2 实现⼀个不能被继承的类 2.3 继承与友元 2.4继承与静态成员 三.多继承及其菱形继承问题 3.1继承方…

物联网实践教程:微信小程序结合OneNET平台MQTT实现STM32单片机远程智能控制 远程上报和接收数据——汇总

物联网实践教程&#xff1a;微信小程序结合OneNET平台MQTT实现STM32单片机远程智能控制 远程上报和接收数据——汇总 前言 之前在学校获得了一个新玩意&#xff1a;ESP-01sWIFI模块&#xff0c;去搜了一下这个小东西很有玩点&#xff0c;远程控制LED啥的&#xff0c;然后我就想…

CUDA编程三、C++和cuda实现矩阵乘法SGEMM

目录 一、矩阵SGEMM 二、SGEMM的各种实现 1、cpu版本的实现 2、GPU并行计算最初始的版本 GPU中数据的移动 3、矩阵分块Shared Memory优化 4、LDS.128 float4* 优化 5、__syncthreads()位置优化 6、blank conflict优化 bank概念 bank conflict bank conflict危害和处…

IO其他流

1. 缓冲流 昨天学习了基本的一些流&#xff0c;作为IO流的入门&#xff0c;今天我们要见识一些更强大的流。比如能够高效读写的缓冲流&#xff0c;能够转换编码的转换流&#xff0c;能够持久化存储对象的序列化流等等。这些功能更为强大的流&#xff0c;都是在基本的流对象基础…

yum库 docker的小白安装教程(附部分问题及其解决方案)

yum库 首先我们安装yum 首先在控制台执行下列语句 首先切换到root用户&#xff0c;假如已经是了就不用打下面的语句 su root #使用国内的镜像&#xff0c;不执行直接安装yum是国外的&#xff0c;那个有问题 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.al…