1> 思维导图
2> 将昨天的My_string类中的所有能重载的运算符全部进行重载
+、[] 、>、=、>)
3> 仿照stack类实现my_stack,实现一个栈的操作
text.h
#ifndef LIST_H
#define LIST_H
#include <iostream>
#include <string.h>
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 MYempty() const;
//尾插
void push_back(char value);
//尾删
void pop_back();
//at函数实现
char &at(int index);
//清空函数
void clear();
//返回C风格字符串
char *data();
//返回实际长度
int get_length();
//返回当前最大容量
int get_size();
//君子函数:二倍扩容
void resize();
void show();
// 重载加法运算符
My_string operator+(const My_string& other);
// 重载下标运算符
char& operator[](size_t index);
// 重载大于运算符
bool operator>(const My_string& other);
// 重载小于运算符
bool operator<(const My_string& other);
// 重载等于运算符
bool operator==(const My_string& other);
// 重载大于等于运算符
bool operator>=(const My_string& other);
// 重载小于等于运算符
bool operator<=(const My_string& other);
// 重载不等于运算符
bool operator!=(const My_string& other);
// 重载加等运算符
My_string& operator+=(const My_string& other);
// 重载加等运算符(字符)
My_string& operator+=(char c);
// 重载输出运算符
friend ostream& operator<<(ostream& os, const My_string& myStr);
// 重载输入运算符
friend istream& operator>>(istream& is, My_string& myStr);
};
#endif // LIST_H
text.c
#include "text.h"
using namespace std;
My_string::My_string():size(15)
{
this->ptr = new char[15];
this->ptr[0] = '\0';
this->len = 0;
}
//有参构造
My_string::My_string(const char * src)
{
this->len = 0;
for(int i=0;src[i] != 0;i++)
{
this->len++;
}
this->size = this->len+1;
this->ptr = new char[this->size];
for(int i = 0;src[i] != 0;i++)
{
this->ptr[i] = src[i];
}
}
My_string::My_string(int num, char value)
{
this->len = num;
this->size = this->len+1;
this->ptr = new char[this->size];
for (int i = 0; i < this->len; i++)
{
this->ptr[i] = value; // 用value填充数组
}
this->ptr[this->len] = '\0'; // 添加结束符
}
//拷贝构造
My_string::My_string(const My_string& other):ptr(new char[other.len + 1]),size(other.size), len(other.len)
{
for(int i = 0;other.ptr[i] != 0;i++) // 复制字符串内容
{
this->ptr[i] = other.ptr[i];
}
}
//拷贝赋值
My_string & My_string::operator= (const My_string &other)
{
if(this != &other) //防止自己给自己赋值
{
this->size = other.size;
this->len = other.len;
//this->ptr = other.ptr; //浅拷贝
for(int i = 0;other.ptr[i] != 0;i++) //深拷贝
{
this->ptr[i] = other.ptr[i];
}
}
return *this; //返回值自身的引用
}
//析构构造
My_string::~My_string()
{
delete[] ptr; //需要显性定义析构函数,在析构函数的函数体内释放堆区空间
cout<<"Person::析构函数,this = "<<this<<endl;
}
//判空
bool My_string::MYempty() const
{
return this->ptr == nullptr || this->len == 0; // 如果指针为空或长度为0,则认为是空
}
//尾插
void My_string::push_back(char value)
{
this->size++;
char *Newptr=new char[this->size];
for(int i = 0;this->ptr[i] != 0;i++) //深拷贝
{
Newptr[i] = this->ptr[i];
}
delete[] this->ptr;
this->ptr=Newptr;
this->ptr[this->len]=value;
this->len++;
this->ptr[this->len]='\0';
}
//尾删
void My_string::pop_back()
{
this->ptr[len-1] = 0;
this->size--;
this->len--;
}
//at函数实现
char & My_string::at(int index)
{
static char num;
num = this->ptr[index-1];
return num;
}
//清空函数
void My_string::clear()
{
this->size = 15;
delete[] ptr;
this->ptr = new char[1];
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::resize()
{
size *= 2; // 容量翻倍
char* newptr = new char[size]; // 分配新的内存
for(int i = 0;this->ptr[i] != 0;i++) //深拷贝
{
newptr[i] = this->ptr[i];
}
delete[] ptr; // 释放旧内存
this->ptr = newptr; // 更新指针
}
void My_string::show()
{
cout<< "ptr = " << this->ptr << endl;
cout<< "size = " << this->size << endl;
cout<< "len = " << this->len << endl;
}
// 重载加法运算符
My_string My_string::operator+(const My_string& other)
{
My_string result;
result.size = len + other.len + 1;
result.ptr = new char[result.size];
strcpy(result.ptr, ptr);
strcat(result.ptr, other.ptr);
result.len = len + other.len;
return result;
}
// 重载下标运算符
char& My_string::operator[](size_t index)
{
return at(index);
}
// 重载大于运算符
bool My_string::operator>(const My_string& other)
{
return strcmp(ptr, other.ptr) > 0;
}
// 重载小于运算符
bool My_string::operator<(const My_string& other)
{
return strcmp(ptr, other.ptr) < 0;
}
// 重载等于运算符
bool My_string::operator==(const My_string& other)
{
return strcmp(ptr, other.ptr) == 0;
}
// 重载大于等于运算符
bool My_string::operator>=(const My_string& other)
{
return !(*this < other);
}
// 重载小于等于运算符
bool My_string::operator<=(const My_string& other)
{
return !(*this > other);
}
// 重载不等于运算符
bool My_string::operator!=(const My_string& other)
{
return !(*this == other);
}
// 重载加等运算符
My_string& My_string::operator+=(const My_string& other)
{
if (len + other.len >= size)
{
resize();
}
strcat(ptr, other.ptr);
len += other.len;
return *this;
}
// 重载加等运算符(字符)
My_string& My_string::operator+=(char c)
{
push_back(c);
return *this;
}
// 重载输出运算符
ostream& operator<<(ostream& os, const My_string& myStr)
{
os << myStr.ptr;
return os;
}
// 重载输入运算符
istream& operator>>(istream& is, My_string& myStr)
{
char buffer[1000]; // 假设最大输入长度为999
is >> buffer;
myStr.clear(); // 清空当前字符串
myStr.size = strlen(buffer) + 1;
myStr.ptr = new char[myStr.size];
strcpy(myStr.ptr, buffer);
myStr.len = strlen(buffer);
return is;
}
main.c
#include "text.h"
using namespace std;
int main()
{
My_string s;
s.show();
if(s.MYempty()==1)
{
cout << "s" << "为空" << endl;
}
My_string s1("hello world");
s1.show();
My_string s2(5,'A');
s2.show();
My_string s3(s1);
s3.show();
s.operator=(s2);
s.show();
My_string s4 = s1;
s4.push_back('B');
s4.show();
s4.pop_back();
s4.show();
cout << s4.at(3) << endl;
s4.clear();
if(s4.MYempty()==1)
{
cout << "s4" << "为空" << endl;
}
s3.data();
s3.show();
cout << s2.get_length() << endl;
cout << s2.get_size() << endl;
s1.resize();
s1.show();
My_string str1("hello");
My_string str2(" world");
str1.show();
str2.show();
My_string str3 = str1 + str2;
str3.show();
cout << "str3[1]: " << str3[1] << endl;
if (s1 < str3)
{
cout << "s1 < str3" << endl;
}
else if(s1 == str3)
{
cout << "s1 = str3" <<endl;
}
else if(s1 > str3)
{
cout << "s1 > str3" <<endl;
}
s1.show();
str3.show();
str1 += '!';
str1.show();
My_string str4;
cout << "请输入: ";
cin >> str4;
str4.show();
return 0;
}
2、
main.c
#include <iostream>
using namespace std;
class my_stack
{
private:
char* data; // 存储栈元素的动态数组
int capacity; // 栈的容量
int top; // 栈顶元素的索引
public:
// 构造函数
my_stack(int size = 10) : capacity(size), top(-1)
{
data = new char[capacity];
}
// 析构函数
~my_stack()
{
delete[] data;
}
// 入栈操作
void push(const char& value)
{
if (top + 1 >= capacity)
{
resize();
}
data[++top] = value;
}
// 出栈操作
void pop()
{
if (!empty())
{
--top;
}
else
{
cout << "Stack is empty. Cannot pop." << endl;
}
}
// 查看栈顶元素
char& peek()
{
if (!empty())
{
return data[top];
}
else
{
throw out_of_range("Stack is empty. Cannot peek.");
}
}
// 检查栈是否为空
bool empty() const
{
return top == -1;
}
// 获取栈的大小
int size() const
{
return top + 1;
}
private:
// 扩容函数
void resize()
{
capacity *= 2;
char* newData = new char[capacity];
for (int i = 0; i <= top; ++i)
{
newData[i] = data[i];
}
delete[] data;
data = newData;
}
};
// 主函数示例
int main()
{
my_stack stack;
// 入栈操作
stack.push(10);
stack.push(20);
stack.push(30);
cout << "栈顶元素:"<< stack.peek() << endl; // 输出: 30
cout << "栈大小:" << stack.size() << endl; // 输出: 3
// 出栈操作
stack.pop();
cout << "弹出操作后的顶部元素:" << stack.peek() << endl; // 输出: 20
// 检查栈是否为空
stack.pop();
stack.pop();
cout << "栈是否为空 " << (stack.empty() ? "是" : "否") << endl;
return 0;
}