#include <iostream>
#include <cstring>
using namespace std;
class mystring{
private:
char *str; //记录c风格的字符串
int size; //记录字符串的实际长度
public:
//无参构造
mystring():size(10){
str=new char[size]; //构造出一个长度为10的字符串
strcpy(str,""); //初始化str
// cout<<"无参构造"<<endl;
}
//有参构造
mystring(const char *s){
size=strlen(s);
str=new char[size+1];
strcpy(str,s);
// cout<<"有参构造"<<endl;
}
//拷贝构造
mystring(mystring &other){
size=other.size;
str=new char[size+1];
strcpy(str,other.str);
// cout<<"拷贝构造"<<endl;
}
//析构函数
~mystring(){
delete []str;
// cout<<"析构函数"<<endl;
}
//判空函数
bool empty(){
if(0==strlen(str)){
return true;
}else{
return false;
}
}
//size函数
int size_t(){
return size;
}
//c_str函数
char *c_str(){
return str;
}
//at函数
char at(int size_s){
if(size_s>=size||size_s<0){ //判断字符串是否越界
cout<<"Out_of_range"<<endl;
}
return str[size_s];
}
//拷贝赋值
mystring &operator= (const mystring &other){
if(this !=&other){
delete []str;
this->size=other.size;
this->str=new char[size+1];
strcpy(this->str,other.str);
}
return *this;
}
//重载下标[]运算符
char &operator[] (int i){
return this->str[i];
}
//重载+运算符,连接两个字符串
const mystring operator+ (const mystring &other)const{
mystring temp;
temp.size=this->size+other.size;
temp.str=new char[size+1];
*temp.str='\0';
strcat(temp.str,this->str);
strcat(temp.str,other.str);
return temp;
}
//重载==预算符,判断两个字符串是否相等
bool operator== (const mystring &other)const{
if(strcmp(this->str,other.str)==0){
return true;
}else{
return false;
}
}
//重载<运算符,判断字符串大小
bool operator< (const mystring &other)const{
if(strcmp(this->str,other.str)<0){
return true;
}else{
return false;
}
}
//重载<=运算符,判断字符串大小
bool operator<= (const mystring &other)const{
if(strcmp(this->str,other.str)<=0){
return true;
}else{
return false;
}
}
//重载>运算符,判断字符串大小
bool operator> (const mystring &other)const{
if(strcmp(this->str,other.str)>0){
return true;
}else{
return false;
}
}
//重载>=运算符,判断字符串大小
bool operator>= (const mystring &other)const{
if(strcmp(this->str,other.str)>=0){
return true;
}else{
return false;
}
}
//重载>=运算符,判断字符串大小
bool operator!= (const mystring &other)const{
if(strcmp(this->str,other.str)!=0){
return true;
}else{
return false;
}
}
//重载<<运算符,执行字符串的流输出
friend ostream &operator<< (ostream &L,mystring &other);
//重载>>运算符,执行字符串的流输出
friend istream &operator>> (istream &L,mystring &other);
};
//全局函数实现<<重载
ostream &operator<< (ostream &L,mystring &other){
L<<other.str<<endl;
return L;
}
//全局函数实现<<重载
istream &operator>> (istream &L,mystring &other){
char s[other.size_t()];
cin>>s;
strcpy(other.c_str(),s);
return L;
}
int main()
{
//有参构造
mystring s1("Hello");
cout<<"s1_size = "<<s1.size_t()<<endl; //size函数
cout<<"s1 = "<<s1.c_str()<<endl; //c_str函数
int n;
cout<<"请输入查询的字符下标: ";
cin>>n;
cout<<"s1_at = "<<s1.at(n)<<endl; //at函数
//拷贝函数
mystring s2(s1);
cout<<"s2_size = "<<s2.size_t()<<endl; //size函数
cout<<"s2 = "<<s2.c_str()<<endl; //c_str函数
int m;
cout<<"请输入查询的字符下标: ";
cin>>m;
cout<<"s2_at = "<<s2.at(m)<<endl; //at函数
//[]
cout<<"[]"<<endl;
int i;
cout<<"输入i= ";
cin>>i;
cout<<s1[i]<<endl;
//+
mystring s3("World");
mystring s4;
s4=s1+s3;
cout<<"+"<<endl;
cout<<s4<<endl;
//==
mystring s5("Hello");
cout<<"=="<<endl;
if(s1==s5){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
//<
mystring s6("xzy");
cout<<"<"<<endl;
if(s1<s6){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
//<=
mystring s7("LKI");
cout<<"<="<<endl;
if(s1<=s7){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
//>
mystring s8("qwer");
cout<<">"<<endl;
if(s1>s8){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
//>=
mystring s9("QAZ");
cout<<">="<<endl;
if(s1>=s9){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
//!=
mystring s10("WSX");
cout<<"!="<<endl;
if(s1!=s9){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
return 0;
}
Day2--C++2
最新推荐文章于 2024-07-24 11:32:27 发布