ADD DS_3_x_string

This commit is contained in:
Kim Yang
2020-08-08 00:06:38 +08:00
parent 591430b42e
commit fce29a01d9
5 changed files with 95 additions and 30 deletions

View File

@@ -2,6 +2,63 @@
// Created by kim yang on 2020/8/3.
//
//顺序存储——静态数组实现方式定长顺序存储注意下面实现在数组中存放字符串时都会舍弃Str[0],第一个结点的空间,以保证字符下标和数组下标保证一致
#include <stdio.h>
#define MAXLEN 255
#define MAXLEN 255 //预定义最大串长为255
typedef struct {
char ch[MAXLEN];//每个分量存储一个字符
int length; //串的实际长度
}SString;
bool InitStr(){
}
//求子串
bool SubString(SString &Sub,SString S,int pos,int len){
if (pos+len-1>S.length)return false;
for (int i = pos; i < pos+len; ++i)
Sub.ch[i-pos+1]=S.ch[i];
Sub.length=len;
return true;
}
//比较操作若S>T,则返回值>0;若S=T,则返回值=0若S<T则返回值<0;
int StrCompare(SString S,SString T){
for (int i = 1; i <=S.length&i<=T.length; i++) {
if (S.ch[i]!=T.ch[i])
return S.ch[i]-T.ch[i];
}
//扫描过的所有字符都相同,则长度长的串更大
return S.length-T.length;
}
//获取字符串长度
int StrLength(SString S){
return S.length;
}
//定位操作,若主串S中存在与串T值相同的子串则返回它在主串S中第一次出现的位置否则函数值为0
int Index(SString S,SString T){
int i=1,n=StrLength(S),m=StrLength(T);
SString sub;
while(i<=n-m+1){
SubString(sub,S,i,m);
if (StrCompare(sub,T)!=0)++i;
else return i;//返回子串在主串中的位置
}
return 0;//S中不存在与T相等的子串
}
void TestStr(){
printf("开始测试!\n");
printf("测试结束!\n");
}
int main(){
TestStr();
return 0;
}