#include <iostream>
#include<string.h>
using namespace std;
class MyString{
private:
char str[1024];
unsigned len;/*字符串长度*/
public:
MyString();
MyString& operator=(const char *mstr)/*常量字符串赋值*/
{
len=strlen(mstr);
strcpy(str,mstr);
return*this;
}
MyString& operator=(const MyString mstr)/*同类型赋值*/
{
strcpy(str,mstr.str);
len=mstr.len;
return*this;
}
MyString operator+(const MyString mstr)/*字符串拼接*/
{
int l=len;
l=l+strlen(mstr.str);
char*s=new char [l];
strcpy(s,str);
strcpy(s+len,mstr.str);
MyString m;
m=s;
return m;
}
char &operator [](int i)/*查询串内字符*/
{
return str[i];
}
bool operator==(const MyString mstr)/*判断是否相等*/
{
if(strcmp(str,mstr.str)==0)return true;
else return false;
}
bool operator!=(const MyString mstr)/*判断是否不等*/
{
if(strcmp(str,mstr.str)==0)return false;
else return true;
}
void operator+=(const MyString mstr)/*字符串拼接赋值*/
{
strcpy(str+len,mstr.str);
len+=mstr.len;
}
bool operator>(const MyString mstr)/*按典序比较*/
{
if(strcmp(str,mstr.str)>0)return true;
else return false;
}
bool operator<(const MyString mstr)/*同上*/
{
if(strcmp(str,mstr.str)<0)return true;
else return false;
}
friend ostream &operator<<(ostream &out,const MyString mstr)/*友元输出流符号重载*/
{
for(int i=0;i<(int)mstr.len;i++)
{
out<<mstr.str[i];
}
return out;
}
};
MyString::MyString()
{
len=0;
}
int main()
{
char ts[1024];
cin>>ts;
MyString s;
s = ts;
cin>>ts;
MyString ss;
ss = ts;
if(ss == s)
cout<<"equal"<<endl;
if(ss != s)
{
cout<<"Not equal"<<endl;
if(s > ss)
cout<<"s da yu ss"<<endl;
if(s < ss)
cout<<"s xiao yu ss"<<endl;
}
ss += s;
MyString sss;
sss = s + ss;
sss[0] = '$';
cout<<sss<<endl;
return 0;
}
字符串拼接strcpy
最新推荐文章于 2024-07-11 10:09:16 发布