python实现顺序串

MaxSize = 100
class SqString():
    def __init__(self,size=0):
        self.data = [None]*MaxSize
        self.size = 0
    def StrAssign(self,str):                  #  创建串
        for i,item in enumerate(str):
            self.data[i] = item
            self.size += 1
    def DispStr(self):                      # 输出串
        if self.size == 0:
            raise ValueError("str is empty")
        for i, item in enumerate(self.data):
            if item is None:
                break
            else:
                print(item)
    def getsize(self,str):           # 输出串s的长度
        print(self.size)
    def Insstr(self,i,s1):          #在串s的第9个字符位置插入串s1而产生串s2
        if i <= 0 or i > self.size:
            raise ValueError("index out of range")
        s2 = SqString(self.size + s1.size)
        for k,item in enumerate(self.data[0:i]):
            s2.data[k] = item
            s2.size += 1
        for k,item in enumerate(s1.data):
            if item is not None:
                s2.data[i+k] = item
                s2.size += 1
            else:
                break
        for k,item in enumerate(self.data[i:]):
            if item is not None:
                s2.data[i+s1.size+k] = item
                s2.size += 1
            else:
                break
        return s2


    def Delstr(self,i,j):          #删除串s的第2个字符开始的5个字符而产生串s2
        if i <= 0 or i > self.size or i+j-1 > self.size:
            raise ValueError("index out of range")
        s2 = SqString()
        for k,item in enumerate(self.data[0:i]):
            s2.data[k] = item
            s2.size += 1
        for k,item in enumerate(self.data[i+j:]):
            if item is not None:
                s2.data[i+k] = item
                s2.size += 1
            else:
                break
        return s2
    def Repstr(self,i,j,t):                        #将串s的第2个字符开始的5个字符替换成串s1而产生串s2
        if i <=0  or i > self.size or i+j-1 > self.size:
            raise ValueError("Index out of range")
        s2 = SqString(self.size-j + t.size)
        for k,item in enumerate(self.data[0:i]):
            s2.data[k] = item
            s2.size += 1
        for k,item in enumerate(t.data):
            if item is not None:
                s2.data[i-1+k] = item
                s2.size += 1
        for k,item in enumerate(self.data[i+j-1:]):
            if item is not None:
                s2.data[i-1+t.size+k] = item
                s2.size += 1
        return s2
    def getstr(self,i,j):          #提取串s的第2个字符开始的10个字符而产生串s3
        if i <= 0 or i > self.size or i+j > self.size:
            raise ValueError("index out of range")
        s3 = SqString(self.size)
        for k,item in enumerate(self.data[i:i+j]):
            if item is not None:
                s3.data[k] = item
                s3.size += 1
            else:
                break
        return s3
    def Concat(self,s2):
        s4 = SqString(self.size+s2.size)
        for k,item in enumerate(self.data):
            if item is not None:
                s4.data[k] = item
                s4.size += 1
            else:
                break
        for i,item in enumerate(s2.data):
            if item is not None:
                s4.data[k+i] = item
                s4.size += 1
            else:
                break
        return s4



if __name__=='__main__':
   s_1 = "abcdefghefghijklmn"
   s1 = SqString()
   s1.StrAssign(s_1)
   s_2 = "xyz"
   s2 = SqString()
   s2.StrAssign(s_2)
   a = s1.Concat(s2)
   a.DispStr()

你可能感兴趣的:(python)