64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
using namespace std;
class String
{
char * str;
int len;
static int num_string;
static const int CINLIM = 80;
public:
String(const char * s);
String();
String(const String &);
~String();
int length()const;
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i)const;
friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st, const String &st2);
friend bool operator==(const String &st,const String &st2);
friend ostream & operator<<(ostream & os, const String &st);
friend istream & operator>>(istream & is, String &st);
static int HowMany();
};
int String::num_string = 0;
int String::HowMany()
{ return num_string;}
String::String(const char * s)
{ len = strlen(s);
str = new char[len+1];
strcpy(str,s);
num_string++;
}
String::String()
{ len = 4 ;
str = new char[1];
str[0] = '\0';
num_string++;
}
String::String(const String & st)
{ len = st.len;
str = new char[len+1];
strcpy(str,st.str);
num_string++;
}
String::~String()
{ num_string--;
delete [] str;
}
String & String::operator=(const String & st)
{ if (this == &st) return *this;
delete []str;
len = st.len;
str = new char[len+1];
strcpy(str,st.str);
return *this;
}
String & String::operator=(const char * s)
{ delete [] str;
len = strlen(s);
str = new char[len+1];
strcpy(str,s);
return *this;
}
char & String::operator[](int i)
{ return str[i];}
const char & String::operator[](int i)const
{ return str[i];}
bool operator<(const String &st,const String &st1)
{ return (strcmp(st.str,st1.str) < 0);}
bool operator>(const String & st,const String &st1)
{return st1.str < st.str;}
bool operator==(const String &st,const String &st1)
{ return (strcmp(st.str,st1.str) == 0);}
ostream & operator<<(ostream & os,const String &st)
{ os << st.str;
return os;
}
istream & operator>>(istream & is,String &st)
{ is>>st.str;
return is;
}
int main(void)
{
String a;
cin >> a;
String b = "wo ai ni";
cout << a << b << endl;;
if (a > b ) cout << "sssssssssssssssssssssss" << endl;
b = a;
cout << a << b << endl;
if (a == b) cout << "xxxxxxxxxxxxxxxxxxxxxxxx" << endl;
system("pause");
return 0;
}
ostream & operator<<(ostream & os,const String &st)
{ os << st.str;
return os;
}
istream & operator>>(istream & is, String &st)
{ char temp[80];
is.get(temp,80);
if(is)
st = temp;
while(is && is.get() != '\n')
continue;
return is;
}
#include <iostream>
#include <string>
using namespace std;
class String
{
char * str;
int len;
static int num_string;
static const int CINLIM = 80;
public:
String(const char * s);
String();
String(const String &);
~String();
int length()const;
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i)const;
friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st, const String &st2);
friend bool operator==(const String &st,const String &st2);
friend ostream & operator<<(ostream & os, const String &st);
friend istream & operator>>(istream & is, const String &st);
static int HowMany();
};
int String::num_string = 0;
int String::HowMany()
{ return num_string;}
String::String(const char * s)
{
len = strlen(s);
str = new char[len+1];
strcpy(str,s);
num_string++;
}
String::String()
{
len = 4 ;
str = new char[1];
str[0] = '\0';
num_string++;
}
String::String(const String & st)
{
len = st.len;
str = new char[len+1];
strcpy(str,st.str);
num_string++;
}
String::~String()
{
num_string--;
delete [] str;
}
String & String::operator=(const String & st)
{
if (this == &st) return *this;
delete []str;
len = st.len;
str = new char[len+1];
strcpy(str,st.str);
return *this;
}
String & String::operator=(const char * s)
{
delete [] str;
len = strlen(s);
str = new char[len+1];
strcpy(str,s);
return *this;
}
char & String::operator[](int i)
{
return str[i];
}
const char & String::operator[](int i)const
{ return str[i];}
bool operator<(const String &st,const String &st1)
{
return (strcmp(st.str,st1.str) < 0);}
bool operator>(const String & st,const String &st1)
{
return st1.str < st.str;}
bool operator==(const String &st,const String &st1)
{
return (strcmp(st.str,st1.str) == 0);}
ostream & operator<<(ostream & os,const String &st)//去掉const
{
os << st.str;
return os;
}
istream & operator>>(istream & is,const String &st)//去掉const ,返回类型改为istream
{
char temp[80];
is.get(temp,80);
if(is)
strcpy(st.str, (const char*)temp);//这里做一些修改。
while(is && is.get() != '\n')
continue;
return is;
}
int main(void)
{
String a;
cin >> a;
String b = "wo ai ni";
cout << a << b << endl;;
if (a > b ) cout << "sssssssssssssssssssssss" << endl;
b = a;
cout << a << b << endl;
if (a == b) cout << "xxxxxxxxxxxxxxxxxxxxxxxx" << endl;
system("pause");
return 0;
}