




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、/ 我希望利用這個String類和其他少數(shù)類外定義的函數(shù)可以實現(xiàn)對 String類的對象和C字符串 實現(xiàn) "無縫操作" , 即對兩者能使用相同的表達式進行類似或相同的操作而無需加以區(qū)分/ 類外面定義類中的函數(shù)(成員或友元)定義順序與他們在類中出現(xiàn)的順序一致,且用注釋編號以便查看/ char * 為0的情況有點煩人,因為字符串處理函數(shù)不能處理它,而且不能使用cout輸出,使得編程時需要增加很多額外的代碼/ 刪掉了轉(zhuǎn)換函數(shù)const char *,因為它使得減法和某些邏輯運算出錯(overloads have two similar covernsions)/ 類String中
2、的初始化函數(shù)String( const char * )相當重要#include <iostream>#include <cstring>using namespace std;class String protected:int Length;char *Sp;public:String() Sp=0; Length=0; String( const String & ); /No.1.0String( const char * s )if (!s) Length=0; Sp=0; return ; Length = strlen( s ) ;Sp = new
3、 char Length + 1 ;strcpy ( Sp, s );void show() if(Sp) cout<< Sp ; /輸出字符串friend ostream & operator << ( ostream & , const String & ); /No.1.1 String() if(Sp) delete Sp; friend bool IsIn( const String &, const char ); /No.2 母字符串為第一個參數(shù),要查找的字符為第二個參數(shù)/能處理(String,char), (char *,
4、char)friend bool IsSubStr( const String &, const String & ); /No.3 母字符串為第一個參數(shù),要查找的子字符串為第二個參數(shù)/能處理(String,String),(String,char*),(char *,String)和(char *,char *)/默認空字符串為任意字符串的子字符串int GetLen() return Length; /取長度char * GetString() return Sp; /取字符串/當指針值為0時,返回后,在調(diào)用函數(shù)中的操作可能會出事String & operator
5、= ( String & ); /No.4 /能處理 String=String 和String= char */因為 operator = 只能是成員函數(shù),所以不能處理第一個操作數(shù)是char*類型的情況/下面從No.5到No.7.6重載的操作符的操作數(shù)必須至少有一個是String類的對象,/如果兩個操作數(shù)都是char*類型的,則必須在至少其中一個之前加上強制轉(zhuǎn)換符String,/這可以說是一個比較大的缺陷friend String operator + ( const String &, const String & ); /No.5friend String ope
6、rator - ( const String &, const String & ); /No.6friend bool operator < ( const String &, const String & ); /No.7.1friend bool operator > ( const String &, const String & ); /No.7.2friend bool operator <= ( const String &, const String & ); /No.7.3friend bool
7、 operator >= ( const String &, const String & ); /No.7.4friend bool operator = ( const String &, const String & ); /No.7.5friend bool operator != ( const String &, const String & ); /No.7.6String operator += ( const String &) ; /No.8/能處理 String+=String 和 String+=char*,
8、下同/對于char*+=String 和 char*+=char*,必須在char*前加上強制轉(zhuǎn)換符String,下同String operator -= ( const String & ); /No.9; / Start defining functions in class String/No.1.0String:String( const String &s )Length = s.Length;if ( s.Sp ) Sp = new char Length + 1 ;strcpy( Sp, s.Sp );else Sp=0;/No.1.1ostream &
9、operator << ( ostream &stream, const String &str )if(str.Sp) stream << str.Sp ;return stream;/No.2bool IsIn ( const String &str, const char c )if ( !str.Sp ) return 0;const char *p = str.Sp ; while( *p ) if( *p + = c ) return 1;return 0;/No.3bool IsSubStr ( const String &
10、str1, const String &str2 )if ( !str2.Sp ) return 1;else if ( !str1.Sp ) return 0;else if ( strstr( str1.Sp, str2.Sp ) ) return 1;else return 0;/No.4String & String:operator = ( String &str )if (Sp) delete Sp;Length = str.Length;if( str.Sp ) Sp = new char Length + 1 ;strcpy( Sp, str.Sp );
11、else Sp=0;return *this;/No.5String operator + ( const String &s1, const String &s2 )String temp;temp.Length = s1.Length + s2.Length;if( !temp.Length ) return temp; temp.Sp = new char temp.Length + 1 ;if( s1.Sp ) strcpy( temp.Sp, s1.Sp );if( s2.Sp ) strcat( temp.Sp, s2.Sp );return temp;/No.6S
12、tring operator - ( const String &str1, const String &str2 ) if( (! str1.Length) | (! str2.Length) ) return str1;int i=0;char *p1=str1.Sp, *p2=0;String temp;if( p2=strstr( str1.Sp, str2.Sp ) ) temp.Length = str1.Length - str2.Length;temp.Sp = new char temp.Length + 1 ;while( p1<p2 ) temp.S
13、pi+=*p1+ ;p1 += str2.Length;while( temp.Spi+=*p1+ ) ;elsetemp.Length = str1.Length;temp.Sp = new char str1.Length + 1 ;strcpy( temp.Sp, str1.Sp );return temp;/No.7.1bool operator < ( const String &str1, const String &str2 )if( str1.Sp && str2.Sp ) return ( strcmp( str1.Sp, str2.Sp
14、 ) < 0 );else if ( str2.Sp ) return true;else return false;/No.7.2bool operator > ( const String &str1, const String &str2 )if ( str1.Sp && str2.Sp ) return ( strcmp( str1.Sp, str2.Sp ) > 0 );else if ( str1.Sp ) return true;else return false;/No.7.3bool operator <= ( cons
15、t String &str1, const String &str2 )return !( str1 > str2 );/No.7.4bool operator >= ( const String &str1, const String &str2 )return !( str1 < str2 );/No.7.5bool operator = ( const String &str1, const String &str2 )if( str1.Sp && str2.Sp ) return !( strcmp( s
16、tr1.Sp, str2.Sp ) );else if ( (!str1.Sp) && (!str2.Sp) ) return true;else return false;/No.7.6bool operator != ( const String &str1, const String &str2 )return !( str1=str2 );/No.8String String:operator += ( const String &str )(*this) = (*this) + str;return *this;/No.9String Stri
17、ng:operator -= ( const String &str )(*this) = (*this) - str;return *this;int main()String str1("I love you"), str2(str1);str1.show();cout << 'n' << str2 << 'n'cout <<"n-n"cout.setf( ios:boolalpha );char ch='o'String str3("y
18、ou");cout<< IsIn( str1, ch ) <<endl;cout<< IsIn( "I love you", ch ) <<endl;cout<< IsSubStr( str1, str3 ) << endl;cout<< IsSubStr( str1, "you" ) << endl;cout<< IsSubStr( "I love you", str3 ) << endl;cout<
19、;< IsSubStr( "I love you", "you" ) << endl;cout<< "n-n"cout<< "The length of str1 is " << str1.GetLen() <<endl;char *s1 = str1.GetString();cout<< "The string of str1 is : " << s1 << endl;cout<<
20、 "n-n"String str4("I love you"), str5(", Jesus Christ"), str6, str7("Christ"), str8;str6 = str4 + str5;cout<< str6 <<endl;cout<< str4 + ", Jesus Christ" <<endl;cout<< "I love you" + str5 <<endl;cout<&l
21、t; "I love you" + (String)", Jesus Christ" <<endl;cout<< str6 - str7 <<endl;cout<< str6 - "Christ" <<endl;cout<< "I love you, Jesus Christ" - str7 <<endl;cout<< "I love you, Jesus Christ" - (String)"Christ" <<endl;str8 = str4 + str5 - str7 - " you," - "I "cout<< str8 <<endl;cout<<"n-n"String str9("Good"), str
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 智慧城市環(huán)境管理與可持續(xù)發(fā)展
- 教育技術(shù)創(chuàng)新對學校發(fā)展的推動作用
- 能效監(jiān)測與智能電網(wǎng)的技術(shù)集成應(yīng)用
- 公交優(yōu)先戰(zhàn)略2025年城市交通擁堵治理的公共交通車輛更新報告
- 廣西河池市2024年九上化學期末達標檢測試題含解析
- 江蘇省連云港灌云縣聯(lián)考2025屆化學九年級第一學期期末教學質(zhì)量檢測模擬試題含解析
- 外交學院《書法藝術(shù)概論》2023-2024學年第一學期期末試卷
- 湖南省懷化市中學方縣2024年數(shù)學七年級第一學期期末檢測模擬試題含解析
- 新能源領(lǐng)域的科技創(chuàng)新及推廣應(yīng)用分析報告
- 廣東機電職業(yè)技術(shù)學院《巖石力學基礎(chǔ)》2023-2024學年第一學期期末試卷
- 遵義市仁懷市選聘城市社區(qū)工作者考試真題2024
- DB45∕T 1098-2024 橡膠瀝青路面施工技術(shù)規(guī)范
- 2025年沈陽水務(wù)集團招聘筆試沖刺題2025
- 《蠶絲》教學課件
- 東莞東華分班數(shù)學試卷
- 江西省金控科技產(chǎn)業(yè)集團有限公司招聘筆試題庫2025
- 2025年湖北省中考英語試題(附答案)
- 2025至2030中國家用血壓計行業(yè)發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報告
- 吉林省長春市2023?2024學年高二下冊期末考試數(shù)學科試卷附解析
- 主管護師《相關(guān)專業(yè)知識》考試真題及答案(2025年)
- 綠化所倉庫管理制度
評論
0/150
提交評論