zoukankan      html  css  js  c++  java
  • 顺序串的基本操作

    #include<stdio.h>
    #define MaxSize 255
    typedef struct
    {
    	char ch[MaxSize];
    	int length;
    }SString;
    void InitStr(SString &S)	
    {
    	S.ch[0]='¥';
    	S.length=0;
    }
    void StrCreate(SString &S,char a[])		//1.赋值操作
    {
    	int i=0,j=1;
    	while(a[i]!='')
    	{
    		S.ch[j++]=a[i++];
    	}
    	S.length=i;
    }
    void StrCopy(SString &S,SString T)	//2.复制操作
    {
    	for(int i=1;i<T.length;i++)
    		S.ch[i]=T.ch[i];
    	S.length=T.length;
    }
    bool StrEmpty(SString S)	//3.判空操作
    {
    	return S.length==0;
    }
    int StrLen(SString S)	//4.求串长
    {
    	return S.length;
    }
    void ClearStr(SString &S)	//5.清空操作
    {
    	S.length=0;
    }
    void Concat(SString &T,SString S1,SString S2)	//6.串链接
    {
    	int i;
    	for(i=1;i<=S1.length;i++)	//将串S1复制到T
    		T.ch[i]=S1.ch[i];
    	for(i=1;i<=S2.length;i++)	//将S2复制到T尾部
    		T.ch[S1.length+i]=S2.ch[i];
    	T.length=S1.length+S2.length;
    }
    bool SubString(SString S,SString &Sub,int front,int len)	//7.求子串
    {
    	if(S.length<front+len-1)	//判断子串是否越界
    		return false;
    	for(int i=1;i<=len;i++)
    		Sub.ch[i]=S.ch[front++];
    	Sub.length=len;
    	return true;
    }
    int CompareStr(SString S,SString T)	//8.比较操作
    {
    	int i=1;
    	while(i<S.length && i<T.length && S.ch[i]==T.ch[i])
    		i++;
    	if(S.ch[i]!=T.ch[i])	
    		return S.ch[i]-T.ch[i];	//S>T 返回值>0;S<T 返回值<0;S=T 返回值=0
    	return S.length-T.length;	//扫描过的字符一致,长串大
    }
    int Index(SString S,SString T)	//9.定位操作
    {
    	int i=1,m=S.length,n=T.length;
    	SString Sub;
    	while(i<m-n+1)		//长度为T.length的子串个数
    	{
    		SubString(S,Sub,i,T.length);	//依次求子串
    		if(CompareStr(Sub,T)==0)	//判断两串是否相等
    			return i;	//返回位置i
    		i++;
    	}
    	return 0;
    }
    void PrintStr(SString S)	//10.输出操作
    {
    	printf("the Strings are:");
    	for(int i=1;i<=S.length;i++)
    		printf("%c	",S.ch[i]);
    	printf("
    ");
    }
    int main()
    {
    	SString S;
    	InitStr(S);
    	char a[]="abcdefg";
    	printf("The Strings is %s
    ",StrEmpty(S)?"空":"非空");
    	StrCreate(S,a);
    	printf("The Strings is %s
    ",StrEmpty(S)?"空":"非空");
    	PrintStr(S);
    	printf("The S's length is %d
    ",S.length);
    	SString Sub;
    	SubString(S,Sub,2,3);
    	PrintStr(Sub);
    	SString T;
    	Concat(T,S,Sub);
    	PrintStr(T);
    	printf("The T's length is %d
    ",T.length);
    	int i=CompareStr(S,T);
    	if(i==1)
    		printf("The max is %c
    ",'S');
    	else if(i==0)
    		printf("The max is S and T
    ");
    	else 
    		printf("The max is %c
    ",'T');
    	printf("The Sub is Num %d in S
    ",Index(S,Sub));
    	char b[]="acdbg";
    	SString R;
    	StrCreate(R,b);
    	printf("The Sub is Num %d in S
    ",Index(S,R));
    	ClearStr(T);
    	printf("The Strings is %s
    ",StrEmpty(T)?"空":"非空");
    	return 0;
    }
    

      

  • 相关阅读:
    loj6145. 「2017 山东三轮集训 Day7」Easy
    CF1019E Raining season
    CF1261F Xor-Set
    Python笔试——递归算法学习
    Python笔试——贪心算法
    Python笔试——万万没想到抓捕孔连顺
    Python笔试——雀魂启动
    Python学习——正则表达式
    Python笔试——毕业旅行问题
    Python笔试——Stern-Brocot tree
  • 原文地址:https://www.cnblogs.com/-slz-2/p/13289924.html
Copyright © 2011-2022 走看看