2024.9.2 作业

完成mystring类部分功能实现

代码:

/*******************************************/

文件名:work4.h

/*******************************************/

复制代码
#ifndef WORK4_H
#define WORK4_H
#include <iostream>
#include<cstring>
using namespace std;
class Mystring
{
private:
    char *buf;
public:
    static int count;
public:
    Mystring()
    {
        buf = new char[1];
        *buf = '\0';
        count++;
    }
    Mystring(const char *b);
    ~Mystring()
    {
        delete[] buf;
        count--;
    }
    void show()
    {
        cout<<buf<<endl;
    }
    const Mystring &operator=(const Mystring &R);
    char at(int index);
    const Mystring operator[](int index);
    char data();
    const Mystring c_str();
    bool empty();
    int size()const;
    int length()const;
    int capacity()const;
    bool clear();
    bool push_back(char s);
    bool pop_balck();
    bool append(char s);
    Mystring &operator+=(const Mystring &R);
    const Mystring operator+(const Mystring &R);
    bool operator==(const Mystring &R);
    bool operator!=(const Mystring &R);
    bool operator>(const Mystring &R);
    bool operator>=(const Mystring &R);
    bool operator<=(const Mystring &R);
    friend ostream & operator<<(ostream &L, const Mystring &R);
    friend istream & operator>>(istream &L, Mystring &R);
};

#endif // WORK4_H

/*******************************************/

文件名:work4.cpp

/*******************************************/

复制代码
#include"work4.h"
int Mystring::count = 0;
Mystring::Mystring(const char *b)
{
    int len=strlen(b);
    this->buf = new char[len + 1];
    strcpy(this->buf, b);
    count++;
}
const Mystring &Mystring::operator=(const Mystring &R)
{
    delete[] buf;
    buf = new char[std::strlen(R.buf) + 1];
    std::strcpy(buf, R.buf);
    return *this;
}
char Mystring::at(int index)
{
    if (index < 0 || index >= (int)strlen(buf)) {
        throw std::out_of_range("Index out of range");
    }
    return this->buf[index];
}
const Mystring Mystring::operator[](int index)
{
    return Mystring(&buf[index]);
}
char Mystring::data()
{
    return *this->buf;
}
const Mystring Mystring::c_str()
{
    return *this;
}
bool Mystring::empty()
{
    return "this->buf=='\0'";
}
int Mystring::size()const
{
    return strlen(this->buf);
}
int Mystring::length()const
{
    return size();
}
int Mystring::capacity()const
{
    return strlen(this->buf)+1;
}
bool Mystring::clear()
{
    delete[]this->buf;
    this->buf=new char[1];
    *buf='\0';
    return 1;
}
bool Mystring::push_back(char s)
{
    char *newBuf = new char[capacity() + 1];
    std::strcpy(newBuf, this->buf);
    newBuf[size()] = s;
    newBuf[size() + 1] = '\0';
    delete[] this->buf;
    buf = newBuf;
    return 1;
}
bool Mystring::pop_balck()
{
    if (empty())
    {
        return 0;
    }
    char *newBuf = new char[size() + 1];
    std::strncpy(newBuf, this->buf, size() - 1);
    newBuf[size() - 1] = '\0';
    delete[] this->buf;
    buf = newBuf;
    return 1;
}
bool Mystring::append(char s)
{
    return push_back(s);
}
Mystring &Mystring::operator+=(const Mystring &R)
{
    *this = *this + R;
    return *this;
}
const Mystring Mystring::operator+(const Mystring &R)
{
    Mystring temp;
    temp.buf = new char[size() + R.size() + 1];
    strcpy(temp.buf, buf);
    strcat(temp.buf, R.buf);
    return temp;
}
bool Mystring::operator==(const Mystring &R)
{
    return strcmp(buf, R.buf) == 0;
}
bool Mystring::operator!=(const Mystring &R)
{
    return !(*this == R);
}
bool Mystring::operator>(const Mystring &R)
{
    return strcmp(this->buf, R.buf) > 0;
}
bool Mystring::operator>=(const Mystring &R)
{
    return *this > R || *this == R;
}
bool Mystring::operator<=(const Mystring &R)
{
    return !(*this > R);
}
ostream & operator<<(ostream &L, const Mystring &R)
{
    L << R.buf;
    return L;
}
istream & operator>>(istream &L, Mystring &R)
{
    L>>R.buf;
    return L;
}

/*******************************************/

文件名:main.cpp

/*******************************************/

复制代码
#include"work4.h"

int main()
{
    Mystring s1="hello";
    s1.show();
    Mystring s2="world!";
    s2.show();
    Mystring s3=s1+s2;
    s3.show();
    cout<<s1.size()<<endl;
    cout<<s2.length()<<endl;
    cout<<s3.capacity()<<endl;
    cout<<s2.at(3)<<endl;
    cout<<s1.data()<<endl;
    cout<<s3.append('e')<<endl;
    cout<<s2.c_str()<<endl;
    s3.pop_balck();
    cout<<s3<<endl;
    s3.push_back('.');
    cout<<s3<<endl;
    Mystring s4="ssr";
    s4+="'a'";
    cout<<s4<<endl;
    Mystring s5;
    cout<<"请输入:";
    cin>>s5;
    cout<<s5<<endl;
    cout<<(s5>=s3)<<endl;
    return 0;
}

结果:

思维导图:

相关推荐
JAVA学习通5 分钟前
OJ竞赛平台----C端题目列表
java·开发语言·jvm·vue.js·elasticsearch
GHL2842710906 分钟前
用PDH库获取CPU使用率(源码)
c++
让我们一起加油好吗7 分钟前
【基础算法】多源 BFS
c++·算法·bfs·宽度优先·多源bfs
冷崖10 分钟前
定时器的学习(二)
linux·c++·学习
B站计算机毕业设计之家12 分钟前
深度学习实战:python动物识别分类检测系统 计算机视觉 Django框架 CNN算法 深度学习 卷积神经网络 TensorFlow 毕业设计(建议收藏)✅
python·深度学习·算法·计算机视觉·分类·毕业设计·动物识别
大胆飞猪14 分钟前
高并发内存池日志
c++·项目
And_Ii26 分钟前
LeetCode 3350. 检测相邻递增子数组 II
数据结构·算法·leetcode
想唱rap29 分钟前
C++ string类的使用
开发语言·c++·笔记·算法·新浪微博
JAVA学习通29 分钟前
Replication(下):事务,一致性与共识
大数据·分布式·算法
胖咕噜的稞达鸭29 分钟前
C++中的父继子承(2)多继承菱形继承问题,多继承指针偏移,继承组合分析+高质量习题扫尾继承多态
c语言·开发语言·数据结构·c++·算法·链表·c#