1 Star 0 Fork 0

王赫辰/c语言

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
运算符重载 3.19 KB
一键复制 编辑 原始数据 按行查看 历史
王赫辰 提交于 2023-12-27 23:22 +08:00 . add 运算符重载.
include<iostream>
using namespace std;
class Mystring
{
private:
char str[1024];
unsigned len;
public:
Mystring() { len = 0; };
Mystring& operator =(const char* str1)
{
int i = 0;
for (i = 0; i < 1024&&str1[i]!='\0'; i++)
{
str[i] = str1[i];
len++;
}
return *this;
}
friend ostream& operator <<(ostream& out, Mystring good)
{
int j = 0;
for (j = 0; j <good. len; j++)
{
out << good.str[j];
}
return out;
}
Mystring& operator=(const Mystring good)
{
len = good.len;
int i = 0;
for (i = 0; i < len; i++)
{
str[i] = good.str[i];
}
return *this;
}
Mystring& operator+(const Mystring good)
{
int j = len;
len = len + good.len;
int i = 0;
for (j; j < len; j++)
{
str[j] = good.str[i];
i++;
}
return *this;
}
char &operator[](int a)
{
return str[a];
}
bool operator==(const Mystring good)
{
if (good.len != len)
{
return false;
}
int i = 0;
for (i = 0; i < len; i++)
{
if (str[i] != good.str[i])
{
return false;
}
}
return true;
}
bool operator!=(Mystring good)
{
if (good.len != len)
{
return true;
}
int i = 0;
for (i = 0; i < len; i++)
{
if (str[i] == good.str[i])
{
return false;
}
}
return true;
}
/*bool operator==(const Mystring mstr) {
if (len != mstr.len) {
return false;
}
for (int i = 0; i < len; i++) {
if (str[i] != mstr.str[i]) {
return false;
}
}
return true;
}*/
/*bool operator!=(const Mystring mstr) {
return !(*this == mstr);
}*/
Mystring& operator+=(const Mystring good)
{
int i = len;
len = len + good.len;
int j = 0;
for (i; i < len; i++)
{
str[i] = good.str[j];
j++;
}
return *this;
}
bool operator>(const Mystring good)
{
int i;
int j;
if (len <= good.len)
{
i = len;
j = 0;
}
else
{
i = good.len;
j = 1;
}
int k = 0;
for (k = 0; k < i; k++)
{
if (str[k] > good.str[k])
{
return true;
}
else if (str[k] < good.str[k])
{
return false;
}
else
{
}
}
if (j == 0)
{
return false;
}
else
{
return true;
}
}
bool operator<(const Mystring good)
{
int i;
int j;
if (len <= good.len)
{
i = len;
j = 0;
}
else
{
i = good.len;
j = 1;
}
int k = 0;
for (k = 0; k < i; k++)
{
if (str[k] < good.str[k])
{
return true;
}
else if (str[k] > good.str[k])
{
return false;
}
else
{
}
}
if (j == 0)
{
return true;
}
else
{
return false;
}
}
};
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;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/hzau2303/c-language.git
git@gitee.com:hzau2303/c-language.git
hzau2303
c-language
c语言
master

搜索帮助