9.24每日作业

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]; // 假设最大输入长度为999is >> 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; // 输出: 30cout << "栈大小:" << stack.size() << endl;  // 输出: 3// 出栈操作stack.pop();cout << "弹出操作后的顶部元素:" << stack.peek() << endl; // 输出: 20// 检查栈是否为空stack.pop();stack.pop();cout << "栈是否为空 " << (stack.empty() ? "是" : "否") << endl; return 0;
}

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

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

相关文章

医院为什么要安装医疗设备防漏费系统?

一、医院防漏费管理的重要性 随着人们健康意识的加强&#xff0c;医生对诊断的依据都造就了检查和化验在新形式下的重要性。人们对体检重要性的认识等各方面因素。导致了现在医院检查和化验位置一度提升。成为了医院工作的重中之中。而在中国国情的大环境下&#xff0c;熟人检…

【最新华为OD机试E卷-支持在线评测】爱吃蟠桃的孙悟空(100分)多语言题解-(Python/C/JavaScript/Java/Cpp)

🍭 大家好这里是春秋招笔试突围 ,一枚热爱算法的程序员 💻 ACM金牌🏅️团队 | 大厂实习经历 | 多年算法竞赛经历 ✨ 本系列打算持续跟新华为OD-E/D卷的多语言AC题解 🧩 大部分包含 Python / C / Javascript / Java / Cpp 多语言代码 👏 感谢大家的订阅➕ 和 喜欢�…

【使用Spring事件监听机制实现业务解耦+相关拓展】

使用Spring事件监听机制实现业务解耦 一.业务场景二.Spring事件监听机制是什么&#xff1f;1.相关概念2.解耦 三.使用步骤1.定义事件2.发布事件3.侦听事件的对象4.测试结果5.有没有小伙伴们发现上面这种发布订阅有什么问题&#xff1f;代码改造&#xff1a;1. 自定义线程池&…

[极客大挑战 2019]EasySQL1

前言&#xff1a; 记录一下web方面的题(第一次接触。。。&#xff09; 学校课程要学web…… - - 行吧&#xff0c;尝试一下&#xff0c;至少学过MySQL。。。 不过&#xff0c;实际上&#xff0c;现实现在SQL漏洞少得可怜&#xff0c;但学习不会有错。 参考&#xff1a;&…

《 C++ 修炼全景指南:十二 》用红黑树加速你的代码!C++ Set 和 Map 容器从入门到精通

摘要 本文详细介绍了基于红黑树实现的 Set 和 Map 容器&#xff0c;包括其底层设计原理、插入和删除操作的实现细节、性能分析与优化策略&#xff0c;以及实际应用场景和未来发展方向。通过采用红黑树的数据结构&#xff0c;Set 和 Map 容器能够高效地处理有序数据&#xff0c…

【笔记】自动驾驶预测与决策规划_Part4_时空联合规划

文章目录 0. 前言1. 时空联合规划的基本概念1.1 时空分离方法1.2 时空联合方法 2.基于搜索的时空联合规划 &#xff08;Hybrid A* &#xff09;2.1 基于Hybrid A* 的时空联合规划建模2.2 构建三维时空联合地图2.3 基于Hybrid A*的时空节点扩展2.4 Hybrid A* &#xff1a;时空节…

多颜色绘制语义分割/变化检测结果图

在论文绘图时&#xff0c;传统的二元语义分割结果图颜色单一&#xff08;下图左&#xff09;&#xff0c;所以论文中常根据混淆矩阵类别使用多颜色进行绘制&#xff08;下图右&#xff09;&#xff0c;可以看到&#xff0c;结果的可视化效果更好。 以下是绘制代码&#xff1a; …

**CentOS7安装配置mysql**

CentOS7安装配置mysql 首先先将mysql57-community-release-el7.rpm解压出来 rpm -ivh mysql57-community-release-el7.rpmls /etc/yum.repos.d/ -l // 检查是否解压成功安装mysql yum install -y mysql-community-server可能会出现 GPG 密钥过期 rpm --import https://r…

OpenHarmony(鸿蒙南向)——平台驱动开发【MIPI DSI】

往期知识点记录&#xff1a; 鸿蒙&#xff08;HarmonyOS&#xff09;应用层开发&#xff08;北向&#xff09;知识点汇总 鸿蒙&#xff08;OpenHarmony&#xff09;南向开发保姆级知识点汇总~ 持续更新中…… 概述 功能简介 DSI&#xff08;Display Serial Interface&#x…

2024年9月23日---关于MyBatis框架(2)

4.7 不同返回值类型的查询 4.7.1 返回基本数据类型 /**查询student表中的记录个数 */ int selectCount(); <select id"selectCount" resultType"_int">select count(*) from student; </select> 4.7.2 返回引用类型(实体类) /**返回值为实…

智能泡茶设备控制系统设计-设计说明书

设计摘要&#xff1a; 智能泡茶设备控制系统设计旨在实现对泡茶过程的自动化控制&#xff0c;提升泡茶的便利性和稳定性。本系统采用嵌入式技术与传感器相结合&#xff0c;实现对泡茶设备的全方位监控和控制。关键功能包括水温控制、浸泡时间控制、浸泡压力控制、茶水浓度控制…

电信、移动、联调等运营商都有那些国产化自研软件

国产化自研软件方面有着积极的探索和实践&#xff0c;包括操作系统、数据库和中间件等&#xff0c;电信运营商在国产化软件方面取得了显著进展&#xff1a; 操作系统&#xff1a; 中国电信推出了基于华为欧拉openEuler开源系统的天翼云操作系统CTyunOS&#xff0c;已上线部署5万…

基于JAVA+SpringBoot+Vue的医院后台管理系统

基于JAVASpringBootVue的医院后台管理系统 前言 ✌全网粉丝20W,csdn特邀作者、博客专家、CSDN[新星计划]导师、java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末附源码下载链接&#x1f345; 哈…

DBeaver启动报错 Faild to load the JNI shared library

DBeaver启动报错 Faild to load the JNI shared library 问题现象 安装完成后&#xff0c;启动dbeaver报错 查看版本为64位版本&#xff0c;JAVA也为64为版本 dbeaver版本 java版本 解决 在dberver.ini添加指定配置&#xff0c;即可启动成功

HarmonyOS NEXT:解密从概念到实践的技术创新与应用前景

HarmonyOS是目前华为手机所搭载的鸿蒙系统&#xff0c;它在Open Harmony的基础上兼容了安卓的AOSP&#xff0c;所以可以使用安卓APK应用&#xff0c;HarmonyOS属于华为在当前阶段过渡使用的系统&#xff0c;原生鸿蒙的应用生态尚未发展起来&#xff0c;兼容安卓应用可以让用户有…

Spring源码-ConfigurationClassPostProcessor类解析spring相关注解

ConfigurationClassPostProcessor类的作用 此类是一个后置处理器的类&#xff0c;主要功能是参与BeanFactory的建造&#xff0c;主要功能如下 1、解析加了Configuration的配置类 2、解析ComponentScan扫描的包 3、解析ComponentScans扫描的包 4、解析Import注解 该类在springbo…

【web开发】Spring Boot 快速搭建Web项目(三)

Date: 2024.08.31 18:01:20 author: lijianzhan 简述&#xff1a;根据上篇原文Spring Boot 快速搭建Web项目&#xff08;二&#xff09;&#xff0c;由于已经搭建好项目初始的框架&#xff0c;以及自动创建了一个启动类文件&#xff08;TestWebApplication.java&#xff09; …

带你一文了解CISP-PTE的用处

CISP-PTE认证是由中国信息安全测评中心颁发的国家级专业证书&#xff0c;专注于培养和考核网络安全渗透测试方面的高级应用安全人才。CISP-PTE认证的目的是提升个人在信息安全领域的技术水平&#xff0c;特别是在渗透测试方面。 一、CISP-PTE的重要性 1.提升职业竞争力 CISP-PT…

华为云发布全栈可观测平台AOM,以AI赋能应用运维可观测

9月19日&#xff0c;华为全联接大会2024举办期间&#xff0c;在“AI赋能应用现代化&#xff0c;加速软件生产力跃升”为主题的论坛上&#xff0c;华为云发布全栈可观测平台AOM&#xff0c;以AI赋能应用运维可观测&#xff0c;提升企业应用可用性与稳定性。 该平台发布标志着华…

MacOS上安装MiniConda的详细步骤

前言 MiniConda是一种环境配置工具。在不同的开发项目中&#xff0c;我们会使用到不同版本的Python和第三方库&#xff08;例如Numpy、Pandas)。如果不使用环境配置工具&#xff0c;每次开发都需要清除电脑里上一次开发的环境和配置文件。为了在同一台机器上同时开发多个项目&…