20160222.CCPP体系详解(0032天)

程序片段(01):宽字符.c+字符串与内存四区.c
内容概要:宽窄字符

///宽字符.c
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

//01.宽字符的应用:
//  1.宽字符用于国际化:
//      Unicode编码情况之下,需要启用宽字符编程
//  2.中文的高级处理:
//      必须依赖于宽字符
//  3.宽窄字符的唯一不同特点:
//      存储数据的盒子尺寸不一致(宽字符采用双字节存储,窄字符采用单字节存储)
//      注:其它特性和窄字符一致
//  4.宽字符情况之下:
//      所有单个宽字符都占据两个字节的内存空间
//  5.设置本地化的目的:
//      启用本地字符集,以实现基于本地化的国际化
//  注:
//      1.L'X'-'X'和L"ABC"-"ABC"两种存储方式存储的数据实质都是一样的,不一样的
//          只是存储该数据的盒子尺寸不一样
//      2.宽窄字符的使用注意事项:
//          宽字符:
//              如果没有设置本地化的情况之下,也就只能处理ASCII码表包含字符
//              只有在设置了正确的本地化的情况之下,才能正确的处理本地语言   
//          窄字符:
//              无论是否设置本地化,都只能处理ASCII码表当中所定义的字符
//              也就是说只有宽字符才存在设置本地化的不同点
//      3.宽窄字符的结尾标识符:
//          宽字符:L'\0'
//          窄字符:'\0
//          注:所有位于代码区常量池的字符串默认携带结尾标识符
//02.采用窄字符存储中文的特点分析:
//  1.所有ASCII码表所包含的字符都只占用一个字节
//  2.所有中文字符将会包含两个字节
//  注:窄字符存储原理
int main01(void)
{
    char * p1 = "我";
    char * p2 = "我的";//所有位于代码区常量池的字符串结尾默认自带'\0'orL'\0'
    printf("%d, %d \n", sizeof("我"), sizeof("我的"));
    printf("%s \n", p1);//窄字符情况下:分ASCII码表字符和非ASCII码表字符

    system("pause");
}

//03.非ASCII码表的其他字符如果按照字符数组进行存储:
//  就需要按照其他字符所占据的字节个数进行连续字符的打印,这样才能正确
//  的还原字符数组当中所存储的字符串内容
int main02(void)
{
    char str[10] = "我";//非ASCII码表内容按照窄字符数组存储,需要按照连续的单字节个数进行字符解析
    printf("%c%c \n", *(str + 0), *(str + 1));//方能正确的显示非ASCII码表字符串

    system("pause");
}

//04.宽窄字符使用注意事项:
//  1.严格区分宽窄字符
//  2.严格区分单字符还是字符串:
//      单字符:就是单个字符
//      字符串:含有结束标识
//  3.严格区分占用字节和使用字节:
//注:认真区分宽窄字符的存储原理!
int main03(void)
{
    wchar_t wchr1 = L'我';
    wchar_t wstr[10] = L"我的C";
    printf("%d \n", sizeof(wchr1));//2
    printf("%d \n", sizeof(wstr));//20
    printf("%d \n", sizeof(L"我的C1"));//宽字符:无论是何种字符,都按照两个字节进行存储

    system("pause");
}

//05.宽窄字符都遵守内存四区理论
int main04(void)
{
    wchar_t * p = L"我的C";//宽窄字符都遵从内存四区理论
    *p = L'X';

    system("pause");
}

//06.无论是宽字符的字符还是字符串:
//  都必须要求添加上宽字符前缀标识L
//      宽字符(单字符):L''
//      宽字符(字符串):L""
//07.宽字符细节问题:
//  1.宽字符(单字符):
//      外部标识:L''
//      格式控制:%c
//  2.宽字符(字符串)
//      外部标识:L""
//      格式控制:%ls
//注:宽字符最好同时使用(本地化)+(宽字符标识)+(宽字符函数)+(宽字符格式控制)
//  这样宽字符才能保证完全正确的显示
int main05(void)
{
    setlocale(LC_ALL, "zh-CN");
    wchar_t * pWChr = L"12345abcdef我的王";
    //printf("%s \n", pWChr);//类型不匹配
    wprintf(L"wprintf = %ls \n", pWChr);

    system("pause");
}

int main06(void)
{
    setlocale(LC_ALL, L"zh-CN");
    wchar_t wchr = L'我';
    putwchar(wchr);

    system("pause");
}
///字符串与内存四区.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char str[100] = "12345";

//01.除了代码区当中的内容不可进行修改,其他地儿几乎都可以:
//      代码区常量池:直接获取
//      代码区符号表:先读取,再生成,最后才能使用
int main07(void)
{
    char * pStr1 = "ABCDEF";//代码区
    char str[100] = "1234567";//栈内存
    char * pStr2 = (char *)malloc(100);//堆内存
    strcpy(pStr2, "ABCDEF");
    //*pStr1 = 'X';
    *pStr2 = 'X';
    *str = 'X';
    char * pChr = str;
    printf("%s, %s, %s \n", pStr1, str, pStr2);

    system("pause");
}

int main08(void)
{
    char * pStr1 = "ABCDEF";
    char * pStr2 = "ABCDEF";//代码区常量池当中的相同常量字符串只有一份儿(地址相同)
    char str1[10] = "ABCDEF";
    char str2[10] = "ABCDEF";
    char * pM1 = (char *)malloc(10);    
    char * pM2 = (char *)malloc(10);
    strcpy(pM1, "ABCDEF");
    strcpy(pM2, "ABCDEF");
    printf("%d, %d, %d \n", pStr1 == pStr2, str1 == str2, pM1 == pM2);

    system("pause");
}

char strX[100] = "ABCDEF";
char * getStr()
{
    char str[100] = "ABCDEF";//返回栈内存不可以
    char * pStr = "ABCDEF";//代码区地址
    char * pStrX = strX;//静态区地址
    return pStrX;
}

//02.函数的返回值所能返回的指针特点:
//  1.绝对不能返回指向栈内存的指针
//  2.返回指向栈内存的指针属于迷途指针:
//      迷途指针:指针所指向的内存块儿已经被回收了
int main09(void)
{
    char * pStr;
    pStr = getStr();
    printf("\n\n\n");
    free(pStr);//迷途指针
    pStr == NULL;//指针为空
    printf("%s \n", pStr);

    system("pause");
}

程序片段(02):MyString.h+MyString.c+main.c
内容概要:字符串库封装

///MyString.h
#pragma once
#include <stdlib.h>

typedef struct 
{
    char * pAStr;//首地址
    int memLen;//实际长
} MyAString;

typedef struct 
{
    wchar_t * pWStr;
    int memLen;
} MyWString;

//指定初始
void initMyAStrWithAStr(MyAString * pMyAStr, char const * pAStr);
void initMyWStrWithWStr(MyWString * pMyWStr, wchar_t const * pWStr);

//显示字串
void showMyAStr(MyAString * pMyAStr);
void showMyWStr(MyWString * pMyWStr);

//字串长度
int myAStrLen(MyAString * pMyAStr);
int myWStrLen(MyWString * pMyWstr);

//字串拷贝
MyAString * myAStrCpy(MyAString * pMyAStr, char const * pAStr);
MyWString * myWStrCpy(MyWString * pMyWStr, wchar_t const * pWStr);

//递归拷贝
MyAString * myAStrCopy(MyAString * pMyAStr, char const * pAStr, int i);
MyWString * myWStrCopy(MyWString * pMyWStr, wchar_t const * pWStr, int i);

//尾部追加
void myAStrAddAStr(MyAString * pMyAStr, char const * pAStr);
void myWStrAddWStr(MyWString * pMyWStr, wchar_t const * pWStr);

//字串查找
char * myAStrAStr(MyAString * pMyAStr, char * pAStr);
wchar_t * myWStrWStr(MyWString * pMyWStr, wchar_t * pWStr);

//随机前插
void myAStrPrevInsertAStr(MyAString * pMyAStr, char * pAStr, char * pInsertAStr);
void myWStrPrevInsertWStr(MyWString * pMyWstr, wchar_t * pWStr, wchar_t * pInsertWStr);

//随机后插
void myAStrNextInsertAStr(MyAString * pMyAStr, char * pAStr, char * pInsertAStr);
void myWStrNextInsertWStr(MyWString * pMyWStr, wchar_t * pWStr, wchar_t * pInsertWStr );

//删除单个
void myAStrDelFirstAStr(MyAString * pMyAStr, char * pAStr);
void myWStrDelFirstWStr(MyWString * pMyWStr, wchar_t * pWStr);

//删除多个
void myAStrDelAllAStr(MyAString * pMyAStr, char * pAStr);
void myWStrDelAllWStr(MyWString * pMyWStr, wchar_t * pWStr);
void myAStrDelAllAStrByRec(MyAString * pMyAStr, char * pAStr);
void myWStrDelAllWStrByRec(MyWString * pMyWStr, wchar_t * pWStr);


//随机替换(低效率)
void myAStrRepFirstAStrLow(MyAString * pMyAStr, char * pAStr, char * pRepAStr);
void myWStrRepFirstWStrLow(MyWString * pMyWStr, wchar_t * pWStr, wchar_t * pRepWStr);
void myAStrRepAllAStrLow(MyAString * pMyAStr, char * pAStr, char * pRepAStr);
void myWStrRepAllWStrLow(MyWString * pMyWStr, wchar_t * pWStr, wchar_t * pRepStr);

//随机替换(高效率)
void myAStrRepFirstAStrHigh(MyAString * pMyAStr, char * pAStr, char * pRepAStr);
void myWStrRepFirstWStrHigh(MyWString * pMyWStr, wchar_t * pWStr, wchar_t * pRepWStr);
void myAStrRepAllAStrHigh(MyAString * pMyAStr, char * pAStr, char * pRepAStr);
void myWStrRepAllWStrHigh(MyWString * pMyWStr, wchar_t * pWStr, wchar_t * pRepWStr);

//扩充功能:
//字符串压缩:
//  1.时间优先or空间优先
//  2.单字符压缩和字符串压缩
///MyString.c
#define _CRT_SECURE_NO_WARNINGS
#include "MyString.h"
#include <string.h>
#include <stdio.h> 

void showMyAStr(MyAString * pMyAStr)
{
    if (NULL == pMyAStr->pAStr)
        return;
    printf("%s \n", pMyAStr->pAStr);
}

void showMyWStr(MyWString * pMyWStr)
{
    if (NULL == pMyWStr->pWStr)
        return;
    wprintf(L"%ls \n", pMyWStr->pWStr);
}

int myAStrLen(MyAString * pMyAStr)
{
    if (NULL == pMyAStr)
        return 0;
    int i = 0;
    while (*pMyAStr->pAStr++)
    {
        ++i;
    }

    return i;
}

int myWStrLen(MyWString * pMyWStr)
{
    if (NULL == pMyWStr)
        return 0;
    int i = 0;
    while (*pMyWStr->pWStr++)
    {
        ++i;
    }

    return i;
}

MyAString * myAStrCpy(MyAString * pMyAStr, char const * pAStr)
{
    if (NULL == pMyAStr || NULL == pAStr)
        return NULL;
    //pAStrLen = strlen(pAStr);
    //for (int i = 0; i < pAStrlen + 1; ++i)
    //{
    //  *(pMyAStr->pAStr + i) = *(pAStr + i);
    //}
    char * pCopy = pMyAStr->pAStr;
    while (*pCopy++ = *pAStr++);
    return pMyAStr;
}

MyWString * myWStrCpy(MyWString * pMyWStr, wchar_t const * pWStr)
{
    if (NULL == pMyWStr || NULL == pWStr)
        return NULL;
    //int pWStrLen = wcslen(pWStr);
    //for (int i = 0; i < pWStrLen + 1; ++i)
    //{
    //  *(pMyWStr->pWStr + i) = *(pWStr + i);
    //}
    wchar_t * pCopy = pMyWStr->pWStr;
    while (*pCopy++ = *pWStr++);
    return pMyWStr;
}

MyAString * myAStrCopy(MyAString * pMyAStr, char const * pAStr, int i)
{
    if (!(*(pMyAStr->pAStr + i) = *(pAStr + i++)))
        return pMyAStr;
    myAStrCopy(pMyAStr, pAStr, i);
}

MyWString * myWStrCopy(MyWString * pMyWStr, wchar_t const * pWStr, int i)
{
    //static char * pTemp = pWStr;//静态局部变量不能使用变量进行初始化
    if (!(*(pMyWStr->pWStr + i) = *(pWStr + i++)))
        return pMyWStr;
    myWStrCopy(pMyWStr, pWStr, i);
}

void    initMyAStrWithAStr(MyAString * pMyAStr, char const * pAStr)
{
    if (NULL == pMyAStr || NULL == pAStr)
        abort();
    int myStrLen = strlen(pAStr) + 1;
    pMyAStr->pAStr = (char *)malloc(myStrLen * sizeof(char));
    //myAStrCpy(pMyAStr, pAStr);
    myAStrCopy(pMyAStr, pAStr, 0);
    pMyAStr->memLen = myStrLen + 1;
}

void initMyWStrWithWStr(MyWString * pMyWStr, wchar_t const * pWStr)
{
    if (NULL == pMyWStr || NULL == pWStr)
        abort();
    int myWStrLen = wcslen(pWStr) + 1;
    pMyWStr->pWStr = (wchar_t *)malloc(myWStrLen * sizeof(wchar_t));
    //myWStrCpy(pMyWStr, pWStr);
    myWStrCopy(pMyWStr, pWStr, 0);
    pMyWStr->memLen = myWStrLen;
}

void myAStrAddAStr(MyAString * pMyAStr, char const * pAStr)
{
    if (NULL == pMyAStr || NULL == pAStr)
        abort();
    if (NULL == pMyAStr)
    {
        initMyAStrWithAStr(pMyAStr, pAStr);
    }
    else
    {
        int myAStrLen = strlen(pMyAStr->pAStr);
        int addAStrLen = strlen(pAStr);
        int allAStrLen = myAStrLen + addAStrLen + 1;
        if (pMyAStr->memLen < allAStrLen)
        {
            pMyAStr->pAStr = (char *)realloc(pMyAStr->pAStr, allAStrLen * sizeof(char));
            pMyAStr->memLen = allAStrLen;
        }
        strcat(pMyAStr->pAStr, pAStr);
    }
}

void myWStrAddWStr(MyWString * pMyWStr, wchar_t const * pWStr)
{
    if (NULL == pMyWStr || NULL == pWStr)
        abort();
    if (NULL == pMyWStr->pWStr)
    {
        initMyWStrWithWStr(pMyWStr, pWStr);
    }
    else
    {
        int myWStrLen = wcslen(pMyWStr->pWStr);
        int addWStrLen = wcslen(pWStr);
        int allWStrLen = myWStrLen + addWStrLen + 1;
        if (pMyWStr->memLen < allWStrLen)
        {
            pMyWStr->pWStr = (wchar_t *)realloc(pMyWStr->pWStr, allWStrLen * sizeof(wchar_t));
            pMyWStr->memLen = allWStrLen;
        }
        wcscat(pMyWStr->pWStr, pWStr);
    }
}

char * myAStrAStr(MyAString * pMyAStr, char * pAStr)
{
    if (NULL == pMyAStr || NULL == pMyAStr->pAStr || NULL == pAStr)
        return NULL;
    int pMyAStrLen = strlen(pMyAStr->pAStr);
    int pAStrLen = strlen(pAStr);
    int diffAStrLen = pMyAStrLen - pAStrLen;
    for (int i = 0; i <= diffAStrLen; ++i)
    {
        int find = 1;
        for (int j = 0; j < pAStrLen; ++j)
        {
            if (*(pMyAStr->pAStr + i + j) != *(pAStr + j))
            {
                find = 0;
                break;
            }
        }
        if (find)
        {
            return pMyAStr->pAStr + i;
        }
    }
    return NULL;
}

wchar_t * myWStrWStr(MyWString * pMyWStr, wchar_t  * pWStr)
{
    if (NULL == pMyWStr || NULL == pMyWStr->pWStr || NULL == pWStr)
        return NULL;
    wchar_t * pCopy = pMyWStr->pWStr;
    while (*pCopy)
    {
        int find = 1;
        wchar_t *pTmp = pCopy;
        wchar_t *pTemp = pWStr;
        while (*pTemp)
        {
            if ('\0' == *pTmp || *pTmp != *pTemp)
            {
                find = 0;
                break;
            }
            ++pTmp;
            ++pTemp;
        }
        if (find)
        {
            return pCopy;
        }
        ++pCopy;
    }
    return NULL;
}

void myAStrPrevInsertAStr(MyAString * pMyAStr, char * pAStr, char * pInsertAStr)
{
    if (NULL == pMyAStr || NULL == pMyAStr->pAStr || NULL == pInsertAStr)
        return;
    char * pFind = myAStrAStr(pMyAStr, pAStr);
    if (NULL == pFind)
        return;
    int myAStrLen = strlen(pMyAStr->pAStr);
    int insertAStrLen = strlen(pInsertAStr);
    int totalAStrLen = myAStrLen + insertAStrLen + 1;
    int relativeLen = pFind - pMyAStr->pAStr;
    if (pMyAStr->memLen < totalAStrLen)
    {
        pMyAStr->pAStr = (char *)realloc(pMyAStr->pAStr, totalAStrLen * sizeof(char));
        pMyAStr->memLen = totalAStrLen;
    }
    for (int i = myAStrLen; i >= relativeLen; --i)
    {
        pMyAStr->pAStr[i + insertAStrLen] = pMyAStr->pAStr[i];
    }
    for (int i = relativeLen, j = 0; i < relativeLen + insertAStrLen; ++i)
    {
        pMyAStr->pAStr[i] = pInsertAStr[j++];
    }
}

void myWStrPrevInsertWStr(MyWString * pMyWStr, wchar_t * pWStr, wchar_t * pInsertWStr)
{
    if (NULL == pMyWStr->pWStr || NULL == pInsertWStr)
        return;
    wchar_t * pFindWStr = wcsstr(pMyWStr->pWStr, pWStr);
    if (NULL == pFindWStr)
        return;
    int myWStrLen = wcslen(pMyWStr->pWStr);
    int insertWStrLen = wcslen(pInsertWStr);
    int totalWStrLen = myWStrLen + insertWStrLen + 1;
    if (pMyWStr->memLen < totalWStrLen)
    {
        pMyWStr->pWStr = (wchar_t *)realloc(pMyWStr->pWStr, totalWStrLen * sizeof(wchar_t));
        pMyWStr->memLen = totalWStrLen;
    }
    pFindWStr = wcsstr(pMyWStr->pWStr, pWStr);
    for (wchar_t * p = pMyWStr->pWStr + myWStrLen; p >= pFindWStr; --p)
    {
        *(p + insertWStrLen) = *p;
    }
    while (*pInsertWStr)
    {
        *pFindWStr++ = *pInsertWStr++;
    }
}

void myAStrNextInsertAStr(MyAString * pMyAStr, char * pAStr, char * pInsertAStr)
{
    if (NULL == pMyAStr->pAStr || NULL == pInsertAStr)
        return;
    char * pFindAStr = strstr(pMyAStr->pAStr, pAStr);
    if (NULL == pFindAStr)
        return;
    int myAStrLen = strlen(pMyAStr->pAStr);
    int findAStrLen = strlen(pFindAStr);
    int relAStrLen = myAStrLen - findAStrLen;
    int insertAStrLen = strlen(pInsertAStr);
    int totalAStrLen = myAStrLen + insertAStrLen + 1;
    if (pMyAStr->memLen < totalAStrLen)
    {
        pMyAStr->pAStr = (char *)realloc(pMyAStr->pAStr, totalAStrLen * sizeof(char));
        pMyAStr->memLen = totalAStrLen;
    }
    for (int i = myAStrLen; i >= relAStrLen + strlen(pAStr); --i)
    {
        *(pMyAStr->pAStr + i + insertAStrLen) = *(pMyAStr->pAStr + i);
    }
    for (int i = 0; i < insertAStrLen; ++i)
    {
        *(pMyAStr->pAStr + relAStrLen + strlen(pAStr) + i) = *(pInsertAStr + i);
    }
}

void myWStrNextInsertWStr(MyWString * pMyWStr, wchar_t * pWStr, wchar_t * pInsertWStr)
{
    if (NULL == pMyWStr->pWStr || NULL == pInsertWStr)
        return;
    wchar_t * pFindWStr = wcsstr(pMyWStr->pWStr, pWStr);
    if (NULL == pFindWStr)
        return;
    int myWStrLen = wcslen(pMyWStr->pWStr);
    int insertWStrLen = wcslen(pInsertWStr);
    int totalWStrLen = myWStrLen + insertWStrLen + 1;
    if (pMyWStr->memLen < totalWStrLen)
    {
        pMyWStr->pWStr = (wchar_t *)realloc(pMyWStr->pWStr, totalWStrLen * sizeof(wchar_t));
        pMyWStr->memLen = totalWStrLen;
    }
    pFindWStr = wcsstr(pMyWStr->pWStr, pWStr);
    for (wchar_t * p = pMyWStr->pWStr + myWStrLen; p >= pFindWStr + wcslen(pWStr); --p)
    {
        *(p + insertWStrLen) = *p;
    }
    while (*pInsertWStr)
    {
        *(pFindWStr + wcslen(pWStr)) = *pInsertWStr++;
        ++pFindWStr;
    }
}

void myAStrDelFirstAStr(MyAString * pMyAStr, char * pAStr)
{
    char * pFindAStr = strstr(pMyAStr->pAStr, pAStr);
    if (NULL == pFindAStr)
        return;
    int delAStrLen = strlen(pAStr);
    char * pRepAStr = pFindAStr + delAStrLen;
    //while ('\0' != *pFindAStr)
    //while (0 != pFindAStr)
    //while (*pFindAStr)
    //{
    //  *pFindAStr = *pRepAStr;
    //  ++pRepAStr;
    //  ++pFindAStr;
    //}
    //while (*pFindAStr++ = *pRepAStr++);
    //while (*pFindAStr = *(pFindAStr + delAStrLen))
    //{
    //  ++pFindAStr;
    //}
    while (*pFindAStr++ = *(pFindAStr + delAStrLen));
}

void myWStrDelFirstWStr(MyWString * pMyWStr, wchar_t * pWStr)
{
    wchar_t * pFindWStr = wcsstr(pMyWStr->pWStr, pWStr);
    if (NULL == pFindWStr)
        return;
    int delWStrLen = wcslen(pWStr);
    wchar_t *   pRepWStr = pFindWStr + delWStrLen;
    //while ('\0' != *pFindWStr);
    //while (0 != *pFindWStr);
    //while (*pFindWStr)
    //{
    //  *pFindWStr = *pRepWStr;
    //  ++pRepWStr;
    //  ++pFindWStr;
    //}
    //while (*pFindWStr++ = *pRepWStr++);
    //while (*pFindWStr = *(pFindWStr + delWStrLen))
    //{
    //  ++pFindWStr;
    //}
    while (*pFindWStr++ = *(pFindWStr + delWStrLen));
}

void myAStrDelAllAStr(MyAString * pMyAStr, char * pAStr)
{
    char * pFindAStr = strstr(pMyAStr->pAStr, pAStr);
    while (NULL != pFindAStr)
    {
        myAStrDelFirstAStr(pMyAStr, pAStr);
        pFindAStr = strstr(pMyAStr->pAStr, pAStr);
    }
}

void myWStrDelAllWStr(MyWString * pMyWStr, wchar_t * pWStr)
{
    wchar_t * pFindWStr = wcsstr(pMyWStr->pWStr, pWStr);
    if (NULL != pFindWStr)
    {
        do
        {
            myWStrDelFirstWStr(pMyWStr, pWStr);
            pFindWStr = wcsstr(pMyWStr->pWStr, pWStr);
        } while (NULL != pFindWStr);
    }
}

void myAStrDelAllAStrByRec(MyAString * pMyAStr, char * pAStr)
{
    if (NULL == strstr(pMyAStr->pAStr, pAStr))
        return;
    myAStrDelFirstAStr(pMyAStr, pAStr);
    myAStrDelAllAStr(pMyAStr, pAStr);
}

void myWStrDelAllWStrByRec(MyWString * pMyWStr, wchar_t * pWStr)
{
    if (NULL == wcsstr(pMyWStr->pWStr, pWStr))
        return;
    myWStrDelFirstWStr(pMyWStr, pWStr);
    myWStrDelAllWStrByRec(pMyWStr, pWStr);
}

void myAStrRepFirstAStrLow(MyAString * pMyAStr, char * pAStr, char * pRepAStr)
{
    char * pFindAStr = strstr(pMyAStr->pAStr, pAStr);
    if (NULL == pFindAStr)
        return;
    myAStrPrevInsertAStr(pMyAStr, pAStr, pRepAStr);
    myAStrDelFirstAStr(pMyAStr, pAStr);
}

void myWStrRepFirstWStrLow(MyWString * pMyWStr, wchar_t * pWStr, wchar_t * pRepWStr)
{
    wchar_t * pFindWStr = wcsstr(pMyWStr->pWStr, pWStr);
    if (NULL == pFindWStr)
        return;
    myWStrPrevInsertWStr(pMyWStr, pWStr, pRepWStr);
    myWStrDelFirstWStr(pMyWStr, pWStr);
}

void myAStrRepAllAStrLow(MyAString * pMyAStr, char * pAStr, char * pRepAStr)
{
    if (NULL == strstr(pMyAStr->pAStr, pAStr))
        return;
    myAStrRepFirstAStrLow(pMyAStr, pAStr, pRepAStr);
    myAStrRepAllAStrLow(pMyAStr, pAStr, pRepAStr);
}

void myWStrRepAllWStrLow(MyWString * pMyWStr, wchar_t * pWStr, wchar_t * pRepWStr)
{
    if (NULL == wcsstr(pMyWStr->pWStr, pWStr))
        return;
    myWStrRepFirstWStrLow(pMyWStr, pWStr, pRepWStr);
    myWStrRepAllWStrLow(pMyWStr, pWStr, pRepWStr);
}

void myAStrRepFirstAStrHigh(MyAString * pMyAStr, char * pAStr, char * pRepAStr)
{
    char * pFindAStr = strstr(pMyAStr->pAStr, pAStr);
    if (NULL == pFindAStr)
        return;
    int pAStrLen = strlen(pAStr);
    int pRepAStrLen = strlen(pRepAStr);
    int allAStrLen = strlen(pMyAStr->pAStr) + 1 + (pRepAStrLen - pAStrLen);
    if (pAStrLen < pRepAStrLen)
    {
        if (allAStrLen < pMyAStr->memLen)
        {
            pMyAStr->pAStr = (char *)realloc(pMyAStr->pAStr, allAStrLen*sizeof(char));
            pMyAStr->memLen = allAStrLen;
        }
        pFindAStr = strstr(pMyAStr->pAStr, pAStr);
        int addAStrLen = pRepAStrLen - pAStrLen;
        for (char * p = pMyAStr->pAStr + strlen(pMyAStr->pAStr); p >= pFindAStr + pAStrLen; --p)
        {
            *(p + addAStrLen) = *p;
        }
        while (*pRepAStr)
        {
            *pFindAStr++ = *pRepAStr;
            ++pRepAStr;
        }
    }
    else if (pAStrLen == pRepAStrLen)
    {
        while (*pRepAStr)
        {
            *pFindAStr++ = *pRepAStr++;
        }
    }
    else
    {
        int subAStrLen = pRepAStrLen - pAStrLen;
        while (*pRepAStr)
        {
            *pFindAStr++ = *pRepAStr++;
        }
        char * pTemp = pFindAStr + pAStrLen;
        while (*pTemp++ = *(pTemp + subAStrLen));
    }
}

void myAStrRepAllAStrHigh(MyAString * pMyAStr, char * pAStr, char * pRepAStr)
{
    if (NULL == strstr(pMyAStr->pAStr, pAStr))
        return;
    myAStrRepFirstAStrHigh(pMyAStr, pAStr, pRepAStr);
    myAStrRepAllAStrHigh(pMyAStr, pAStr, pRepAStr);
}

void myWStrRepFirstWStrHigh(MyWString * pMyWStr, wchar_t * pWStr, wchar_t * pRepWStr)
{
    wchar_t * pFindWStr = wcsstr(pMyWStr->pWStr, pWStr);
    if (NULL == pFindWStr)
        return;
    int pWStrLen = wcslen(pWStr);
    int pRepWStrLen = wcslen(pRepWStr);
    if (pWStrLen < pRepWStrLen)
    {
        int totalWStrLen = wcslen(pMyWStr->pWStr) + (pRepWStrLen - pWStrLen) + 1;
        if (pMyWStr->memLen < totalWStrLen)
        {
            pMyWStr->pWStr = (wchar_t *)realloc(pMyWStr->pWStr, totalWStrLen * sizeof(wchar_t));
            pMyWStr->memLen = totalWStrLen;
        }
        pFindWStr = wcsstr(pMyWStr->pWStr, pWStr);
        int myWStrLen = wcslen(pMyWStr->pWStr);
        int moveWStrLen = pRepWStrLen - pWStrLen;
        for (wchar_t * p = pMyWStr->pWStr + myWStrLen; p >= pFindWStr + pWStrLen; --p)
        {
            *(p + moveWStrLen) = *p;
        }
        while (*pRepWStr)
        {
            *pFindWStr++ = *pRepWStr++;
        }
    }
    else if (pWStrLen == pRepWStr)
    {
        while (*pRepWStr)
        {
            *pFindWStr++ = *pRepWStr++;
        }
    }
    else
    {
        int moveWStrLen = pWStrLen - pRepWStrLen;
        wchar_t * p = pFindWStr + pWStrLen - moveWStrLen;
        while (*p++ = *(p + moveWStrLen));
        while (*pRepWStr)
        {
            *pFindWStr++ = *pRepWStr++;
        }
    }
}

void myWStrRepAllWStrHigh(MyWString * pMyWStr, wchar_t * pWStr, wchar_t * pRepWStr)
{
    if (NULL == wcsstr(pMyWStr->pWStr, pWStr))
        return;
    myWStrRepFirstWStrHigh(pMyWStr, pWStr, pRepWStr);
    myWStrRepAllWStrHigh(pMyWStr, pWStr, pRepWStr);
}
///main.c
#define _CRT_SECURE_NO_WARNINGS
#include "MyString.h"
#include <locale.h>
#include <stdio.h>

int main(void)
{
    setlocale(LC_ALL, "zh-CN");
    MyAString myAString;
    MyWString myWString;
    initMyAStrWithAStr(&myAString, "ABC123321CBA123");
    initMyWStrWithWStr(&myWString, L"ABC123321CBA123");
    myAStrAddAStr(&myAString,"HaHaHa嘻嘻嘻");
    myWStrAddWStr(&myWString, L"XiXiXi哈哈哈");
    //myAStrPrevInsertAStr(&myAString, "123", "JKL");
    //myAStrNextInsertAStr(&myAString, "123", "JKL");
    //myWStrPrevInsertWStr(&myWString, L"123", L"JKL");
    //myWStrNextInsertWStr(&myWString, L"123", L"JKL"); 
    //myAStrDelFirstAStr(&myAString, "ABC");
    //myWStrDelFirstWStr(&myWString, L"ABC");
    //myAStrDelAllAStr(&myAString, "123");
    //myWStrDelAllWStr(&myWString, L"123");
    //myAStrDelAllAStrByRec(&myAString, "123");
    //myWStrDelAllWStrByRec(&myWString, L"123");
    //myAStrRepFirstAStrLow(&myAString, "123", "JKL");
    //myWStrRepFirstWStrLow(&myWString, L"123", L"JKL");
    //myAStrRepAllAStrLow(&myAString, "123", "JKL");
    //myWStrRepAllWStrLow(&myWString, L"123", L"JKL");
    //myAStrRepFirstAStrHigh(&myAString, "123", "JKL");
    //myAStrRepAllAStrHigh(&myAString, "123", "JKL");
    //myWStrRepFirstWStrHigh(&myWString, L"123", L"JKL");
    //myWStrRepAllWStrHigh(&myWString, L"123", L"JKL");
    showMyAStr(&myAString);
    showMyWStr(&myWString);
    //printf("%s \n", myAStrAStr(&myAString, "123321"));
    //wprintf(L"%ws \n", myWStrWStr(&myWString, L"123321"));

    system("pause");
}

程序片段(03):myarray.h+myarray.c+main.c
内容概要:数组库

///myarray.h
#pragma once//1.包含的时候,只包含一次[预编译的时候]
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define datatype int//2.double ,char-X->int * struct[扩展的时候,只能扩展基本数据类型]

struct array
{//3.[采用结构体进行数组模拟]
    datatype *pstart;//数组首地址
    int length;//长度[数组]
    int sortstat;//有序或者无序,0无序,1有序
};

struct Res
{//4.[采用结构体模拟字符串类型的数组,由于字符串数组当中的每个元素存储的是字符串地址,所以采用二级指针进行该数组的模拟动作]
    datatype **ppstat;//创建指针数组[二级指针(一级指针地址)-->一级指针(变量地址)-->零级指针(数据)]
    int n;
};

//5.[为结构体模拟数组创建一些针对于该数组的操作函数]
//[初始化]
void init(struct array *parr);
void initwithdata(struct array *parr, datatype data);
void initwitharray(struct array *parr, datatype *pdata, int datalength);

//[增加]
void addobject(struct array *parr, datatype data);
void addobjects(struct array *parr, datatype *pdata, int datalength);

//[插入]
void insertobject(struct array *parr, datatype data, datatype insertdata);//根据位置插入
void insertobjects(struct array *parr, datatype data, datatype *pdata, int datalength);

//[删除]
void deletefirstobject(struct array *parr, datatype data);
void deleteallobject(struct array *parr, datatype);

//[修改]
void changefirstobject(struct array *parr, datatype data, datatype newdata);
void changeallobject(struct array *parr, datatype data, datatype newdata);

//[查询]
datatype * findfirst(struct array *parr, datatype data);//查找
struct Res findall(struct array *parr, datatype data);//查找全部

//[显示]
void show(struct array *parr);

//5.数组库扩展功能实现
//      (1)排序:选择,插入,二分插入,冒泡,堆,快速,从左之右
//      (2)插值查找,二分查找
//      (3)初始化with可变参数
///myarray.c
#include "myarray.h"

void init(struct array *parr)
{//1.[初始化数据结构] 
    if (parr != NULL)
    {
        parr->pstart = NULL;
        parr->length = 0;
        parr->sortstat = 0;
    }
    else
    {
        printf("init error!");
    }
}

void initwithdata(struct array *parr, datatype data)
{//2[.初始化结构体数据(单个)]
    if (parr != NULL)
    {
        parr->pstart = malloc(sizeof(datatype));
        *(parr->pstart) = data;//初始化
        parr->length = 1;
        parr->sortstat = 0;
    }
    else
    {
        printf("initwithdata error!");
    }
}

void initwitharray(struct array *parr, datatype *pdata, int datalength)
{//3.[初始化结构体数据(多个)]
    if (parr != NULL)
    {
        parr->pstart = malloc(sizeof(datatype)*datalength);
        memcpy(parr->pstart, pdata, sizeof(datatype)*datalength);
        parr->length = datalength;
        parr->sortstat = 0;
    }
    else
    {
        printf("initwitharray error!");
    }
}

void addobject(struct array *parr, datatype data)
{
    if (parr != NULL)
    {//4.该结构体模拟的数组存在
        if (parr->pstart == NULL || parr->length == 0)
        {//5.该数组的状态处于初始化状态
            initwithdata(parr, data);
        }
        else
        {
            //6.数组操作说明:5 a[4]-->[5个元素-->最后一个元素为a[4]-->5这个索引为数组外尾第一个元素]
            parr->pstart = realloc(parr->pstart, (parr->length + 1)*sizeof(datatype));//拓展内存
            parr->pstart[parr->length] = data;//插入
            parr->length += 1;//长度自增
        }
    }
    else
    {
        printf("addobject error!");
    }
}

void addobjects(struct array *parr, datatype *pdata, int datalength)
{
    if (parr != NULL)
    {
        if (parr->pstart == NULL || parr->length == 0)
        {
            initwitharray(parr, pdata, datalength);//调用初始化
        }
        else
        {
            //7.说明:12345 a[4] &a[5]
            parr->pstart = realloc(parr->pstart, (parr->length + datalength)*sizeof(datatype));//拓展内存
            memcpy(parr->pstart + parr->length, pdata, datalength*sizeof(datatype));//8.追加一个数组的时候,可以指定位置进行追加动作,如果是追加多个数据,建议采用内存拷贝机制,提升效率
            parr->length += datalength;//长度自增
        }
    }
    else
    {
        printf("addobjects error!");
    }
}

void insertobject(struct array *parr, datatype data, datatype insertdata)
{
    if (parr != NULL)
    {
        datatype *pfind = findfirst(parr, data);
        if (pfind == NULL)
        {
            printf("can not insertobject error!");
        }
        else
        {
            int curr = pfind - parr->pstart;//9.指针相减确定相对下标,当前数据的相对下标
            //printf("\n curr=%d", curr);//10.同类型的指针相减,自动除以该指针所指向的变量类型长度
            parr->pstart = realloc(parr->pstart, (parr->length + 1)*sizeof(datatype));//拓展内存
            for (int i = parr->length - 1; i >= curr; i--)//11.减一的目的是为了从最后一个元素开始进行数据的拖动
            {
                parr->pstart[i + 1] = parr->pstart[i];//往后拖动
            }
            parr->pstart[curr] = insertdata;//插入数据
            parr->length += 1;//长度自增
        }
    }
    else
    {
        printf("insertobject error!");
    }
}

void insertobjects(struct array *parr, datatype data, datatype *pdata, int datalength)
{
    if (parr != NULL)
    {
        datatype *pfind = findfirst(parr, data);
        if (pfind == NULL)
        {
            printf("can not insertobject error!");
        }
        else
        {
            int curr = pfind - parr->pstart;
            parr->pstart = realloc(parr->pstart, (parr->length + datalength)*sizeof(datatype));
            for (int i = parr->length - 1; i >= curr; i--)
            {
                parr->pstart[i + datalength] = parr->pstart[i];
            }
            memcpy(parr->pstart + curr, pdata, datalength*sizeof(datatype));//拷贝数组
            parr->length += datalength;//修改长度
        }
    }
    else
    {
        printf("insertobjects error!");
    }
}

void deletefirstobject(struct array *parr, datatype data)
{
    if (parr!=NULL)
    {
        datatype *pfind = findfirst(parr, data);
        if (pfind==NULL)
        {
            printf("can not find deleteerror!");
        }
        else
        {
            int curr = pfind - parr->pstart;//指针相减确定下标,当前数据的相对下标
            for (int i = curr; i < parr->length - 1; i++)
            {
                parr->pstart[i] = parr->pstart[i + 1];//删除,从后向前移动
            }
            parr->length -= 1;//长度自减
            parr->pstart = realloc(parr->pstart, (parr->length)*sizeof(datatype));//压缩内存
        }
    }
    else
    {
        printf("deleteobject error!");
    }
}

void deleteallobject(struct array *parr, datatype data)
{
    if (parr!=NULL)
    {
        for (int *pcurr = findfirst(parr, data); pcurr != NULL; pcurr = findfirst(parr, data))
        {
            deletefirstobject(parr, data);
        }
    }
    else
    {
        printf("deleteobject error!");
    }
}

void changefirstobject(struct array *parr, datatype data, datatype newdata)
{
    if (parr != NULL)
    {
        datatype *pfind = findfirst(parr, data);
        if (pfind == NULL)
        {
            printf("can not find changeobject error!");
        }
        else
        {
            *pfind = newdata;
        }
    }
    else
    {
        printf("deleteobject error");
    }
}

void changeallobject(struct array *parr, datatype data, datatype newdata)
{
    if (parr!=NULL)
    {
        for (int *pcurr = findfirst(parr, data); pcurr != NULL; pcurr = findfirst(parr, data))
        {
            changefirstobject(parr, data, newdata);
        }
    }
    else
    {
        printf("changeallobject error");
    }
}

datatype * findfirst(struct array *parr, datatype data)
{
    if (parr == NULL || parr->pstart == NULL || parr->length == 0)
    {
        printf("没有数据咋查找?");
        return NULL;
    }
    else
    {
        //5 0-4
        datatype *pfind = NULL;
        for (int i = 0; i < parr->length; i++)
        {
            if (data == parr->pstart[i])
            {
                pfind = parr->pstart[i];//parr->pstart+i
                break;
            }
        }
        return pfind;
    }
}

struct Res findall(struct array *parr, datatype data)
{
    struct Res ResA;
    ResA.n = 0;//统计元素个数
    for (int i = 0; i < parr->length; i++)
    {
        if (data==parr->pstart[i])//基本完成
        {
            ResA.n++;
        }
    }
    ResA.ppstat = malloc(sizeof(datatype *)*ResA.n);//分配内存[指针数组占用]
    int j = 0;//代表下标
    for (int i = 0; i < parr->length; i++)
    {
        if (data==parr->pstart[i])
        {
            ResA.ppstat[j++] = parr->pstart + i;//保存地址
        }
    }

    return ResA;
}

void show(struct array *parr)
{
    if (parr == NULL || parr->pstart == NULL || parr->length == 0)
    {
        printf("没有数据咋显示?");
        return;
    }
    else
    {
        //5 0-4
        printf("\n数组此时状态\n");
        for (int i = 0; i < parr->length; i++)
        {
            printf("%4d", parr->pstart[i]);//打印数据
        }
    }
}
///main.c
#include "myarray.h"

void main()
{
    struct array mydata;
    int a[10] = { 1, 2, 6, 4, 5, 6, 7, 8, 9, 6 };
    int b[5] = { 11, 12, 13, 14 };
    int c[4] = { 21, 22, 23, 24 };
    initwitharray(&mydata, a, 10);
    show(&mydata);

    //changeallobject(&mydata, 6, 660);
    //changefirstobject(&mydata,5, 950);
    //insertobjects(&mydata, 8, c, 4);
    //deleteallobject(&mydata, 6);
    //deletefirstobject(&mydata, 6);
    //addobjects(&mydata, b, 5);
    //addobjects(&mydata, c, 4);
    //insertobject(&mydata, 1,999);//根据位置插入

    struct Res res = findall(&mydata, 6);
    for (int i = 0; i < res.n; i++)
    {
        printf("\n%p,%d", res.ppstat[i], *res.ppstat[i]);
    }

    show(&mydata);

    system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值