diff --git a/_03.串/01串_String.c b/_03.串/01串_String.c deleted file mode 100644 index db9b232..0000000 --- a/_03.串/01串_String.c +++ /dev/null @@ -1,318 +0,0 @@ -#include "string.h" -#include "stdio.h" -#include "stdlib.h" -#include "io.h" -#include "math.h" -#include "time.h" - -#define OK 1 -#define ERROR 0 -#define TRUE 1 -#define FALSE 0 - -#define MAXSIZE 40 /* ´æ´¢¿Õ¼ä³õʼ·ÖÅäÁ¿ */ - -typedef int Status; /* StatusÊǺ¯ÊýµÄÀàÐÍ,ÆäÖµÊǺ¯Êý½á¹û״̬´úÂ룬ÈçOKµÈ */ -typedef int ElemType; /* ElemTypeÀàÐ͸ù¾Ýʵ¼ÊÇé¿ö¶ø¶¨£¬ÕâÀï¼ÙÉèΪint */ - -typedef char String[MAXSIZE+1]; /* 0ºÅµ¥Ôª´æ·Å´®µÄ³¤¶È */ - -/* Éú³ÉÒ»¸öÆäÖµµÈÓÚcharsµÄ´®T */ -Status StrAssign(String T,char *chars) -{ - int i; - if(strlen(chars)>MAXSIZE) - return ERROR; - else - { - T[0]=strlen(chars); - for(i=1;i<=T[0];i++) - T[i]=*(chars+i-1); - return OK; - } -} - -/* ÓÉ´®S¸´ÖƵô®T */ -Status StrCopy(String T,String S) -{ - int i; - for(i=0;i<=S[0];i++) - T[i]=S[i]; - return OK; -} - -/* ÈôSΪ¿Õ´®,Ôò·µ»ØTRUE,·ñÔò·µ»ØFALSE */ -Status StrEmpty(String S) -{ - if(S[0]==0) - return TRUE; - else - return FALSE; -} - -/* ³õʼÌõ¼þ: ´®SºÍT´æÔÚ */ -/* ²Ù×÷½á¹û: ÈôS>T,Ôò·µ»ØÖµ>0;ÈôS=T,Ôò·µ»ØÖµ=0;ÈôSS[0]||len<0||len>S[0]-pos+1) - return ERROR; - for(i=1;i<=len;i++) - Sub[i]=S[pos+i-1]; - Sub[0]=len; - return OK; -} - -/* ·µ»Ø×Ó´®TÔÚÖ÷´®SÖеÚpos¸ö×Ö·ûÖ®ºóµÄλÖá£Èô²»´æÔÚ,Ôòº¯Êý·µ»ØÖµÎª0¡£ */ -/* ÆäÖÐ,T·Ç¿Õ,1¡Üpos¡ÜStrLength(S)¡£ */ -int Index(String S, String T, int pos) -{ - int i = pos; /* iÓÃÓÚÖ÷´®SÖе±Ç°Î»ÖÃϱêÖµ£¬Èôpos²»Îª1£¬Ôò´ÓposλÖÿªÊ¼Æ¥Åä */ - int j = 1; /* jÓÃÓÚ×Ó´®TÖе±Ç°Î»ÖÃϱêÖµ */ - while (i <= S[0] && j <= T[0]) /* ÈôiСÓÚSµÄ³¤¶È²¢ÇÒjСÓÚTµÄ³¤¶Èʱ£¬Ñ­»·¼ÌÐø */ - { - if (S[i] == T[j]) /* Á½×ÖĸÏàµÈÔò¼ÌÐø */ - { - ++i; - ++j; - } - else /* Ö¸ÕëºóÍËÖØÐ¿ªÊ¼Æ¥Åä */ - { - i = i-j+2; /* iÍ˻ص½ÉÏ´ÎÆ¥ÅäÊ×λµÄÏÂһλ */ - j = 1; /* jÍ˻ص½×Ó´®TµÄÊ×λ */ - } - } - if (j > T[0]) - return i-T[0]; - else - return 0; -} - - -/* TΪ·Ç¿Õ´®¡£ÈôÖ÷´®SÖеÚpos¸ö×Ö·ûÖ®ºó´æÔÚÓëTÏàµÈµÄ×Ó´®£¬ */ -/* Ôò·µ»ØµÚÒ»¸öÕâÑùµÄ×Ó´®ÔÚSÖеÄλÖ㬷ñÔò·µ»Ø0 */ -int Index2(String S, String T, int pos) -{ - int n,m,i; - String sub; - if (pos > 0) - { - n = StrLength(S); /* µÃµ½Ö÷´®SµÄ³¤¶È */ - m = StrLength(T); /* µÃµ½×Ó´®TµÄ³¤¶È */ - i = pos; - while (i <= n-m+1) - { - SubString (sub, S, i, m); /* È¡Ö÷´®ÖеÚi¸öλÖó¤¶ÈÓëTÏàµÈµÄ×Ó´®¸øsub */ - if (StrCompare(sub,T) != 0) /* Èç¹ûÁ½´®²»ÏàµÈ */ - ++i; - else /* Èç¹ûÁ½´®ÏàµÈ */ - return i; /* Ôò·µ»ØiÖµ */ - } - } - return 0; /* ÈôÎÞ×Ó´®ÓëTÏàµÈ£¬·µ»Ø0 */ -} - - -/* ³õʼÌõ¼þ: ´®SºÍT´æÔÚ,1¡Üpos¡ÜStrLength(S)+1 */ -/* ²Ù×÷½á¹û: ÔÚ´®SµÄµÚpos¸ö×Ö·û֮ǰ²åÈë´®T¡£ÍêÈ«²åÈë·µ»ØTRUE,²¿·Ö²åÈë·µ»ØFALSE */ -Status StrInsert(String S,int pos,String T) -{ - int i; - if(pos<1||pos>S[0]+1) - return ERROR; - if(S[0]+T[0]<=MAXSIZE) - { /* ÍêÈ«²åÈë */ - for(i=S[0];i>=pos;i--) - S[i+T[0]]=S[i]; - for(i=pos;iS[0]-len+1||len<0) - return ERROR; - for(i=pos+len;i<=S[0];i++) - S[i-len]=S[i]; - S[0]-=len; - return OK; -} - -/* ³õʼÌõ¼þ: ´®S,TºÍV´æÔÚ,TÊǷǿմ®£¨´Ëº¯ÊýÓë´®µÄ´æ´¢½á¹¹Î޹أ© */ -/* ²Ù×÷½á¹û: ÓÃVÌæ»»Ö÷´®SÖгöÏÖµÄËùÓÐÓëTÏàµÈµÄ²»ÖصþµÄ×Ó´® */ -Status Replace(String S,String T,String V) -{ - int i=1; /* ´Ó´®SµÄµÚÒ»¸ö×Ö·ûÆð²éÕÒ´®T */ - if(StrEmpty(T)) /* TÊǿմ® */ - return ERROR; - do - { - i=Index(S,T,i); /* ½á¹ûiΪ´ÓÉÏÒ»¸öiÖ®ºóÕÒµ½µÄ×Ó´®TµÄλÖà */ - if(i) /* ´®SÖдæÔÚ´®T */ - { - StrDelete(S,i,StrLength(T)); /* ɾ³ý¸Ã´®T */ - StrInsert(S,i,V); /* ÔÚÔ­´®TµÄλÖòåÈë´®V */ - i+=StrLength(V); /* ÔÚ²åÈëµÄ´®VºóÃæ¼ÌÐø²éÕÒ´®T */ - } - }while(i); - return OK; -} - -/* Êä³ö×Ö·û´®T */ -void StrPrint(String T) -{ - int i; - for(i=1;i<=T[0];i++) - printf("%c",T[i]); - printf("\n"); -} - - -int main() -{ - - int i,j; - Status k; - char s; - String t,s1,s2; - printf("ÇëÊäÈë´®s1: "); - - k=StrAssign(s1,"abcd"); - if(!k) - { - printf("´®³¤³¬¹ýMAXSIZE(=%d)\n",MAXSIZE); - exit(0); - } - printf("´®³¤Îª%d ´®¿Õ·ñ£¿%d(1:ÊÇ 0:·ñ)\n",StrLength(s1),StrEmpty(s1)); - StrCopy(s2,s1); - printf("¿½±´s1Éú³ÉµÄ´®Îª: "); - StrPrint(s2); - printf("ÇëÊäÈë´®s2: "); - - k=StrAssign(s2,"efghijk"); - if(!k) - { - printf("´®³¤³¬¹ýMAXSIZE(%d)\n",MAXSIZE); - exit(0); - } - i=StrCompare(s1,s2); - if(i<0) - s='<'; - else if(i==0) - s='='; - else - s='>'; - printf("´®s1%c´®s2\n",s); - k=Concat(t,s1,s2); - printf("´®s1Áª½Ó´®s2µÃµ½µÄ´®tΪ: "); - StrPrint(t); - if(k==FALSE) - printf("´®tÓнضÏ\n"); - ClearString(s1); - printf("ÇåΪ¿Õ´®ºó,´®s1Ϊ: "); - StrPrint(s1); - printf("´®³¤Îª%d ´®¿Õ·ñ£¿%d(1:ÊÇ 0:·ñ)\n",StrLength(s1),StrEmpty(s1)); - printf("Çó´®tµÄ×Ó´®,ÇëÊäÈë×Ó´®µÄÆðʼλÖÃ,×Ó´®³¤¶È: "); - - i=2; - j=3; - printf("%d,%d \n",i,j); - - k=SubString(s2,t,i,j); - if(k) - { - printf("×Ó´®s2Ϊ: "); - StrPrint(s2); - } - printf("´Ó´®tµÄµÚpos¸ö×Ö·ûÆð,ɾ³ýlen¸ö×Ö·û£¬ÇëÊäÈëpos,len: "); - - i=4; - j=2; - printf("%d,%d \n",i,j); - - - StrDelete(t,i,j); - printf("ɾ³ýºóµÄ´®tΪ: "); - StrPrint(t); - i=StrLength(s2)/2; - StrInsert(s2,i,t); - printf("ÔÚ´®s2µÄµÚ%d¸ö×Ö·û֮ǰ²åÈë´®tºó,´®s2Ϊ:\n",i); - StrPrint(s2); - i=Index(s2,t,1); - printf("s2µÄµÚ%d¸ö×ÖĸÆðºÍtµÚÒ»´ÎÆ¥Åä\n",i); - SubString(t,s2,1,1); - printf("´®tΪ£º"); - StrPrint(t); - Concat(s1,t,t); - printf("´®s1Ϊ£º"); - StrPrint(s1); - Replace(s2,t,s1); - printf("Óô®s1È¡´ú´®s2Öкʹ®tÏàͬµÄ²»ÖصþµÄ´®ºó,´®s2Ϊ: "); - StrPrint(s2); - - - return 0; -} - diff --git a/_03.串/_a.串.c b/_03.串/_a.串.c index cb083ac..6943cfa 100644 --- a/_03.串/_a.串.c +++ b/_03.串/_a.串.c @@ -2,7 +2,7 @@ * @Author: Xu Bai * @Date: 2019-07-06 22:20:08 * @LastEditors: Xu Bai - * @LastEditTime: 2019-07-07 23:58:05 + * @LastEditTime: 2019-07-08 00:10:40 */ #include "string.h" @@ -309,10 +309,34 @@ int main() printf("ERROR!\n"); exit(0); } - StrPrint("s1=%s \n",s1); - StrCopy(s2,s1); - printf("copyed s2=%s \n",s2); - + printf("s1= "); + StrPrint(s1); + StrCopy(s2, s1); + printf("s2= "); + StrPrint(s2); + StrAssign(s2, "-------"); + printf("then, s2= "); + StrPrint(s2); + String t; + k = Concat(t, s1, s2); + if (k) + { + printf("concat completed: "); + StrPrint(t); + } + else + { + printf("cutted off:"); + StrPrint(t); + } + int i = 2, j = 3; + k = SubString(s2, t, i, j); + printf("Substring: "); + StrPrint(s2); + StrDelete(t, 1, 2); + StrInsert(t, 1, s2); + Replace(s2, t, s1); + StrPrint(s2); getchar(); return OK; } \ No newline at end of file diff --git a/a.out b/a.out index 86c558f..8e41c6f 100644 Binary files a/a.out and b/a.out differ