实现字符串

实现字符串的作业,挺有意思(别说的你是大神一样。)

/*String.h*/
#ifndef SSCPP2014_STRING_H
#define SSCPP2014_STRING_H
 
#include <iostream>
 
class String {
private:
char *_buff;
int _length, _size; // _size is of the allocated memory
 
public:
// constructors
String();
explicit String(const char *src);
String(const String &src);
// destructor
~String();
// member methods
void assign(const char *src);
void append(const char &other);
void clear();
int compare(const String &other) const;
const char* c_str() const;
bool empty() const;
int find(const String &other, int pos = 0) const;
int length() const;
String substr(const int &pos, const int &count) const;
// overload operators
char& operator[](const int &index);
void operator=(const String &other);
void operator=(const char *src);
String operator+(const String &other) const;
bool operator<(const String &other) const;
bool operator<=(const String &other) const;
bool operator>(const String &other) const;
bool operator>=(const String &other) const;
bool operator==(const String &other) const;
bool operator!=(const String &other) const;
// friend methods
friend std::ostream& operator<<(std::ostream& out, const String &str);
// non-meaning static property
static char _error_sign; // initial as any char is okay
};
 
#endif
/*String.cpp*/
    #include "String.h"
    #include <cstdio>
    #include <cstring>
     
    char String::_error_sign = '\0';
     
    String::String() {
    _buff = 0;
    _length = 0;
    _size = 0;
    }
     
    String::String(const char *src) {
    _size = std::strlen(src) + 1;
    _buff = new char[_size];
    std::memset(_buff, 0, _size);
    std::snprintf(_buff, _size, "%s", src);
    _length = std::strlen(_buff);
    }
     
    String::String(const String &src) {
    _length = src._length;
    _size = src._size;
    _buff = new char[_size];
    std::memset(_buff, 0, _size);
    std::snprintf(_buff, _size, "%s", src._buff);
    }
     
    String::~String() {
    if (_buff)
    delete[] _buff;
    }
     
    void String::assign(const char *src) {
    this->clear();
    _size = std::strlen(src) + 1;
    _buff = new char[_size];
    std::memset(_buff, 0, _size);
    std::snprintf(_buff, _size, "%s", src);
    _length = std::strlen(_buff);
    }
     
    void String::append(const char &other) {
    if (this->empty()) {
    this->assign("A");
    _buff[0] = other;
    } else {
    if (_length + 1 < _size) {
    _buff[_length++] = other;
    } else {
    _size *= 2;
    char *new_buff = new char[_size];
    std::memset(new_buff, 0, _size);
    snprintf(new_buff, _size, "%s", _buff);
    delete[] _buff;
    _buff = new_buff;
    _buff[_length++] = other;
    }
    }
    }
     
    void String::clear() {
    _length = _size = 0;
    if (_buff) {
    delete[] _buff;
    _buff = 0;
    }
    }
     
    int String::compare(const String &other) const {
    int len = (_length > other._length ? other._length : _length);
    for (int i = 0; i < len; ++i) {
    if (_buff[i] < other._buff[i])
    return -1;
    else if (_buff[i] > other._buff[i])
    return 1;
    }
    if (_length < other._length)
    return -1;
    else if (_length > other._length)
    return 1;
    else
    return 0;
    }
     
    const char* String::c_str() const {
    if (!this->empty())
    return _buff;
    else
    return "";
    }
     
    bool String::empty() const {
    if (_length == 0 )
    return true;
    else
    return false;
    }
     
    int String::find(const String &other, int pos) const {
    if (other.empty())
    return -3;
    if (pos >= _length || pos < 0)
    return -2;
    if (other._length > _length - pos)
    return -1;
    int* overlay_value = new int[other._length + 1];
    overlay_value[0] = -1;
    int i, index = 0, p_index = 0, t_index = pos;
    for (i = 1; i < other._length; ++i) {
    index = overlay_value[i - 1];
    while (index >= 0 && other._buff[index + 1] != other._buff[i])
    index = overlay_value[index];
    if (other._buff[index + 1] == other._buff[i]) {
    overlay_value[i] = index + 1;
    } else {
    overlay_value[i] = -1;
    }
    }
    while (p_index < other._length && t_index < _length) {
    if (_buff[t_index] == other._buff[p_index]) {
    ++t_index;
    ++p_index;
    } else if (p_index == 0) {
    ++t_index;
    } else {
    p_index = overlay_value[p_index - 1] + 1;
    }
    }
    delete[] overlay_value;
    if (p_index == other._length) {
    return t_index - p_index;
    } else {
    return _length;
    }
    }
     
    int String::length() const {
    return _length;
    }
     
    String String::substr(const int &pos, const int &count) const {
    String ret;
    if (pos >= _length || pos + count > _length)
    return ret;
    ret._buff = new char[count + 1];
    ret._size = count + 1;
    std::memset(ret._buff, 0, ret._size);
    std::snprintf(ret._buff, count + 1, "%s", _buff + pos);
    ret._length = std::strlen(ret._buff);
    return ret;
    }
     
    char& String::operator[](const int &index) {
    if (index >= 0 && index < _length)
    return _buff[index];
    else
    return _error_sign;
    }
     
    void String::operator=(const String &other) {
    this->assign(other.c_str());
    }
     
    void String::operator=(const char *src) {
    this->assign(src);
    }
     
    String String::operator+(const String &other) const {
    String ret;
    ret._size = _length + other._length + 1;
    ret._buff = new char[ret._size];
    std::snprintf(ret._buff, _length + 1, "%s", _buff);
    std::snprintf(ret._buff + _length, other._length + 1, "%s", other._buff);
    ret._length = std::strlen(ret._buff);
    return ret;
    }
     
    bool String::operator<(const String &other) const {
    return this->compare(other) == -1 ? true : false;
    }
     
    bool String::operator<=(const String &other) const {
    return this->compare(other) == 1 ? false : true;
    }
     
    bool String::operator>(const String &other) const {
    return this->compare(other) == 1 ? true : false;
    }
     
    bool String::operator>=(const String &other) const {
    return this->compare(other) == -1 ? false : true;
    }
     
    bool String::operator==(const String &other) const {
    return this->compare(other) == 0 ? true : false;
    }
     
    bool String::operator!=(const String &other) const {
    return this->compare(other) == 0 ? false : true;
    }
     
    std::ostream& operator<<(std::ostream& out, const String &str) {
    out << str.c_str();
    return out;
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值