고정 직렬 조작
11558 단어 데이터 구조정장 꼬치데이터 구조 와 알고리즘
: , :“abcde”,“PI”
: , :“abcde” 5,“PI” 2
: , :“abcde” “abcde”
: , , , :“ ”
: , :“”
: , :“abcde” “a”,“bc”,“bcd”
헤더 파일:
/*****************************************************************************************************
*Copyright:Yue Workstation
*
*FileName:FixedLengthString.h
*
*Function:
*
*Author:Abel Lee
*
*CreateOn:2011-8-11
*
*Log:2011-8-11
*****************************************************************************************************/
#ifndef FIXED_LENGTH_STRING_H
#define FIXED_LENGTH_STRING_H
#include
#include
#include
#include "global.h"
typedef unsigned char SString[MAX_LENGTH + 1];//SString[0]
SString *StringCopy(SString *S, SString T);
SString *StringClear(SString *S);
SString *StringCat(SString *S, const SString T);
SString *StringReplace(SString *S, SString T, SString V);
int StringInstert(SString *S, SString T, int pos);
int StringInit(SString *S, const char *str);
int StringCompare(SString S,SString T);
int StringIndex(SString S, SString T, int pos);
int StringLength(SString S);
int StringDestroy(SString *S);
void StringPrint(SString S);
int StringSub(SString *S, SString T, int pos, int len);
#endif
원본 파일:
/*****************************************************************************************************
*Copyright:Yue Workstation
*
*FileName:FixedLengthString.c
*
*Function:
*
*Author:Abel Lee
*
*CreateOn:2011-8-11
*
*Log:2011-8-11
*****************************************************************************************************/
#include "../inc/FixedLengthString.h"
/****************************************************************************************************
*Function Name:StringCopy
*
*Function:
*
*Parameter: S:
* str:
*
*Return Value: 0, -1
*
*Author:Abel Lee
*
*Log:2011-9-15
***************************************************************************************************/
SString *StringCopy(SString *S, SString T)
{
int i = 0;
if(*S == NULL || T == NULL)
{
return NULL;
}
while(T[i])
{
(*S)[i] = T[i];
i++;
}
(*S)[i] = T[i];
return S;
}
/****************************************************************************************************
*Function Name: StringCompare
*
*Function: S T
*
*Parameter: S:
* T:
*
*Return Value: 0,S > T 1,S < T -1
*
*Author:Abel Lee
*
*Log:2011-9-16
***************************************************************************************************/
int StringCompare(SString S,SString T)
{
int i = 0;
int flag = 0;
if(S[0] - T[0] >= 0)
{
flag = S[0];
}
else
{
flag = T[0];
}
for(i = 1; i < flag; i++)
{
if(S[i] - T[i] > 0)
{
return 1;
}
else if(S[i] - T[i] < 0)
{
return -1;
}
else
{
}
}
return 0;
}
/****************************************************************************************************
*Function Name: StringClear
*
*Function:
*
*Parameter: S:
*
*Return Value: S, NULL
*
*Author:Abel Lee
*
*Log:2011-9-16
***************************************************************************************************/
SString *StringClear(SString *S)
{
int i = 0;
if(S == NULL)
{
return NULL;
}
while((*S)[i])
{
(*S)[i] = '\0';
i++;
}
return S;
}
/****************************************************************************************************
*Function Name: StringCat
*
*Function: , T S
*
*Parameter: S:
* T:
*
*Return Value: S, NULL
*
*Author:Abel Lee
*
*Log:2011-9-16
***************************************************************************************************/
SString *StringCat(SString *S, const SString T)
{
int i = 1;
int j = 1;
if(S == NULL || T == NULL)
{
return NULL;
}
if((*S)[0] + T[0] > MAX_LENGTH)
{
return NULL;
}
while((*S)[i])
{
i++;
}
while(T[j])
{
(*S)[i] = T[j];
i++;
j++;
}
(*S)[i] = '\0';
(*S)[0] += T[0];
return S;
}
/****************************************************************************************************
*Function Name: StringSub
*
*Function: T pos len S
*
*Parameter: S:
* T:
* pos:
* len:
*
*Return Value: 0, -1
*
*Author:Abel Lee
*
*Log:2011-9-20
***************************************************************************************************/
int StringSub(SString *S, SString T, int pos, int len)
{
int i = 1;
if(pos < 1 || pos > T[0] || len < 0 || len > T[0] - pos + 1)
{
perror("Input Parameter is error! pos < 1 || pos > T[0] || len < 0 || len > T[0] - pos + 1
");
return -1;
}
(*S)[0] = len;
while(len--)
{
(*S)[i++] = T[pos++];
}
(*S)[i] = '\0';
return 0;
}
/****************************************************************************************************
*Function Name: StringReplace
*
*Function: V S T
*
*Parameter: S:
* T: S
* V: S T
*
*Return Value: , -1
*
*Author:Abel Lee
*
*Log:2011-9-16
***************************************************************************************************/
SString *StringReplace(SString *S, SString T, SString V)
{
int pos = 1;
int len = T[0];
int i = 0;
SString STemp;
SString StrLeft;
if(len <= 0 || (*S)[0] < T[0])
{
perror("Input Parameter is error! len <= 0 || (*S)[0] < T[0]
");
return NULL;
}
while((*S)[0] - pos +1 >= len)
{
StringSub(&STemp,*S,pos,len);
if(StringCompare(STemp,T) == 0)
{
i = 0;
while((*S)[i+pos+len-1])
{
i++;
StrLeft[i] = (*S)[i+pos+len-1];
}
StrLeft[i] = '\0';
StrLeft[0] = i - 1;
(*S)[pos] = '\0';
(*S)[0] = pos -1;
StringCat(S,V);
StringCat(S,StrLeft);
pos = pos + V[0];
continue;
}
pos++;
}
return S;
}
/****************************************************************************************************
*Function Name: StringInstert
*
*Function: S pos T
*
*Parameter: S:
* T:
* pos:
*
*Return Value: 0, -1
*
*Author:Abel Lee
*
*Log:2011-9-21
***************************************************************************************************/
int StringInstert(SString *S, SString T, int pos)
{
SString StrLeft;
int i = 1;
if((*S)[0] + T[0] > MAX_LENGTH || pos < 1 || pos > (*S)[0] + 1)
{
perror("Input parameter is error,(*S)[0] + T[0] > MAX_LENGTH || pos < 1 || pos > (*S)[0] + 1
");
return -1;
}
while((*S)[i+pos-1])
{
StrLeft[i] = (*S)[i+pos-1];
i++;
}
StrLeft[i] = '\0';
StrLeft[0] = i - 1;
(*S)[pos] = '\0';
(*S)[0] = pos -1;
StringCat(S,T);
StringCat(S,StrLeft);
return 0;
}
/****************************************************************************************************
*Function Name: StringIndex
*
*Function: , S pos T, S
*
*Parameter: S:
* T:
* pos:
*
*Return Value: , 0, -1
*
*Author:Abel Lee
*
*Log:2011-9-16
***************************************************************************************************/
int StringIndex(SString S, SString T, int pos)
{
int len = T[0];
SString STemp;
if(pos < 1 || S[0] < T[0] || pos > S[0])
{
perror("Input Parameter is error! pos < 1 || S[0] < T[0] || pos > S[0]
");
return -1;
}
while(S[0] - pos +1 >= len)
{
StringSub(&STemp,S,pos,len);
if(StringCompare(STemp,T) == 0)
{
return pos;
}
pos++;
}
return 0;
}
/****************************************************************************************************
*Function Name:StringInit
*
*Function:
*
*Parameter: S:
* str:
*
*Return Value: 0, -1
*
*Author:Abel Lee
*
*Log:2011-9-15
***************************************************************************************************/
int StringInit(SString *S, const char *str)
{
int i = 0;
const char *p = str;
if(strlen(str) > MAX_LENGTH)
{
perror("The string length is very large!
");
return -1;
}
while(*p != '\0')
{
i++;
(*S)[i] = *p;
p++;
}
(*S)[0] = i;
(*S)[++i] = '\0';
return 0;
}
/****************************************************************************************************
*Function Name:StringInit
*
*Function:
*
*Parameter: S:
*
*Return Value: 0, -1
*
*Author:Abel Lee
*
*Log:2011-9-15
***************************************************************************************************/
void StringPrint(SString S)
{
printf("%s
",(char *)(S+1));
return;
}
/****************************************************************************************************
*Function Name: StringLength
*
*Function:
*
*Parameter: S:
*
*Return Value:
*
*Author:Abel Lee
*
*Log:2011-9-16
***************************************************************************************************/
int StringLength(SString S)
{
return S[0];
}
/****************************************************************************************************
*Function Name: StringDestroy
*
*Function:
*
*Parameter: S:
*
*Return Value: 0, -1
*
*Author:Abel Lee
*
*Log:2011-9-16
***************************************************************************************************/
int StringDestroy(SString *S)
{
int i = 0;
if(S == NULL)
{
return -1;
}
while((*S)[i])
{
(*S)[i] = '\0';
i++;
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.