《数据结构》串基本操作与实现

#include"stdio.h"
#include"stdlib.h"
#include"String.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define Maxstrlen 225

typedef unsigned char SString[Maxstrlen+1];
typedef int Status;

/*typedef struct Node
{
	char ch;
	int count;
	struct Node * next;
}LNode,* LinkList;*/

//生成一个值为chars的串T
Status StrAssign(SString T,char chars[])
{
	int i=1;
	if(strlen(chars)>Maxstrlen)return ERROR;
	T[0]=strlen(chars);//串的长度
	for(i=1;i<=T[0];i++)
	{	
			T[i]=chars[i-1];
	}
	return TRUE;
}

//由串S复制得到串T
Status StrCopy(SString T,SString S)
{
	int i=1;
	T[0]=S[0];
	printf("T string after copy is :");
	for(i=1;i<=T[0];i++)
	{
		T[i]=S[i];
		printf("%c",T[i]);
	}
	printf("\n");
	return OK;
}

//判空
Status StrEmpty(SString S)
{
	return S[0]==0?TRUE:FALSE;
}

//比较串长,返回长度差
int StrCompare(SString S,SString T)
{
	int i=1;
	for(i=1;i<=S[0]&&i<=T[0];i++)
	{
		if(S[i]!=T[i])return S[i]-T[i];
	}
	return S[0]-T[0];//strlen(S)>strlen(T)时,如果前面完全一样返回长度差,不用在意返回的含义
}

//串长度
int StrLength(SString S)
{
	return S[0];
}

//清空
Status ClearString(SString S)
{
	S[0]=0;
	return OK;
}

//销毁
Status DestroyString(SString S)
{
	S=NULL;
	free(S);
	return OK;
}

Status Concat(SString T,SString S1,SString S2)
/*用T返回由串S1和串S2连接成的新串。若未截断,则返回true,否则返回false*/
{
	int i=1;
	if(S1[0]+S2[0]<=Maxstrlen)
	{
		T[0]=S1[0]+S2[0];
		for(i=1;i<=S1[0];i++)
		{
			T[i]=S1[i];
		}
		for(i=1;i<=S2[0];i++)
		{
			T[i+S1[0]]=S2[i];
		}
		return OK;
	}
	else if(S1[0]<Maxstrlen)//截断;只取S2的一部分,返回false
	{
		T[0]=Maxstrlen;
		for(i=1;i<=S1[0];i++)
		{
			T[i]=S1[i];
		}
		for(i=1;i<=Maxstrlen-S1[0];i++)
		{
			T[i+S1[0]]=S2[i];
		}
		return FALSE;
	}
	else
	{
		T[0]=Maxstrlen;
		for(i=1;i<=T[0];i++)
		{
			T[i]=S1[i];
		}
		return FALSE;
	}
}

//在串S的第pos个位置插入串T
Status StrInsert(SString S,SString T,int pos)
{
	int i=1;
	if(pos<1||pos>S[0]+1)return ERROR;

	if(S[0]+T[0]<=Maxstrlen)
	{/*
		for(i=S[0];i>=pos;i--)
		{
			S[i+T[0]]=S[i];
		}*/
		for(i=pos;i<pos+T[0];i++)
		{
			S[i]=T[i-pos+1];
		}
		S[0]=S[0]+T[0];
		return OK;
	}
	else
	{
		for(i=Maxstrlen;i>=pos;i--)
		{
			S[i]=S[i-S[0]];
		}
		for(i=pos;i<pos+Maxstrlen-S[0];i++)
		{
			S[i]=T[i-pos+1];
		}
		S[0]=Maxstrlen;
		return OK;
	}
}

//在串S中从第pos个位置开始删除长度为len的元素
Status StrDelete(SString S,int pos,int len)
{
	int i=1;
	if(len<0||pos<1||pos>S[0]-len+1)return ERROR;
	for(i=pos+len;i<=S[0];i++)
	{
		S[i-len]=S[i];
	}
	S[0]-=len;
	return OK;
}

//返回串s中长度为len的子串
Status SubString(SString Sub,SString S,int pos,int len)
{
	int i=1;
	if(pos<1||pos>S[0]||len<0||len>S[0]-pos+1)return ERROR;
	for(i=pos;i<=pos+len;i++)
	{
		Sub[i-pos+1]=S[i];
	}
	Sub[0]=len;
	return OK;
}

//若串S中存在相同子串,则返回在主串中第pos个字符后第一次出现的位置
int Index(SString S,SString T,int pos)
{
	int i=pos,j=1;
	while(i<=S[0]&&j<=T[0])
	{
		if(S[i]==T[j])
		{
			i++;
			j++;
		}
		else
		{
			i=i-j+1+1;
			j=1;
		}
	}
	if(j>T[0])return i-T[0];
	else
		return 0;
}

//遍历
void PrintString(SString S)
{
	int i=1;
	printf("string is :");
	for(i=1;i<=S[0];i++)
	{
		printf("%c",S[i]);
	}
	printf("\n");
}


void main()
{
	//LinkList L;
	SString S,T,S1,S2,Sub;
	char chars[Maxstrlen];
	printf("input S string:\n");
	gets(chars);
	StrAssign(S,chars);

	int pos,len;
	int choice=1;
	while(choice!=0)
	{
		printf("0.exit 1.StrAssign 2.StrCopy 3.StrEmpty 4.StrCompare 5.StrLength 6.ClearString 7.DestroyString \n");
		printf("8.Concat 9.StrInsert 10.StrDelete 11.SubString 12.Index 13.PrintString\n");
		printf("input your choice : ");
		scanf("%d",&choice);
		switch(choice)
		{
		case 0:printf("BEY BEY !");break;
		case 1:
			{				
				printf("input assign chars: ");
				getchar();
				gets(chars);
				if(StrAssign(T,chars)==OK)printf("strassign T succeess.\n");
				else
				{
					printf("strassign T not success.\n");
				}
			}break;
		case 2:
			{
				if(StrCopy(T,S)==OK)printf("T copy S success.\n");
				else
					printf("T copy S not success.\n");
			}break;
		case 3:
			{
				if(StrEmpty(S)==TRUE)printf("S string is empty.\n");
				else
					printf("S string is not empty.\n");
			}break;
		case 4:
			{
				int value=0;
				value=StrCompare(S,T);
				if(value==0)printf("string S==srtring T\n");
				else if(value>0)printf("string S>srtring T\n");
				else
					printf("string S<srtring T\n");
			}break;
		case 5:printf("S string's length is %d",StrLength(S));break;
		case 6:
			{
				if(ClearString(S)==OK)printf("S string clear sucess.\n");
				else
					printf("S string clear not success.\n");
			}break;
		case 7:
			{
				if(DestroyString(S)==OK)printf("S string destroy sucess.\n");
				else
					printf("S string destroy not success.\n");
			}break;
		case 8:
			{
				printf("input S1 string:\n");
				getchar();
				gets(chars);
				StrAssign(S1,chars);
				printf("input S2 string:\n");
				gets(chars);
				StrAssign(S2,chars);
				if(Concat(T,S1,S2)==TRUE)
				{
					printf("T 完全连接 S1 and S2.\n");
					PrintString(T);
				}
				else
				{
					printf("T连接了部分 S1 and S2 \n");
					PrintString(T);
				}
			}break;
		case 9:
			{
				printf("input insert location: ");
				scanf("%d",&pos);
				if(StrInsert(S,T,pos)==OK)printf("T string inser sucess.\n");
				else
					printf("T String insert not sucess.\n");
			}break;
		case 10:
			{
				printf("input delete location and len: ");
				scanf("%d %d",&pos,&len);
				if(StrDelete(S,pos,len)==OK)printf("delete sucess.\n");
				else
					printf("delete not sucess.\n");
			}break;
		case 11:
			{
				printf("input sub location and len: ");
				scanf("%d %d",&pos,&len);
				if(SubString(Sub,S,pos,len)==OK)
				{
					printf("substring is : ");
					for(int i=1;i<=Sub[0];i++)
					{
						printf("%c",Sub[i]);
					}
					printf("\n");
				}
				else
					printf("substring not sucess.\n");
			}break;
		case 12:
			{
				printf("input start valued pos:");
				scanf("%d",&pos);
				int first=0;
				first=Index(S,T,pos);
				printf("T string from S string's %d start first apprea is %d ",pos,first);
			}break;
		case 13:PrintString(S);break;
		}
		printf("\n");
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值