9.24作业

将昨天的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();
    //自定义 + 运算符重载函数
    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

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) const{
    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){
	char buff[1024];
    L>>buff;
    if(strlen(buff)<R.size){
		strcpy(R.ptr,buff);
		R.len = strlen(buff);
	}else cout<<"超出大小!"<<endl;
    return L;
}

main.cpp

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

cpp 复制代码
#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

cpp 复制代码
#include "MyStack.h"
My_stack::My_stack() : topIndex(-1) {}

// 拷贝构造函数
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 == -1;
}

// 返回栈的大小
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

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

运行结果

思维导图

相关推荐
想拿大厂offer2 分钟前
【算法】2022年第十三届蓝桥杯大赛软件类省赛Java大学C组真题
c语言·c++·算法·蓝桥杯
秋秋秋叶8 分钟前
Python学习——【6.1】文件操作
python·学习
Li_03040629 分钟前
Servlet入门:服务端小程序的初试(自己学习整理的资料)
java·学习·servlet
水木流年追梦1 小时前
【shell脚本5】Shell脚本学习--条件控制
人工智能·python·深度学习·学习·算法·机器学习
YRr YRr1 小时前
详解 CMake 命令:启用详细 Makefile 输出
linux·开发语言·c++·cmake
且行且知1 小时前
`#include <vector>`
c++·算法
蜡笔小新星1 小时前
Vue3与Flask后端Demo
开发语言·vue.js·经验分享·后端·python·学习·flask
姆路1 小时前
Qt中多语言的操作(以QtCreator为例)
c++·qt
散一世繁华,颠半世琉璃2 小时前
从零开始:在VSCode中打造完美的C++开发环境
c++·ide·vscode
小咖拉眯2 小时前
vscode将c++项目打包exe进行反汇编练习
c语言·汇编·c++·ide·vscode·安全·密码学