順序串的實現

2021-10-07 01:47:46 字數 3086 閱讀 1727

package com.lovely.string;

/** *

* @author echo lovely

* 2023年6月9日下午6:44:31

* * 串的介面描述

*/public

inte***ce

istring

package com.lovely.string;

/** * @author echo lovely

* 2023年6月9日下午6:55:32

* 順序串的實現

*/public

class

seqstring

implements

istring

// 以字串常量構造串 本質是字串 -> 字元陣列

public

seqstring

(string str)

// 以字元陣列構造串

public

seqstring

(char

ch)

curlen = ch.length;

}public

void

clear()

public

boolean

isempty()

public

intlength()

public

char

charat

(int i)

throws exception

// 將串的長度擴充為newcapacity

public

void

allocate

(int newcapacity)

char

tmp = strvalue;

strvalue =

newchar

[newcapacity]

;// 剩下長度newcapacity - tmp.length

for(

int i =

0; i < tmp.length; i++)}

public istring substring

(int begin,

int end)

return

newseqstring

(tmp)

;// 利用字元陣列構造得到新的串

}public

void

insert

(int i, istring str)

throws exception

for(

int j = i; j < i + len; j ++

) strvalue[j]

= str.

charat

(j - i)

;// 插入

curlen = newcapacity;

}@override

public

void

delete

(int begin,

int end)

/*don't konw...*/

}@override

public istring concat

(istring str)

catch

(exception e)

return

newseqstring

(strvalue);}

public

intcompareto

(istring str)

catch

(exception e)

}return0;

}public

intindexof

(istring str,

int fromindex)

throws exception

else

if(j == len -1)

}}}return-1

;}public

void

display()

}

package com.lovely.string;

/** *

* @author echo lovely

* 2023年6月10日下午3:37:30

* * 串的測試demo

*/public

class

testseqstring

;// 插入串1下標

ss.insert(1

,new

seqstring

(ch));

// 插入串的尾部

ss.insert

(ss.

length()

,new

seqstring

("ghi"))

;}catch

(exception e)

// 連線

ss.concat

(new

seqstring

("連線到串的尾部"))

; ss.

display()

;// 比較

int i = ss.

compareto

(new

seqstring

("ghi"))

; system.out.

println

(i >0?

"前面的大"

:"後面的大");

// 擷取

istring sub = ss.

substring(0

,3);

system.out.

println

("前三個串: ");

((seqstring)sub)

.display()

;try

catch

(exception e)

}}

adefbcghi連線到串的尾部

前面的大

前三個串:

adedef的下標 1

最後乙個字串 部

順序串(陣列實現)

include using namespace std define max 100 typedef struct sstring void in sstring sa 初始化 cout ddw sstring totwo sstring sb,sstring sc 合併兩個串,返回合成後的串 el...

練習 順序串的實現及應用

define maxlength 60 typedef struct seqstring void strassign seqstring s,char cstr 串的賦值操作 int strempty seqstring s 判斷串是否為空,串為空返回1,否則返回0 int strlength s...

實現順序串各種基本運算的演算法

實驗題目 實現順序串各種基本運算的演算法 實驗內容 編寫程式,實現順序串的各種基本運算,並在此基礎上設計乙個主程式完成如下功能 1 建立串s abcdefghefghijklmn 和串s1 xyz 2 輸出串s 3 輸出串s的長度 4 在串s的第9個字元位置插入串s1而產生串s2 5 輸出串s2 6...