9.24作业

news/2024/9/25 5:40:43/

将昨天的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.ppmy.cn/news/1530145.html

相关文章

智能Ai语音机器人的应用价值有哪些?

随着时间的推移&#xff0c;人工智能的发展越来越成熟&#xff0c;智能时代也离人们越来越近&#xff0c;近几年人工智能越来越火爆&#xff0c;人工智能的应用已经开始渗透到各行各业&#xff0c;与生活交融&#xff0c;成为人们无法拒绝&#xff0c;无法失去的一个重要存在。…

Qt日志输出及QsLog日志库

目录 Qt日志输出及QsLog日志库日志输出格式化日志普通格式化条件格式化环境变量设置格式化日志输出位置日志输出对象信息禁用输出 QsLog日志库使用方法1. 将QsLog目录添加到项目中2. 配置CMakeLists.txt文件3. 配置.pro文件4. 日志记录器的配置5. 运行程序6. 启用行号和文件名C…

【计算机网络 - 基础问题】每日 3 题(二十)

✍个人博客&#xff1a;Pandaconda-CSDN博客 &#x1f4e3;专栏地址&#xff1a;http://t.csdnimg.cn/fYaBd &#x1f4da;专栏简介&#xff1a;在这个专栏中&#xff0c;我将会分享 C 面试中常见的面试题给大家~ ❤️如果有收获的话&#xff0c;欢迎点赞&#x1f44d;收藏&…

性能监控之Python实战SkyWalking链路追踪

文章目录 一、介绍二、SkyWalking支持的语言三、SkyWalking安装3.1 前提准备3.2 先安装ElasticSearch7.X3.3 Skywalking-OAP 安装3.4 Skywalking-UI 界面安装3.5 访问页面检查SkyWalking是否可以访问 四、Python 项目接入SkyWalking4.1 演示项目代码4.2 验证 sw-python4.3 配置…

关于wordPress中的用户登录注册等问题

前言 大家在做类似的功能的时候&#xff0c;有没有相关的疑问。那就是我都已经选择好了相应的主题和模版&#xff0c;但是为什么都没有用户注册和用户登录的页面存在呢&#xff1f; WordPress默认情况下不提供用户注册和登录功能的原因是它最初是作为一个博客平台开发的&…

AjAX(简介以及一些用法)

AJAX 1. 简介 什么是 Ajax Ajax 的全称是 Asynchronous JavaScript And XML &#xff08;异步 JavaScript 和 XML &#xff09;我们可以理解为&#xff1a;在网页中 利用 XMLHttpRequest 对象和服务器进行数据交互的方式就是 Ajax &#xff0c;它可以帮助我们轻松实现网页…

Python画笔案例-059 绘制甩曲彩点动图

1、绘制甩曲彩点动图 通过 python 的turtle 库绘制 甩曲彩点动图,如下图: 2、实现代码 绘制甩曲彩点动图,以下为实现代码: """甩曲彩点动图.py """ import time import turtlecs = [red,orange,

优选算法之 分治-快排

目录 一、颜色分类 1. 题目链接&#xff1a;75. 颜色分类 2. 题目描述&#xff1a; 3. 解法&#xff08;快排思想 - 三指针法使数组分三块&#xff09; &#x1f334;算法思路&#xff1a; &#x1f334;算法流程&#xff1a; &#x1f334;算法代码&#xff1a; 二、快…