定义一个MyString类,该类的声明如下:
输入描述本题无输入
输出描述参考输出样例
提示只需要提交需要实现的运算符函数的代码,类的声明和主函数不需要提交
样例输入
样例输出
0 0 1 0
0 1
HelloWorld
China China China
HelloWorldChina
anihCdlroWolleH
#include<stdio.h>
#include <iostream>
#pragma warning(disable:4996)
#include <string>
ostream& operator<<(ostream& out, const MyString& str)
{
out << str.ptr;
return out;
}
int operator==(const MyString& s1, const MyString& s2)
{
if (strcmp(s1.ptr, s2.ptr) == 0)return 1;
else return 0;
}
int operator!=(const MyString& s1, const MyString& s2)
{
if (strcmp(s1.ptr, s2.ptr) == 0)return 0;
else return 1;
}
int operator<(const MyString& s1, const MyString& s2)
{
if (strcmp(s1.ptr, s2.ptr) < 0)return 1;
else return 0;
}
int operator<=(const MyString& s1, const MyString& s2)
{
if (strcmp(s1.ptr, s2.ptr) > 0)return 0;
else return 1;
}
int operator>(const MyString& s1, const MyString& s2)
{
if (strcmp(s1.ptr, s2.ptr) > 0)return 1;
else return 0;
}
int operator>=(const MyString& s1, const MyString& s2)
{
if (strcmp(s1.ptr, s2.ptr) < 0)return 0;
else return 1;
}
MyString operator+(const MyString& s1, const MyString& s2)
{
MyString s;
s.ptr = new char[strlen(s1.ptr) + strlen(s2.ptr) + 1];
strcpy(s.ptr, s1.ptr);
strcat(s.ptr, s2.ptr);
return s;
}
MyString &MyString :: operator=(const MyString& s)
{
ptr = new char[strlen(s.ptr) + 1];
strcpy(ptr, s.ptr);
return *this;
}
MyString &MyString :: operator+=(const MyString& s)
{
char* temp;
temp = ptr;
ptr = new char[strlen(temp) + strlen(s.ptr) + 1];
strcpy(ptr, temp);
delete[]temp;
strcat(ptr, s.ptr);
return *this;
}
char MyString::operator[](int i)
{
return this->ptr[i];
}