




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、#include class szcl int e; public: szcl ( ) e = 0; szcl ( int value ) e = value; int get_value ( ) return e; main ( ) szcl a13 = 3, 5, 7 , *elem; for ( int i=0, i3, i+ ) cout a1i.get_value ( ) “n”; /打印靜態數組打印靜態數組 elem = &a1; for ( int i=0, i3, i+ ) cout elemget_value( ) “n”; /打印動態數組打印動態數組 elem+;
2、return 0; #include #include template class array type *elements; /數組存放空間數組存放空間 int arraysize; /當前長度當前長度 void getarray ( ); /建立數組空間建立數組空間 public: array(int size=defaultsize ); array(const array& x ); array( ) delete elements; array & operator = ( const array & a ); type& operato ( int
3、 i ); array operator type * ( ) const return elements; int length ( ) const return arraysize; void resize ( int sz ); template void array:getarray ( ) /私有函數:創建數組存儲空間私有函數:創建數組存儲空間 elements = new typearraysize; if ( elements = 0 ) cerr memory allocation error endl; template void array:array ( int sz )
4、 /構造函數構造函數 if ( sz = 0 ) cerr invalid array size endl; return; arraysize = sz; getarray ( ); template array: array ( const array & x ) /復制構造函數復制構造函數 int n = arraysize = x.arraysize; elements = new typen; if ( elements = 0 ) cerr memory allocation error endl; type *srcptr = x.elements; type *dest
5、ptr = elements; while ( n- ) * destptr+ = * srcptr+; template type & array:operator ( int i ) /按數組名及下標按數組名及下標i,取數組元素的值,取數組元素的值 if ( i arraysize-1 ) cerr index out of range endl; return elementi; positioni = positioni -1 + numberi -1 template void array:resize (int sz) if ( sz = 0 & sz != arr
6、aysize ) type * newarray = new typesz; if ( newarray = 0 ) cerr memory allocation error endl; int n = ( sz = arraysize ) ? sz : arraysize; type *srcptr = elements; type *destptr = newarray; while ( n- ) * destptr+ = * srcptr+; delete elements; elements = newarray; arraysize = sz; 時時 0 0, ,) )( (時時 0
7、 0, ,) )( (ililociiloc1loc ( i ) = loc ( i - -1 ) + l =+ i*l11211101122212021121110110201000mnanananamaaaamaaaamaaaaa m1, m2, m3, , mnnnjnjkkjnnnnnnimiimimmmimmmiiiiloc111143232121),(, , , ()template class seqlist type *data; /順序表存儲數組順序表存儲數組 int maxsize; /最大允許長度最大允許長度 int last; /當前最后元素下標當前最后元素下標publ
8、ic: seqlist ( int maxsize = defaultsize ); seqlist ( ) delete data; int length ( ) const return last+1; int find ( type & x ) const;int isin ( type & x );int insert ( type & x, int i );int remove ( type & x ); int next ( type & x ) ;int prior ( type & x ) ; int isempty ( ) re
9、turn last =-1; int isfull ( ) return last = maxsize-1; type & get ( int i ) return i last?null : datai; template seqlist:seqlist ( int sz ) /構造函數構造函數 if ( sz 0 ) maxsize = sz; last = -1; data = new typemaxsize; template int seqlist:find ( type & x ) const /搜索函數:在表中從前向后順序查找搜索函數:在表中從前向后順序查找 x
10、int i = 0; while ( i last ) return -1; else return i;順序搜索圖示順序搜索圖示 x = 48 x = 50niniicpacn10=212)(11)2(111)(1=10nnnnnninacnni 221)(1)(1 0)1(11)(11=0nnnnnninnamnnitemplate int seqlist:insert ( type & x, int i )/在表中第在表中第 i 個位置插入新元素個位置插入新元素 x if ( i last+1 | last = maxsize- -1 ) return 0; /插入不成功插入不成
11、功 else last+; for ( int j=last; ji; j- ) dataj = dataj -1; datai = x; return 1; /插入成功插入成功 102121)(11)(1=ninnnninnamn template int seqlist:remove ( type & x ) /在表中刪除已有元素在表中刪除已有元素 x int i = find (x); /在表中搜索在表中搜索 x if ( i = 0 ) last- ; for ( int j=i; j=last; j+ ) dataj = dataj+1; return 1; /成功刪除成功刪
12、除 return 0; /表中沒有表中沒有 x template void union ( seqlist & la, seqlist & lb ) int n = la.length ( ); int m = lb.length ( ); for ( int i=1; i=m; i+ ) type x = lb.get(i); /在在lb中取一元素中取一元素 int k = la.find (x); /在在la中搜索它中搜索它 if ( k = -1 ) /若未找到插入它若未找到插入它 la.insert (n+1, x); n+; template void interse
13、ction ( seqlist & la, seqlist & lb ) int n = la.length ( ); int m = lb.length ( ); int i=1; while ( i n ) type x = la.get(i); /在在la中取一元素中取一元素 int k = lb.find (x); /在在lb中搜索它中搜索它 if ( k = -1 ) la.remove (i); n-; else i+; /未找到在未找到在la中刪除它中刪除它 iniinnnxaxaxaxaaxp02210 )(nn階多項式階多項式pn(x)有有n+1項。項。u 系
14、數系數 a0, a1, a2, , anu 指數指數 0, 1, 2, , n。按升冪排列。按升冪排列class polynomial public: polynomial ( ); /構造函數構造函數 int operator ! ( ); /判是否零多項式判是否零多項式 coefficient coef (exponent e); exponent leadexp ( ); /返回最大指數返回最大指數 polynomial add (polynomial poly); polynomial mult (polynomial poly); float eval ( float x); /求值
15、求值 #include class power double x; int e; double mul; public: power (double val, int exp); double get_power ( ) return mul; ; power:power (double val, int exp) /按按val值計算乘冪值計算乘冪 x = val; e = exp; mul = 1.0; if (exp = 0 ) return; for ( ; exp0; exp-) mul = mul * x; main ( ) power pwr ( 1.5, 2 ); cout pw
16、r.get_power ( ) “n”; private: int degree; float coef maxdegree+1; pl.degree = n pl.coefi = ai, 0 i nprivate: int degree; float * coef; polynomial:polynomial (int sz) degree = sz; coef = new float degree + 1; : class polynomial;class term /多項式的項定義多項式的項定義friend polynomial;private: float coef; /系數系數 in
17、t exp; /指數指數;class polynomial /多項式定義多項式定義public: private: static term termarraymaxterms; /項數組項數組 static int free; /當前空閑位置指針當前空閑位置指針 / term polynomial:termarraymaxterms; / int polynomial:free = 0; int start, finish; /多項式始末位置多項式始末位置 a(x) = 2.0 x1000+1.8 b(x) = 1.2 + 51.3x50 + 3.7x101 兩個多項式存放在兩個多項式存放在t
18、ermarray中中polynomial polynomial:add ( polynomial b ) polynomial c; int a = start; int b = b.start; c.start = free; float c; while ( a = finish & b : newterm ( termarrayb.coef, termarrayb.exp ); b+; break; case : newterm ( termarraya.coef, termarraya.exp ); a+; for ( ; a=finish; a+ ) /a未檢測完時未檢測完時
19、 newterm ( termarraya.coef, termarraya.exp ); for ( ; b= maxterms ) cout too many terms in polynomials” endl; return; termarrayfree.coef = c; termarrayfree.exp = e; free+; 0000015003901700000000006022280000000001100910000b 0000280000000091039000000006000017000110150022000a6776 template class sparsem
20、atrix int rows, cols, terms; /行行/列列/非零元素數非零元素數 trituple smarraymaxterms; public: /三元組表三元組表 sparsematrix ( int maxrow, int maxcol ); sparsematrix transpose ( ); /轉置轉置 sparsematrix /相加相加 add ( sparsematrix b ); sparsematrix /相乘相乘 multiply ( sparsematrix b ); template class sparsematrix; template class
21、 trituple friend class sparsematrix private: int row, col;/非零元素所在行號非零元素所在行號/列號列號 type value; /非零元素的值非零元素的值 r ra aw w c co ol l v va al lu ue e0000015003901700000000006022280000000001100910000b 0000280000000091039000000006000017000110150022000a6776 行行行行( (r ro ow w) ) 列列列列( (c co ol l) ) 值值值值( (v va
22、al lu ue e) ) 行行行行( (r ro ow w) ) 列列列列( (c co ol l) ) 值值值值( (v va al lu ue e) ) 0 0 0 3 3 2 22 2 0 0 0 4 4 9 91 1 1 0 0 6 6 1 15 5 1 1 1 1 1 1 11 1 2 1 1 1 1 1 11 1 2 2 2 5 5 2 28 8 3 1 1 5 5 1 17 7 3 3 3 0 0 2 22 2 4 2 2 3 3 - - - -6 6 4 3 3 2 2 - - - -6 6 5 3 3 5 5 3 39 9 5 5 5 1 1 1 17 7 6 4 4 0
23、0 9 91 1 6 5 5 3 3 3 39 9 7 5 5 2 2 2 28 8 7 6 6 0 0 1 16 6 template sparsematrix sparsematrix: transpose ( ) sparsematrix b; b.rows = cols; b.cols = rows; b.terms = terms; /轉置矩陣的列數轉置矩陣的列數, ,行數和非零元素個數行數和非零元素個數 if ( terms 0 ) int currentb = 0; /轉置三元組表存放指針轉置三元組表存放指針 for ( int k=0; kcols; k+ ) for ( in
24、t i=0; iterms; i+ ) if ( smarrayi.col = k ) b.smarraycurrentb.row = k; b.smarraycurrentb.col = smarrayi.row; b.smarraycurrentb.value= smarrayi.value; currentb+; return b; 0123456 語語 義義rowsize 1 1 1 2 0 2 1矩陣矩陣 a 各列非各列非零元素個數零元素個數rowstart 0 1 2 3 5 5 7矩陣矩陣 b 各行開各行開始存放位置始存放位置 for ( int i=0; icols; i+ )
25、 rowsizei = 0; for ( i=0; iterms; i+ ) rowsizesmarrayi.col+; rowstart0 = 0; for ( i=1; i cols; i+ ) rowstarti = rowstarti-1+rowsizei-1;template sparsematrixsparsematrix:fasttranspos ( ) int *rowsize = new intcols; int *rowstart = new intcols; sparsematrix b; b.rows = cols; b.cols = rows; b.terms = t
26、erms; if ( terms 0 ) for (int i=0; icols; i+) rowsizei = 0; for ( i=0; iterms; i+ ) rowsizesmarrayi.col+; rowstart0 = 0; for ( i=1; i cols; i+ ) rowstarti = rowstarti-1+rowsizei-1;for ( i=0; iterms; i+ ) int j = rowstartsmarrayi.col; b.smarrayj.row = smarrayi.col; b.smarrayj.col = smarrayi.row; b.sm
27、arrayj.value = smarrayi.value; rowstartsmarrayi.col+; delete rowsize; delete rowstart; return b; const int maxlen = 128; class string int curlen; /串的當前長度串的當前長度 char *ch; /串的存儲數組串的存儲數組 public: string ( const string & ob); string ( const char *init ); string ( ); string ( ) delete ch; int length (
28、 ) const return curlen; string &operator ( ) ( int pos, int len ); int operator = ( const string &ob ) const return strcmp (ch, ob.ch) = 0; int operator != ( const string &ob ) const return strcmp (ch, ob.ch) != 0; int operator ! ( ) const return curlen = 0; string &operator = ( cons
29、t string &ob ); string &operator += ( const string &ob ); char &operator ( int i ); int find ( string pat ) const; string:string ( const string &ob ) /復制構造函數:復制構造函數:從已有串從已有串ob復制復制 ch = new charmaxlen+1; if ( !ch ) cout “allocation errorn”; exit(1); curlen = ob.curlen; strcpy ( ch
30、, ob.ch ); string:string ( const char *init ) /復制構造函數復制構造函數: 從已有字符數組從已有字符數組* *init復制復制 ch = new charmaxlen+1; if ( !ch ) cout “allocation errorn”; exit(1); curlen = strlen (init); strcpy ( ch, init ); string:string ( ) /構造函數:創建一個空串構造函數:創建一個空串 ch = new charmaxlen+1; if ( !ch ) cout “allocation errorn
31、”; exit(1); curlen = 0; ch0 = 0; 提取子串的算法示例提取子串的算法示例pos+len - -1 pos+len - -1 curlen- -1 curlen string &string: operator ( ) ( int pos, int len ) /從串中第從串中第pos個位置起連續提取個位置起連續提取len個字符個字符 /形成子串返回形成子串返回 if ( pos = maxlen | len = curlen ) len = curlen - pos; tempcurlen = len; /子串長度子串長度 for ( int i=0, j
32、=pos; ilen; i+, j+ ) tempchi = chj; /傳送串數組傳送串數組 tempchlen = 0; /子串結束子串結束 return temp; string &string:operator = ( const string &ob ) /串賦值:從已有串串賦值:從已有串ob復制復制 if ( &ob != this ) delete ch; ch = new char maxlen+1; /重新分配重新分配 if ( ! ch ) cerr “out of memory!n ”; exit (1); curlen = ob.curlen;
33、/串復制串復制 strcpy ( ch, ob.ch ); else cout “attempted assignment of a string to itself!n”; return *this; char &string:operator ( int i ) /按串名提取串中第按串名提取串中第i個字符個字符 if ( i = curlen ) cout “out of boundary!n ”; exit (1) ; return chi;string &string: /串連接串連接operator += ( const string &ob ) char *
34、 temp =ch; /暫存原串數組暫存原串數組 curlen += ob.curlen; /串長度累加串長度累加 ch = new char maxlen+1; if ( ! ch ) cerr “out of memory!n ”; exit (1) ; strcpy ( ch, temp );/拷貝原串數組拷貝原串數組 strcat ( ch, ob.ch );/連接連接ob串數組串數組 delete temp; return *this; int string:find ( string &pat ) const /窮舉的模式匹配窮舉的模式匹配 char * p = pat.c
35、h, * s = ch; int i = 0; if ( *p & *s )/當兩串未檢測完當兩串未檢測完 while ( i = curlen - pat.curlen ) if ( *p+ = *s+ ) /比較串字符比較串字符 if ( !*p ) return i; /相等相等 else i+; s = ch+i; p = pat.ch; /對應字符不相等,對齊目標的對應字符不相等,對齊目標的 /下一位置,繼續比較下一位置,繼續比較 return -1; tt0 t1 t2 tm-1 tn-1 pat p0 p1 p2 pm-1 t t0 t1 t2 tm-1 tm tn-1 pat p0 p1 pm-2 pm-1 t t0 t1 ti ti+1 ti+m-2 ti+m-1 tn-1 pat p0 p1 pm-2 pm-1 t t0 t1 ts-1 ts ts+1 ts+2 ts+j 1 ts+j ts+j+1 tn-1 p p0 p1 p2 pj 1 pj pj+1 ts ts+1 ts+2 ts+j = p0 p1 p2 pj (1) p0 p1 p2 pj-1 pm-1 = ts+1 ts+
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年家政服務職業技能競賽試題
- 智能家居系統在社區安全中的應用-洞察闡釋
- 高中美術鑒賞《沒有什么不可能》教學設計
- 重慶食品鈣生產線項目可行性研究報告(模板范文)
- 香料企業經營管理方案
- 2025至2030年中國玩具模型機床行業投資前景及策略咨詢報告
- 2025至2030年中國牛筋索行業投資前景及策略咨詢報告
- 2025至2030年中國火炎燒入鋼行業投資前景及策略咨詢報告
- 2025至2030年中國液壓管件接頭行業投資前景及策略咨詢報告
- 2025至2030年中國潔廁凈行業投資前景及策略咨詢報告
- 2024-2025學年人教版數學一年級下學期期末模擬試卷(含答案)
- 安徽省合肥一中2025屆高三最后一卷英語試題及答案
- 有關工廠實習心得體會模版
- 2025年組織行為學專業考試試題及答案
- 智能化汽車中的專利戰略布局-洞察闡釋
- 不寐的中醫護理常規
- 天津市兩學校2025屆生物七下期末達標檢測試題含解析
- 創新設計思維
- 2024年新疆維吾爾自治區、新疆生產建設兵團中考語文試卷(含答案與解析)
- 客訴處理培訓課件
- 人工智能在數據治理中的應用-洞察闡釋
評論
0/150
提交評論