2024.9.24 作业

server/2024/12/22 3:59:52/

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';}}//atchar &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.ppmy.cn/server/124781.html

相关文章

分布式项目-开盒头条

开盒头条 前言 只懂得技术理论是远远不够的&#xff0c;还需要熟练掌握很多业务功能逻辑的实现&#xff0c;这样才能真正的提高自己的开发水平。因此&#xff0c;我新开了这个专栏&#xff0c;专门做项目&#xff0c;教给大家很多业务功能实现的逻辑以及在实现这些业务功能时…

实战19-详情页UI4等分

import { PADDING } from ../../constants/size; import rvp from ../../utils/resposive/rvIndex;Component export default struct SearchFilter {build() {Row() {Row({ space: rvp(6) }) {Text("位置").fontSize(rvp(14)).fontColor(#333333)Image($r(app.media.…

在pycharm中怎样调试HTML网页程序

在PyCharm中调试HTML页面通常是指调试与HTML页面相关的JavaScript代码。PyCharm提供了内置的JavaScript调试支持&#xff0c;可以通过以下步骤进行设置和调试&#xff1a; 确保你的项目设置为能够本地服务器或者开发服务器。 在PyCharm中打开你的HTML文件。 在浏览器中打开你…

fastadmin 搜索提交重置按钮文本修改

默认 修改require-backend.min.js文件 效果 当然最好还是去需修改lang文件 效果 如果修改没生效记得清楚一下缓存&#xff0c;再刷新 完结 赠人玫瑰&#xff0c;手有余香&#xff01;如果文章内容对你有所帮助&#xff0c;请不要吝啬你的点赞评论和关注&#xff0c;你…

推荐4款2024年热门的PDF转ppt工具

有时候&#xff0c;我们为了方便&#xff0c;需要将PDF里面的内容直接转换的PPT的格式&#xff0c;既方便自己演示和讲解&#xff0c;也让我们可以更加灵活的进行文件的编辑和修改。如果大家不知道要如何进行操作的话&#xff0c;我可以为大家推荐几个比窘方便实用的PDF转换工具…

SprinbBoot 文件上传

SprinbBoot 文件上传 后端&#xff08;Spring Boot&#xff09; 添加依赖&#xff1a;确保你的pom.xml文件中包含了Spring Boot的Web依赖。 xml <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-…

回归预测 | Matlab基于SO-SVR蛇群算法优化支持向量机的数据多输入单输出回归预测

回归预测 | Matlab基于SO-SVR蛇群算法优化支持向量机的数据多输入单输出回归预测 目录 回归预测 | Matlab基于SO-SVR蛇群算法优化支持向量机的数据多输入单输出回归预测预测效果基本描述程序设计参考资料 预测效果 基本描述 1.Matlab基于SO-SVR蛇群算法优化支持向量机的数据多…

JavaScript中的输出方式

1. console.log() console.log() 是开发者在调试代码时最常用的方法。它将信息打印到浏览器的控制台&#xff0c;使开发者能够查看变量的值、程序的执行状态以及其他有用的信息。 用途&#xff1a;用于调试和记录程序运行时的信息。优点&#xff1a;简单易用&#xff0c;适合…