11.6.cpp
来自「这是C++的一部分练习程序!对初学者有一定的帮助作用。」· C++ 代码 · 共 77 行
CPP
77 行
#include <iostream.h>
#include <string.h>
class String
{
public:
String()
{ str=NULL; }
String(char *s)
{ str=s; }
friend bool operator >(String &s1,String &s2);
friend bool operator <(String &s1,String &s2);
friend bool operator ==(String &s1,String &s2);
void Print()
{ cout<<str; }
private:
char *str;
};
bool operator >(String &s1,String &s2)
{
if(strcmp(s1.str,s2.str)>0)
return true;
else
return false;
}
bool operator <(String &s1,String &s2)
{
if(strcmp(s1.str,s2.str)<0)
return true;
else
return false;
}
bool operator ==(String &s1,String &s2)
{
if(strcmp(s1.str,s2.str)==0)
return true;
else
return false;
}
void compare(String &s1,String &s2)
{
if(s1>s2)
{
s1.Print();
cout<<'>';
s2.Print();
}
else if(s1<s2)
{
s1.Print();
cout<<'<';
s2.Print();
}
else
{
s1.Print();
cout<<"==";
s2.Print();
}
cout<<endl;
}
void main()
{
String s1("continue"),s2("break"),s3("while"),s4("continue");
compare(s1,s2);
compare(s2,s3);
compare(s1,s4);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?