데이터 구조 - C 언어 구현 문자열

3417 단어 데이터 구조
#include "stdlib.h"
#include "stdio.h"
#define true 1
#define false 0
#define MAXLEN 255
typedef int Status;
typedef struct{
	char ch[MAXLEN];
	int length;
}String;
//    
void StrAssign(String *S,char *string)
{
	//      string  S,string        
	char *str;
	int len,i;
	str=string;
	for(len=0;str!='\0';str++);
	len++;
	if(len==0)
	{
		S->ch[0]='\0';
		S->length=0;
	}
	else
	{
		for(i=0;ich[i]=string[i];
		S->length=len;
	}
}
//       
Status StrEmpty(String S)
{
	//        ,   ,  true,    false
	if(S.length==0)
		return true;
	else
		return false;
}
//   
Status StrCompare(String S,String T)
{
	//     S T, S>T,  1, S=T,  0, ST.ch[i])
				return 1;
			else
				return -1;
		}
	}
	return 0;
}
//   
void StrCopy(String *T,String S)
{
	//  S      T
	int i;
	for(i=0;ich[i]=S.ch[i];
	}
	T->length=S.length;
}
//   
int StrLength(String S)
{
	//   S   
	return S.length;
}
//   
void StrClear(String *S)
{
	//  S   
	S->length=0;
}
//   
/*
1. S1.length+S2.length<=MAXLEN ,  S2    S1   
2. S1.length+S2.length>MAXLEN , S1.lengthch[i]=S1.ch[i];
		for(i=0;ich[S1.length+i]=S2.ch[i];
		T->length=S1.length+S2.length;
		return true;
	}
	else if(S1.lengthch[i]=S1.ch[i];
		for(i=S1.length;ich[i]=S2.ch[i-S1.length];
		T->length=MAXLEN;
		return false;
	}
	else
	{
		for(i=0;ich[i]=S1.ch[i];
		}
		return false;
	}
}
//   
Status SubString(String *Sub,String S,int pos,int len)
{
	// Sub   S  pos       len   
	int i;
	if(pos<0||pos>=S.length||len<1||len>S.length)
	{
		Sub->length=0;
		return false;
	}
	for(i=0;ich[i]=S.ch[pos+i];
	Sub->length=len;
	return true;
}
//   
Status StrIndex(String S,String T,int pos)
{
	//  T   S  pos             
	int i,j;
	if(S.length==0||T.length==0||pos<0)
		return false;
	i=pos;
	j=0;
	while(i=T.length)
	{
		return i-j+1;
	} 
	else
	{
		return 0;
	}
}
//   
/*
1. S.length+T.length<=MAXLEN ,  S2  T.length   ,  T  
2. S.length+T.length>MAXLEN, S1.length+T.lengthMAXLEN ,  S2    (     ),
  T     MAXLEN      
*/
Status StrInsert(String *S,int pos,String T)
{
	//  S  pos        T
	int i;
	if(pos<0||pos>S->length||S->length==0)
		return false;
	if(S->length+T.length<=MAXLEN)
	{
		for(i=S->length-1+T.length;i>=pos+T.length;i--)
			S->ch[i]=S->ch[i-T.length];// S       
		for(i=0;ich[pos+i]=T.ch[i];
		S->length=S->length+T.length;
	}
	else if (pos+T.length<=MAXLEN)
	{//    S   MAXLEN,  T      
	//   S             
		for(i=MAXLEN-1;i>pos-1+T.length;i--)
			S->ch[i]=S->ch[i-T.length];
		for(i=0;ich[pos+i]=T.ch[i];
		S->length=MAXLEN;
	}
	else// T     
	{
		for(i=0;ich[pos+i]=T.ch[i];
		S->length=MAXLEN;
	}
	return true;
}
//   
Status StrDelete(String *S,int pos,int len)
{
	//  S    pos       len   
	int i;
	if(pos<0||pos>S->length-len)
		return false;
	for(i=pos+len;ilength;i++)
		S->ch[i-len]=S->ch[i];
	S->length=S->length-len;
	return true;
}

좋은 웹페이지 즐겨찾기