当前位置: 首页 > article >正文

2024.9.24 作业

My_string类中的所有能重载的运算符全部进行重载+、[] 、>、=、>)

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

#include <iostream>
#include <cstring>

using namespace std;


class My_string{
private:
    char *ptr;
    int size;
    int len;

public:
    //无参构造
    My_string():size(15){
        this->ptr=new char[size];
        this->ptr[0]='\0';
        this->len=0;
    }
    //有参构造
    My_string(const char*src):len(0){
        len=strlen(src);
        this->size=this->len+1;
        this->ptr=new char[size];
        strcpy(ptr,src);
    }
    My_string(int num,char value):size(num+1),len(num){
        ptr=new char[size];
        for(int i=0;i<len;i++){
            ptr[i]=value;
        }
        ptr[len]='\0';
    }
    //拷贝构造
    My_string(const My_string&other){
        size=other.size;
        len=other.len;
        ptr=new char[other.size];
        strcpy(ptr,other.ptr);

    }
    //拷贝赋值
    My_string& operator=(const My_string&other){
        if(this==&other){
            return *this;
        }
        size=other.size;
        len=other.size;
        ptr=new char[other.size];
        strcpy(ptr,other.ptr);
    }

    //析构
    ~My_string(){
        delete[] ptr;
    }

    //判空
    bool isempty(){
        return len==0;
    }

    //尾插
    void push_back(char value){
       if(len+1>=size){
           size+=1;
           char *new_ptr=new char(size);
           strcpy(new_ptr,ptr);
           delete []ptr;
           ptr=new_ptr;
       }
       ptr[len++]=value;
       ptr[len]='\0';
    }

    //尾删
    void pop_back(){
        if(len>0){
        ptr[--len]='\0';
    }
    }

    //at
    char &at(int index){
        return ptr[index];
    }

    //清空
    void clear(){
        len=0;
        ptr[0]='\0';
    }

    //获取c风格字符串
    void *data(){
        return ptr;
    }

    //获取实际长度
    int get_length(){
        return len;
    }

    //获取最大容量
    int get_size(){
        return size;
    }

    //二倍扩充,注意new_ptr不需要delete回收是因为这个是栈空间,函数结束会自动回收。
    void double_capacity(){
        size*=2;
        char *new_ptr=new char[size];
        strcpy(new_ptr,ptr);
        delete []ptr;
        ptr=new_ptr;
    }

    My_string operator+(const My_string&other)const{
        My_string result;
        result.len=len+other.len;
        result.size=result.len+1;
        result.ptr=new char[result.size];
        strcpy(result.ptr,ptr);
        strcat(result.ptr,other.ptr);
        //虽然result在栈上的空间会自动释放,注意result的数组不是,需要析构函数手动删除
        return result;
    }

    My_string& operator+=(const My_string&other){
        len+=other.len;
        if(len+1>size){
            size=len+1;
            char *new_ptr=new char[size];
            strcpy(new_ptr,ptr);
            strcat(new_ptr,other.ptr);
            delete [] ptr;
            ptr=new_ptr;
        }else{
            strcat(ptr,other.ptr);
        }
        return *this;

    }

    My_string& operator+=(char value){
        push_back(value);
        return *this;
    }

    bool operator<(const My_string&other)const{
        return strcmp(ptr,other.ptr)<0;
    }
    bool operator>(const My_string&other)const{
        return strcmp(ptr,other.ptr)>0;
    }

    bool operator<=(const My_string&other)const{
        return strcmp(ptr,other.ptr)<=0;
    }
    bool operator>=(const My_string&other)const{
        return strcmp(ptr,other.ptr)>=0;
    }
    bool operator==(const My_string&other)const{
        return strcmp(ptr,other.ptr)==0;
    }
    friend ostream&operator<<(ostream&os,const My_string&str){
        os<<str.ptr;
        return os;
    }
    friend istream& operator>>(istream&is,My_string&str){
        char buffer[1024];
        is>>buffer;
        str=My_string(buffer);
        return is;
    }


};

class My_stack{
private:
    int* data;
    int capacity;
    int top;
public:
    My_stack(int size=10):capacity(size),top(-1){
        data=new int[size];
    }
    ~My_stack(){
        delete []data;
    }
    void double_size(){
        capacity=2;
        int *new_ptr=new int[capacity];
        for(int i=0;i<=top;i++){
            new_ptr[i]=data[i];
        }
        delete []data;
        data=new_ptr;
    }
    bool isempty(){
        return top==-1;
    }
    void push(const int&value){
        if(top+1>=capacity){
            double_size();
        }
        data[++top]=value;
    }

    void pop(){
        if(isempty())return;
        else --top;
    }
    int get_top(){
        return data[top];
    }
    void swap(int index1,int index2){
        int temp=data[index1];
        data[index1]=data[index2];
        data[index2]=temp;
    }
};

int main()
{
    My_string str1("Hello");
    My_string str2(" World");
    My_string str3 = str1 + str2;

    cout << "连接 " << str3 << endl;

    My_string str4;
    cout << "输入 ";
    cin >> str4;
    cout << "输入了" << str4 << endl;

    return 0;
}


http://www.kler.cn/news/326951.html

相关文章:

  • Stable Diffusion 蒙版:填充、原图、潜空间噪声(潜变量噪声)、潜空间数值零(潜变量数值零)
  • 我店生活系统小程序开发功能解析
  • uniapp框架中实现文件选择上传组件,可以选择图片、视频等任意文件并上传到当前绑定的服务空间
  • 修改 idea 的 Terminal 命令窗口使用 git-bash
  • 得物App荣获新奖项,科技创新助力高质量发展
  • 国内ChatGPT镜像网站整理汇总【OpenAI o1/GPT 4o】-2024/10月最新
  • 【数据结构与算法】算法和算法分析
  • nginx服务介绍
  • ros2 colcon build 构建后,install中的local_setup.bash 和setup.bash有什么区别
  • 企业数据可视化大屏的工具选择有哪些
  • UI设计师面试整理-设计趋势和行业理解
  • 互斥量mutex、锁、条件变量和信号量相关原语(函数)----很全
  • 从两个 Excel 表格中提取相关信息,并根据学生的 学号 和 姓名 将第一个表格中的成绩数据填充到第二个表格中(附Python代码)
  • 【LeetCode HOT 100】详细题解之链表篇
  • 【Kubernetes】常见面试题汇总(四十八)
  • MySQL实现跨服务器查询
  • Vscode超好看的渐变主题插件
  • Axure9破解
  • MySQL中的嵌套查询
  • Go实现RabbitMQ消息模式
  • 科研绘图系列:R语言堆积图(stacked barplot)
  • 数据驱动农业——农业中的大数据
  • MySQL | excel数据输出insert语句
  • STM8S003F定时器延时
  • 【华为HCIP实战课程二】OSPF基础介绍和OSPF RID NBMA配置详解
  • 软件测试学习笔记丨curl命令发送请求
  • 【机器学习】---异构数据融合
  • 【C语言】字符和字符串函数(2)
  • uniapp在线打包的ios后调用摄像头失败的解决方法
  • 51单片机的智能垃圾桶【proteus仿真+程序+报告+原理图+演示视频】