字符串string

来源:互联网 发布:杭州知彼科技 编辑:程序博客网 时间:2024/06/05 08:53
#ifndef _STRING_
#define _STRING_
#include "iostream"




using std::ostream;
using std::istream;








class String
{
private:
char *s;




public:
String();
String(int n);
String(const char *str);
String(const String& s2);
~String();
String& operator = (const char *str);
String& operator = (const String& s1);
friend String& operator + (const String& s1,const String& s2);
friend bool operator == (const String& s1,const String& s2);
friend bool operator != (const String& s1,const String& s2);
friend bool operator >= (const String& s1,const String& s2);
friend bool operator <= (const String& s1,const String& s2);
friend bool operator >  (const String& s1,const String& s2);
friend bool operator <  (const String& s1,const String& s2);
friend ostream& operator << (ostream& output,const String& s1);
friend istream& operator >> (istream& input ,String& s1);
};






#endif






#include "String.h"
#include "cstring"
#include "iostream"




using std::iostream;
using std::iostream;
using std::endl;
using std::cout;




String s3;




String::String()
{
s=new char[100];
s[0]='\0';
}




String::String(int n)
{
s=new char[n];
s[0]='\0';
}




String::String(const char *str)
{
s=new char[100];
strcpy(s,str);
}




String::String(const String& s2)
{
strcpy(s,s2.s);
}


String::~String()
{
if(s!=NULL)
delete []s;
}




String& String::operator = (const char *str)
{
strcpy(s,str);
return *this; 
}




String& String::operator = (const String& s1)
{
strcpy(s,s1.s);
return *this;
}




String& operator + (const String& s1,const String& s2)
{
extern String s3;
strcat(s3.s,s1.s);
strcat(s3.s,s2.s);




return s3;
}




bool operator == (const String& s1,const String& s2)
{
if(strcmp(s1.s,s2.s)==0)
{
return true;
}
else
{
return false;
}
}


bool operator != (const String& s1,const String& s2)
{
if(strcmp(s1.s,s2.s)==0)
{
return false;
}
else
{
return true;
}
}




bool operator >= (const String& s1,const String& s2)
{
if(strcmp(s1.s,s2.s)>=0)
{
return true;
}
else
{
return false;
}
}
bool operator <= (const String& s1,const String& s2)
{
if(strcmp(s1.s,s2.s)<=0)
{
return true;
}
else
{
return false;
}
}
bool operator >  (const String& s1,const String& s2)
{
if(strcmp(s1.s,s2.s)>0)
{
return true;
}
else
{
return false;
}
}
bool operator <  (const String& s1,const String& s2)
{
if(strcmp(s1.s,s2.s)<0)
{
return true;
}
else
{
return false;
}
}




ostream& operator << (ostream& output,const String& s1)
{
output<<s1.s;
return output;
}




istream& operator >> (istream& input ,String& s1)
{
input>>s1.s;
return input;
}


#include "iostream"
#include "String.h"




using std::cin;
using std::cout;
using std::endl;








int main(int argc, char const *argv[])
{
String A("dog");
cout<<A<<endl;
String B("god");
cout<<B<<endl;
String C;
String D(A);
cout<<D<<endl;
C=A+B;
cout<<C<<endl;
String E("A");




if(A==D) cout<<"A==D"<<endl;
if(A<=B) cout<<"A<=B"<<endl;
if(A>=E) cout<<"A>=E"<<endl;
if(A<B) cout<<"A<B"<<endl;
if(A>E) cout<<"A>E"<<endl;
if(A!=B) cout<<"A!=B"<<endl;
return 0;
0 0
原创粉丝点击