//头文件
#ifndef STRING_H
#define STRING_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 *str);
//有参构造
My_string(int num,char value);
//拷贝赋值
My_string & operator =(const My_string &other);
//析构函数
~My_string();
//判空
void empty();
//输出
void show();
//尾插
void push_back(char a);
//尾删
void pop_back();
//at函数实现
char at(int num);
//清空函数
void clear();
//返回c风格字符串
char *data();
//返回实际长度
int get_length();
//返回当前最大容量
int get_size();
//二倍扩容
void kr();
//加法重载运算符函数
const My_string operator+(const My_string &R)const;
//下标运算符重载函数
char operator[](int num);
//>运算符重载函数
bool operator>(const My_string &R)const;
//<运算符重载函数
bool operator<(const My_string &R)const;
bool operator==(const My_string &R)const;
bool operator>=(const My_string &R)const;
bool operator<=(const My_string &R)const;
bool operator!=(const My_string &R)const;
My_string& operator+=(const char*arr);
friend ostream & operator<< (ostream &L, const My_string &R);
friend istream & operator>> (istream &L, My_string &R);
};
#endif // STRING_H
//源文件
#include "mstring.h"
//无参构造
My_string::My_string():size(15)
{
this->ptr = new char[size];
this->ptr[0] = '\0'; //表示串为空串
this->len = 0;
}
//有参构造
My_string::My_string(const char *str)
{
this->len = strlen(str);
this->size = len+1;
this->ptr = new char[size];
strcpy(this->ptr,str);
}
//有参构造
My_string::My_string(int num,char value)
{
this->len = num;
this->size = num+1;
this->ptr = new char[size];
for(int i=0;i<num;i++)
{
this->ptr[i] = value;
}
ptr[num] = '\0';
}
//拷贝赋值
My_string & My_string::operator =(const My_string &other) {
if (this != &other) {
delete[] ptr; // 释放原有内存
this->len = other.len;
this->size = other.size;
this->ptr = new char[size];
strcpy(this->ptr, other.ptr);
}
return *this;
}
//析构函数
My_string::~My_string()
{
delete[] ptr;
cout<<"使用了析构函数"<<endl;
}
//判空
void My_string::empty()
{
if(this->len==0)
{
cout<<"字符串为空"<<endl;
}
else
{
cout<<"字符串不为空"<<endl;
}
}
//输出
void My_string::show()
{
if(this->len == 0)
{
cout<<"字符串为空"<<endl;
return;
}
for(int i=0;i<this->len;i++)
{
cout<<this->ptr[i];
}
cout<<endl;
}
//尾插
void My_string::push_back(char a)
{
if(this->len>=this->size-1)
{
kr();
}
ptr[this->len] = a;
this->len++;
ptr[this->len] = '\0';
}
//尾删
void My_string::pop_back()
{
if(this->len == 0)
{
cout<<"字符串为空,无法尾删"<<endl;
return;
}
else
{
this->ptr[len-1] = '\0';
this->len--;
}
}
//at函数实现
char My_string::at(int num)
{
if(this->len == 0)
{
cout<<"字符串为空,无法查找"<<endl;
return '\0';
}
else if(num<1||num>this->len)
{
cout<<"输入的位置错误,无法查找"<<endl;
return '\0';
}
return this->ptr[num-1];
}
//清空函数
void My_string::clear()
{
if(this->len == 0)
{
cout<<"无需清空"<<endl;
}
else
{
this->ptr[0] = '\0';
this->len = 0;
}
}
//返回c风格字符串
char *My_string::data()
{
return ptr;
}
//返回实际长度
int My_string::get_length()
{
return this->len;
}
//返回当前最大容量
int My_string::get_size()
{
return this->size;
}
//二倍扩容函数
void My_string::kr()
{
if (this->len >= this->size - 1)
{
size *= 2;
char* new_ptr = new char[size];
strcpy(new_ptr, this->ptr);
delete[] ptr;
ptr = new_ptr;
}
}
//加法重载运算符函数
const My_string My_string::operator+(const My_string &R)const
{
My_string s1;
s1.len = this->len+R.len;
s1.size = s1.len+1;
s1.ptr = new char[s1.size];
strcpy(s1.ptr,this->ptr);
strcat(s1.ptr,R.ptr);
return s1;
}
//下标运算符重载函数
char My_string::operator[](int num)
{
if(this->len==0 || num<1 || num>this->len-1)
{
cout<<"输入位置错误,无法查找"<<endl;
return '\0';
}
else
{
return this->ptr[num-1];
}
}
//>运算符重载函数
bool My_string::operator>(const My_string &R)const
{
return strcmp(this->ptr,R.ptr)>0;
}
//<运算符重载函数
bool My_string::operator<(const My_string &R)const
{
return strcmp(this->ptr,R.ptr)<0;
}
//==运算符重载函数
bool My_string::operator==(const My_string &R)const
{
return strcmp(this->ptr,R.ptr)==0;
}
//>=运算符重载函数
bool My_string::operator>=(const My_string &R)const
{
return strcmp(this->ptr,R.ptr)==0||strcmp(this->ptr,R.ptr)>0;
}
//<=运算符重载函数
bool My_string::operator<=(const My_string &R)const
{
return strcmp(this->ptr,R.ptr)==0||strcmp(this->ptr,R.ptr)<0;
}
//!=运算符重载函数
bool My_string::operator!=(const My_string &R)const
{
return strcmp(this->ptr,R.ptr)>0||strcmp(this->ptr,R.ptr)<0;
}
//+=运算符重载函数
My_string& My_string::operator+=(const char* arr)
{
int new_len = this->len + strlen(arr);
char* new_ptr = new char[new_len + 1];
strcpy(new_ptr, this->ptr);
strcat(new_ptr, arr);
delete[] this->ptr;
this->ptr = new_ptr;
this->len = new_len;
this->size = new_len + 1;
return *this;
}
//<<运算符重载函数
ostream & operator<< (ostream &L, const My_string &R)
{
for(int i=0;i<R.len;i++)
{
L<<R.ptr[i];
}
L<<endl;
return L;
}
//>>运算符重载函数
istream & operator>> (istream &L,My_string &R)
{
string s1;
getline(L, s1);
R.len = s1.size();
R.size = R.len+1;
char *newptr = new char[s1.size()];
delete [] R.ptr;
R.ptr = newptr;
strcpy(R.ptr, s1.c_str());
R.len += s1.size();
return L;
}
//主函数
#include "mstring.h"
int main()
{
My_string s1(10,'y');
My_string s2("hello world");
My_string s3(10,'w');
cout<<s1.operator[](5)<<endl;
s1.show();
s2.show();
if(s1>s2)
{
cout<<"s1大于s2"<<endl;
}
else
{
cout<<"s1小于s2"<<endl;
}
if(s1<s2)
{
cout<<"s1小于s2"<<endl;
}
else
{
cout<<"s1大于s2"<<endl;
}
if(s1==s2)
{
cout<<"s1等于s2"<<endl;
}
else
{
cout<<"s1不等于s2"<<endl;
}
if(s1>=s2)
{
cout<<"s1大于等于s2"<<endl;
}
else
{
cout<<"s1小于s2"<<endl;
}
if(s1<=s2)
{
cout<<"s1小于等于s2"<<endl;
}
else
{
cout<<"s1大于s2"<<endl;
}
if(s1!=s2)
{
cout<<"s1不等于s2"<<endl;
}
else
{
cout<<"s1等于s2"<<endl;
}
cout<<s1<<endl;
cin>>s1;
s1.show();
return 0;
}
9.25作业
最新推荐文章于 2024-10-18 18:23:24 发布