// String.cpp : Powered By Null AT 2006-11-28.#include "stdafx.h"#include "string.h"#include "iostream.h"#include "stdlib.h"//错误信息列表,PS:应该有一个类似的系统信息吧?const char *ErrorCode
[4]={"NoMemory","UnderFlow","OverFlow","OutOfBounds"};class String
{ char *Str
; //数据 int Size
; //数据长度 void Error
(int ErrorType
,char *StopFun
,int BadIndex
) const; //错误指示public: String
(void); //构造函数 String
(const char *str
); String
(const String
&s
); ~String
(void); //析构函数 String
& operator=(const String
&s
); //赋值 String
& operator=(const char *str
); friend int operator==(const char *str
,const String
&s
);//对比 int operator<(const String
&s
) const; //比较 int operator<(const char *str
) const; friend int operator<(const char *str
,const String
&s
); int operator>(const String
&s
) const; //比较 int operator>(const char *str
) const; friend int operator>(const char *str
,const String
&s
); String
operator+(const String
&s
) const; //相加 String
operator+(const char *str
) const; friend String
operator+(const char *str
,const String
&s
); friend istream
&operator>>(istream
&istr
, String
&s
);//控制输入 friend ostream
&operator<<(ostream
&ostr
, const String
&s
);//控制输出 char* operator*(void) const; //返回字符串内容 int Length
(void) const; //返回字符串};//错误指示,事实上,在现在的系统下,很难发生这类错误inline void String
::Error
(int ErrorType
,char *StopFun
,int BadIndex
=0) const{ cout
<<"=====程序发生如下错误====="<<endl
; cout
<<"错误类型:"<<ErrorCode
[ErrorType
]<<endl
; cout
<<"错误位置:"<<StopFun
<<endl
; cout
<<"=======程序已经结束======="<<endl
; exit
(0);}//构造函数inline String
::String
(void){ Str
=new char[1]; Str
[0]='\0'; Size
=0;}inline String
::String
(const char *str
){ try { Str
=new char[strlen
(str
)+1]; strcpy
(Str
,str
); Size
=strlen
(Str
); } catch(int err
) { Error
(err
,"String::String(const char *str)"); }}inline String
::String
(const String
&s
){ try { Str
=new char[s
.Size
+1]; strcpy
(Str
,s
.Str
); Size
=s
.Size
; } catch(int err
) { Error
(err
,"String::String(const String &s)"); }}//析构函数inline String
::~String
(void){ delete []Str
;}//赋值inline String
& String
::operator=(const String
&s
){ if (this!=&s
) //自己是不能给自己赋值的 { try { Size
=s
.Size
; delete []Str
; Str
=new char[Size
+1]; strcpy
(Str
, s
.Str
); } catch(int err
) { Error
(err
,"String& String::operator=(const String &s)"); } } else { cout
<<"不能给自己赋值!"<<endl
; } return *this;}inline String
& String
::operator=(const char *str
){ try { Size
=strlen
(str
); delete []Str
; Str
=new char[Size
+1]; strcpy
(Str
,str
); } catch(int err
) { Error
(err
,"String& String::operator=(const char *str)"); } return *this;}//对比int operator==(const char *str
,const String
&s
){ return strcmp
(str
,s
.Str
)==0;}//比较inline int String
::operator<(const String
&s
) const{ return strcmp
(Str
,s
.Str
)<0;}inline int String
::operator<(const char *str
) const{ return strcmp
(Str
,str
)<0;}int operator<(const char *str
,const String
&s
){ return strcmp
(str
,s
.Str
)<0;}//比较inline int String
::operator>(const String
&s
) const{ return strcmp
(Str
,s
.Str
)>0;}inline int String
::operator>(const char *str
) const{ return strcmp
(Str
,str
)>0;}int operator>(const char *str
,const String
&s
){ return strcmp
(str
,s
.Str
)>0;}inline String String
::operator+(const char *str
) const{ String S
; try { delete []S
.Str
; S
.Str
=new char[Size
+strlen
(str
)+1]; strcpy
(S
.Str
,Str
); strcat
(S
.Str
,str
); S
.Size
=strlen
(S
.Str
); } catch(int err
) { Error
(err
,"String String::operator+(const char *str) const"); } return S
;}//相加inline String String
::operator+(const String
&s
) const{ String S
; try { delete []S
.Str
; S
.Str
=new char[Size
+s
.Size
+1]; strcpy
(S
.Str
,Str
); strcat
(S
.Str
,s
.Str
); S
.Size
=strlen
(S
.Str
); } catch(int err
) { Error
(err
,"String String::operator+(const String &s) const"); } return S
;}String
operator+(const char *str
,const String
&s
){ String S
; try { S
.Str
=new char[strlen
(str
)+s
.Size
+1]; strcpy
(S
.Str
,str
); strcat
(S
.Str
,s
.Str
); S
.Size
=strlen
(S
.Str
); } catch(int err
) { s
.Error
(err
,"String operator+(const char *str,const String &s)"); } return S
;}//控制输出ostream
&operator<<(ostream
&ostr
, const String
&s
){ try { char *ss
; int n
=0; ss
=new char[s
.Size
+1]; for(n
=0;n
<s
.Size
;n
++) ss
[n
]='-'; ss
[s
.Size
]='\0'; ostr
<<endl
; ostr
<<"------------"<<ss
<<endl
; ostr
<<"存在字符串:"<<s
.Str
<<endl
; ostr
<<"字符串长度:"<<s
.Size
<<endl
; ostr
<<"------------"<<ss
<<endl
; ostr
<<endl
; } catch(int err
) { s
.Error
(err
,"ostream &operator<<(ostream &ostr, const String &s)"); } return ostr
;}//控制输入istream
&operator>>(istream
&istr
,String
&s
){ try { char str
[100]; cout
<<"请输入一个小于100字节的字符串:"<<endl
; istr
>>str
; delete []s
.Str
; s
.Size
=strlen
(str
); s
.Str
=new char[s
.Size
+1]; strcpy
(s
.Str
,str
); } catch(int err
) { s
.Error
(err
,"istream &operator<<(istream &istr,String &s)"); } return istr
;}//返回字符串内容inline char* String
::operator*(void) const{ return Str
;}//返回字符串长度inline int String
::Length
(void) const{ return Size
;}呵呵,百度空间发表文章有字数限制,主函数就不能 String 类写在一起了。
请按以下操作:
新建一个简单的控制台工程。
将
hetaoos/blog/item/16b3ca5cce830d43faf2c031.html中的源码复制工程文件中,
然后再修改主函数如下:
(PS:我就不多解释了,很容易看懂的。。。)
int main()
{
String P(""),B("HeTaoOS");
P=P+B;
P=P;
cout<<P;
String s1("String"),s2("Class"),s3,s4;
s4=s1+ " " +s2;
cout<<s4;
s4=s4+" In MFC is";
s4=s4+" Cstring Class";
cout<<s4;
cout<<"请分别输入两个小于100字节的字符串:"<<endl;
char str1[100],str2[100];
String SS1,SS2;
cin>>str1;
cin>>str2;
SS1=str1;
SS2=str2;
cout<<"OK\t"<<*SS1<<"\tOK"<<endl;
if(SS1>SS2)
cout<<"SS1 Max"<<endl;
else if(SS1<SS2)
cout<<"SS2 Max"<<endl;
else
cout<<"SS1==SS2"<<endl;
cout<<endl<<"SS1"<<SS1<<"SS2"<<SS2<<endl;
if (str1==SS2)
cout<<"str1 与 SS2 相等!"<<endl;
else
cout<<"str1 与 SS2 不相等!"<<endl;
cin>>SS1;
cout<<SS1;
return 0;
}
阅读(1252) | 评论(0) | 转发(0) |