zoukankan      html  css  js  c++  java
  • 20160223.CCPP体系具体解释(0033天)

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

    ///MyArray.h
    #pragma once
    
    #define DT int//类型通用
    
    typedef struct
    {
        DT * pStart;//起始地址
        int len;//元素个数
        int sortState;//排序状态(0无序+1有序)
    }Array;
    
    typedef struct
    {
        DT ** ppStart;
        int len;
    }ResArray;
    
    void initArray(Array * pArray);
    void initArrayWithData(Array * pArray, DT data);
    void initArrayWithDatas(Array * pArray, DT * pDatas, int pDatasLen);
    void showArray(Array * pArray);
    void    arrayAddData(Array * pArray, DT data);
    void arrayAddDatas(Array * pArray, DT * pDatas, int pDatasLen);
    DT * arrayFindFirstData(Array * pArray, DT data);
    ResArray arrayFindAllDatas(Array * pArray, DT data);
    void arrayInsertData(Array * pArray, DT data, DT insertData);
    void arrayInsertDatas(Array * pArray, DT data, DT * pInsertDatas, int pInsertDatasLen);
    void arrayDelFirstData(Array * pArray, DT data);
    void arrayDelAllData(Array * pArray, DT data);
    void arrayUpdateFirstData(Array * pArray, DT oldData, DT newData);
    void arrayUpdateAllData(Array * pArray , DT oldData, DT newData);
    //扩展功能:
    //  1.排序:
    //      冒泡(左右)-->选择-->插入-->高速(单线程)-->希尔(多线程)-->堆排序
    //  2.查找:
    //      二分-->插值
    //  3.可变:
    //      可变參数
    //  4.多线程检索
    ///MyArray.c
    #include "MyArray.h"
    #include <stdlib.h>
    #include <memory.h>
    #include <stdio.h>
    
    void initArray(Array * pArray)
    {
        if (NULL == pArray)
        {
            printf("init error! 
    ");
            abort();
        }
        pArray->pStart = NULL;
        pArray->len = 0;
        pArray->sortState = 0;
    }
    
    void initArrayWithData(Array * pArray, DT data)
    {
        if (NULL == pArray)
        {
            printf("init error! 
    ");
            abort();
        }
        pArray->pStart = (DT *)malloc(sizeof(DT));
        *(pArray->pStart) = data;
        pArray->len = 1;
        pArray->sortState = 0;
    }
    
    void initArrayWithDatas(Array * pArray, DT * pDatas, int pDatasLen)
    {
        if (NULL == pArray)
        {
            printf("init error! 
    ");
            abort();
        }
        pArray->pStart = (DT *)malloc(pDatasLen * sizeof(DT));
        memcpy(pArray->pStart, pDatas, pDatasLen * sizeof(DT));
        pArray->len = pDatasLen;
        pArray->sortState = 0;
    }
    
    void showArray(Array * pArray)
    {
        if (NULL == pArray || NULL == pArray->pStart || 0 == pArray->len)
        {
            printf("没有数据咋打印? 
    ");
            abort();
        }
        printf("数组此时状态: 
    ");
        for (int i = 0; i < pArray->len; ++i)
        {//优先级问题:从第一个标识符開始进行推断(从左往右不断结合)
            printf("%4d", pArray->pStart[i]);
        }
    }
    
    void arrayAddData(Array * pArray, DT data)
    {
        if (NULL == pArray)
        {
            printf("数组根本不存在! 
    ");
            abort();
        }
        if (NULL == pArray->pStart)
        {
            initArrayWithData(pArray, data);
        }
        else
        {
            pArray->pStart = (DT *)realloc(pArray->pStart, (pArray->len + 1) * sizeof(DT));
            pArray->pStart[pArray->len] = data;
            ++pArray->len;
        }
    }
    
    void arrayAddDatas(Array * pArray, DT * pDatas, int pDatasLen)
    {
        if (NULL == pArray)
        {
            printf("数组根本不存在! 
    ");
            abort();
        }
        if (NULL == pArray->pStart)
        {
            initArrayWithDatas(pArray, pDatas, pDatasLen);
        }
        else
        {
            pArray->pStart = (DT *)realloc(pArray->pStart, (pArray->len + pDatasLen) * sizeof(DT));
            memcpy(pArray->pStart + pArray->len, pDatas, pDatasLen * sizeof(DT));
            pArray->len += pDatasLen;
        }
    }
    
    DT * arrayFindFirstData(Array * pArray, DT data)
    {
        if (NULL == pArray)
        {
            printf("数组根本不存在! 
    ");
            abort();
        }
        if (NULL == pArray->pStart)
        {
            printf("数组未初始化数据! 
    ");
            abort();
        }
        if (0 == pArray->len)
        {
            printf("数组没有数据! 
    ");
            abort();
        }
        for (int i = 0; i < pArray->len; ++i)
        {
            if (data == *(pArray->pStart + i))
            {
                return pArray->pStart + i;
            }
        }
        return NULL;
    }
    
    ResArray arrayFindAllDatas(Array * pArray, DT data)
    {
        if (NULL == pArray)
        {
            printf("数组根本不存在! 
    ");
            abort();
        }
        if (NULL == pArray->pStart)
        {
            printf("数组未初始化数据! 
    ");
            abort();
        }
        ResArray resArray;
        resArray.len = 0;
        for (int i = 0; i < pArray->len; ++i)
        {
            if (data == pArray->pStart[i])
            {
                ++resArray.len;
            }
        }
        resArray.ppStart = (DT **)malloc(resArray.len * sizeof(DT *));
        int j = 0;
        for (int i = 0; i < pArray->len; ++i)
        {
            if (data == pArray->pStart[i])
            {
                resArray.ppStart[j++] = pArray->pStart + i;
            }
        }
        return resArray;
    }
    
    void arrayInsertData(Array * pArray, DT data, DT insertData)
    {
        if (NULL == pArray)
        {
            printf("数组根本不存在! 
    ");
            abort();
        }
        if (NULL == pArray->pStart)
        {
            printf("数组没有初始化数据! 
    ");
            abort();
        }
        if (0 == pArray->len)
        {
            printf("数组没有数据! 
    ");
            abort();
        }
        DT * pFindData = arrayFindFirstData(pArray, data);
        if (NULL == pFindData)
        {
            printf("未能找到数据待插入的位置! 
    ");
            abort();
        }
        int relPos = pFindData - pArray->pStart;
        pArray->pStart = (DT *)realloc(pArray->pStart, (pArray->len + 1) * sizeof(DT));
        for (int i = pArray->len; i >= relPos; --i)
        {
            *(pArray->pStart + i + 1) = *(pArray->pStart + i);
        }
        *(pArray->pStart + relPos) = insertData;
        ++pArray->len;
    }
    
    void arrayInsertDatas(Array * pArray, DT data, DT * pInsertDatas, int pInsertDatasLen)
    {
        if (NULL == pArray)
        {
            printf("数组根本不存在! 
    ");
            abort();
        }
        if (NULL == pArray->pStart)
        {
            printf("数组未初始化数据! 
    ");
            abort();
        }
        if (0 == pArray->len)
        {
            printf("数组没有数据! 
    ");
            abort();
        }
        DT * pFindData = arrayFindFirstData(pArray, data);
        if (NULL == pFindData)
        {
            printf("未能找到数据待插入的位置! 
    ");
            abort();
        }
        int relPos = pFindData - pArray->pStart;
        pArray->pStart = (DT *)realloc(pArray->pStart, (pArray->len + pInsertDatasLen) * sizeof(DT));
        for (int i = pArray->len; i >= relPos; --i)
        {
            *(pArray->pStart + i + pInsertDatasLen) = *(pArray->pStart + i);
        }
        memcpy(pArray->pStart + relPos, pInsertDatas, pInsertDatasLen * sizeof(DT));
        pArray->len += pInsertDatasLen;
    }
    
    void arrayDelFirstData(Array * pArray, DT data)
    {
        if (NULL == pArray)
        {
            printf("数组不存在! 
    ");
            abort();
        }
        if (NULL == pArray->pStart)
        {
            printf("数组无初始化数据! 
    ");
            abort();
        }
        if (0 == pArray->len)
        {
            printf("数组没有数据! 
    ");
            abort();
        }
        DT * pFindData = arrayFindFirstData(pArray, data);
        if (NULL == pFindData)
        {
            printf("没有找到待删除数据! 
    ");
            abort();
        }
        int relPos = pFindData - pArray->pStart;
        for (int i = relPos; i < pArray->len - 1; ++i)
        {
            *(pArray->pStart + i) = *(pArray->pStart + i + 1);
        }
        --pArray->len;
        pArray->pStart = (DT *)realloc(pArray->pStart, pArray->len * sizeof(DT));
    }
    
    void arrayDelAllData(Array * pArray, DT data)
    {
        if (NULL == pArray)
        {
            printf("数组不存在! 
    ");
            abort();
        }
        if (NULL == pArray->pStart)
        {
            printf("数组无初始化数据! 
    ");
            abort();
        }
        if (0 == pArray->len)
        {
            printf("数组没有数据! 
    ");
            abort();
        }
        for (DT * p = arrayFindFirstData(pArray, data); NULL != p; p = arrayFindFirstData(pArray, data))
        {
            arrayDelFirstData(pArray, data);
        }
    }
    
    void arrayUpdateFirstData(Array * pArray, DT oldData, DT newData)
    {
        if (NULL == pArray)
        {
            printf("数组不存在! 
    ");
            abort();
        }
        if (NULL == pArray->pStart)
        {
            printf("数组未初始化数据! 
    ");
            abort();
        }
        if (0 == pArray->len)
        {
            printf("数组没有数据! 
    ");
            abort();
        }
        DT * pFindData = arrayFindFirstData(pArray, oldData);
        if (NULL == pFindData)
        {
            printf("未能找到待替换的数据! 
    ");
            abort();
        }
        *pFindData = newData;
    }
    
    void arrayUpdateAllData(Array * pArray, DT oldData, DT newData)
    {
        if (NULL == pArray)
        {
            printf("数组不存在! 
    ");
            abort();
        }
        if (NULL == pArray->pStart)
        {
            printf("数组未初始化数据! 
    ");
            abort();
        }
        if (0 == pArray->len)
        {
            printf("数组没有数据! 
    ");
            abort();
        }
        for (DT * p = arrayFindFirstData(pArray, oldData); NULL != p; p = arrayFindFirstData(pArray, oldData))
        {
            arrayUpdateFirstData(pArray, oldData, newData);
        }
    }
    ///main.c
    #include "MyArray.h"
    #include <stdlib.h>
    
    int main(void)
    {
        Array array;
        //initArrayWithData(&array, 1);
        int intArr1[5] = { 1, 2, 3, 4, 5 };
        initArrayWithDatas(&array, intArr1, 5);
        //arrayAddData(&array, 6);
        int intArr2[3] = { 6, 7, 8 };
        arrayAddDatas(&array, intArr2, 3);
        //arrayInsertData(&array, 6, 7);
        int intArr3[3] = { 8, 9, 10 };
        arrayInsertDatas(&array, 6, intArr3, 3);
        //arrayDelFirstData(&array, 6);
        //arrayDelAllData(&array, 8);
        //arrayUpdateFirstData(&array, 8, 11);
        arrayUpdateAllData(&array, 8 ,11);
        showArray(&array);
    
    
        system("pause");
    }
    
    
    //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("
    %p,%d", res.ppstart[i], *res.ppstart[i]);
    //  }
    //  
    //  show(&mydata);
    //
    //
    //
    //
    //
    //
    //  system("pause");
    //}

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

    ///MyArray.h
    #pragma once
    
    //#define DT int
    #define DT char *
    
    typedef struct
    {
        DT * pStart;
        int len;
        int sortState;
    }Array;
    
    typedef struct
    {
        DT ** ppStart;
        int len;
    }ResArray;
    
    typedef int (* EqualFunP)(DT *, DT *);
    typedef void(*PrintFunP)(Array * pArray);
    
    
    void initArray(Array * pArray);
    void initArrayWithData(Array * pArray, DT data);
    void initArrayWithDatas(Array * pArray, DT * pDatas, int pDataLen);
    
    int isValidA(Array * pArray);
    int isValidB(Array * pArray);
    int isValidC(Array * pArray);
    int isValidD(Array * pArray);
    int isValidE(Array * pArray);
    
    void printInt(Array * pArray);
    void printString(Array * pArray);
    
    void showArray(Array * pArray, PrintFunP funP);
    
    void arrayAddData(Array * pArray, DT data);
    void arrayAddDatas(Array * pArray, DT * pDatas, int pDatasLen);
    
    int intIsEqual(int * pDataA, int * pDataB);
    int strIsEqual(char ** pDataA, char ** pDataB);
    DT *    arraySelectFirstData(EqualFunP funP, Array * pArray, DT data);
    ResArray arraySelectAllDatas(EqualFunP funP, Array * pArray, DT data);
    
    void arrayInsertData(Array * pArray, DT data, DT insertData);
    void arrayInsertDatas(Array * pArray, DT data, DT * pInsertDatas, int pInsertDatasLen);
    
    void arrayDeleteFirstData(Array * pArray, DT data);
    void arrayDeleteAllDatas(Array * pArray, DT data);
    
    void arrayUpdateFirstData(Array * pArray, DT oldData, DT newData);
    void arrayUpdateAllDatas(Array * pArray, DT oldData, DT newData);
    //数组库拓展功能:
    //  1.排序:
    //      冒泡->选择->插入->堆排序->高速(单线程)->希尔(多线程)
    //  2.查找:
    //      二分->插值->多线程
    //  3.可变參数
    //      可变參数拓展
    ///MyArray.c
    #include "MyArray.h"
    #include <stdlib.h>
    #include <stdio.h>
    #include <memory.h>
    #include <string.h>
    
    void initArray(Array * pArray)
    {
        if (NULL == pArray)
        {
            printf("数组不存在! 
    ");
            abort();
        }
        (*pArray).pStart = NULL;
        (*pArray).len = 0;
        (*pArray).sortState = 0;
    }
    
    void initArrayWithData(Array * pArray, DT data)
    {
        if (NULL == pArray)
        {
            printf("数组不存在! 
    ");
            abort();
        }
        (*pArray).pStart = (DT *)malloc(sizeof(DT));
        *((*pArray).pStart) = data;
        (*pArray).len = 1;
        (*pArray).sortState = 0;
    }
    
    void initArrayWithDatas(Array * pArray, DT * pDatas, int pDatasLen)
    {
        if (NULL == pArray)
        {
            printf("数组不存在! 
    ");
            abort();
        }
        (*pArray).pStart = (DT *)malloc(pDatasLen*sizeof(DT));
        memcpy((*pArray).pStart, pDatas, pDatasLen * sizeof(DT));
        (*pArray).len = pDatasLen;
        (*pArray).sortState = 0;
    }
    
    int isValidA(Array * pArray)
    {
        if (NULL == pArray)
        {
            printf("数组不存在! 
    ");
            return 0;
        }
        return 1;
    }
    
    int isValidB(Array * pArray)
    {
        if (NULL == (*pArray).pStart)
        {
            printf("数组无初始化数据! 
    ");
            return 0;
        }
        return 1;
    }
    
    int isValidC(Array * pArray)
    {
        if (0 == (*pArray).len)
        {
            printf("数组没有数据! 
    ");
            return 0;
        }
        return 1;
    }
    
    int isValidD(Array * pArray)
    {
        if (NULL == pArray)
        {
            printf("数组不存在! 
    ");
            return 0;
        }
        if (NULL == (*pArray).pStart)
        {
            printf("数组无初始化数据! 
    ");
            return 0;
        }
        return 1;
    }
    
    int isValidE(Array * pArray)
    {
        if (NULL == pArray)
        {
            printf("数组不存在! 
    ");
            return 0;
        }
        if (NULL == (*pArray).pStart)
        {
            printf("数组无初始化数据! 
    ");
            return 0;
        }
        return 1;
    }
    
    void printString(Array * pArray)
    {
        for (int i = 0; i < (*pArray).len; ++i)
        {
            printf("%s 
    ", *((*pArray).pStart + i));
        }
    }
    
    void printInt(Array * pArray)
    {
        for (int i = 0; i < (*pArray).len; ++i)
        {
            printf("%d 
    ", *((*pArray).pStart + i));
        }
    }
    
    void showArray(Array * pArray, PrintFunP funP)
    {
        if (!isValidE(pArray))
        {
            abort();
        }
        funP(pArray);
    }
    
    void arrayAddData(Array * pArray, DT data)
    {
        if (!isValidA(pArray))
        {
            abort();
        }
        if (!isValidB(pArray))
        {
            initArrayWithData(pArray, data);
        }
        else
        {
            (*pArray).pStart = (DT *)realloc((*pArray).pStart, ((*pArray).len + 1) * sizeof(DT));
            *((*pArray).pStart + (*pArray).len) = data;
            ++(*pArray).len;
        }
    }
    
    void arrayAddDatas(Array * pArray, DT * pDatas, int pDatasLen)
    {
        if (!isValidA(pArray))
        {
            abort();
        }
        if (!isValidB(pArray))
        {
            initArrayWithDatas(pArray, pDatas, pDatasLen);
        }
        else
        {
            (*pArray).pStart = (DT *)realloc((*pArray).pStart, ((*pArray).len + pDatasLen) * sizeof(DT));
            memcpy((*pArray).pStart + (*pArray).len, pDatas, pDatasLen * sizeof(DT));
            (*pArray).len += pDatasLen;
        }
    }
    
    int intIsEqual(int * pDataA, int * pDataB)
    {
        return *pDataA == *pDataB;
    }
    
    int strIsEqual(char ** pDataA, char ** pDataB)
    {
        return !strcmp(*pDataA, *pDataB);
    }
    
    DT * arraySelectFirstData(EqualFunP funP, Array * pArray, DT data)
    {
        if (!isValidE(pArray))
        {
            abort();
        }
        for (int i = 0; i < (*pArray).len; ++i)
        { 
            if (funP(&data, (*pArray).pStart + i))
            {
                return (*pArray).pStart + i;
            }
        }
        return NULL;
    }
    
    ResArray arraySelectAllDatas(EqualFunP funP, Array * pArray, DT data)
    {
        if (!isValidE(pArray))
        {
            abort();
        }
        ResArray resArr;
        resArr.len = 0;
        for (int i = 0; i < (*pArray).len; ++i)
        {
            if (funP(&data, (*pArray).pStart + i))
            {
                ++resArr.len;
            }
        }
        resArr.ppStart = (DT **)malloc(resArr.len * sizeof(DT *));
        int j = 0;
        for (int i = 0; i < (*pArray).len; ++i)
        {
            if (funP(&data, (*pArray).pStart + i))
            {
                *(resArr.ppStart + j++) = (*pArray).pStart + i;
            }
        }
        return resArr;
    }
    
    void arrayInsertData(Array * pArray, DT data, DT insertData)
    {
        if (!isValidE(pArray))
        {
            abort();
        }
        DT * pFindData = arraySelectFirstData(strIsEqual, pArray, data);
        if (NULL == pFindData)
        {
            printf("没有找到待插入的位置! 
    ");
            abort();
        }
        int relPos = pFindData - (*pArray).pStart;
        (*pArray).pStart = (DT *)realloc((*pArray).pStart, ((*pArray).len + 1)*sizeof(DT));
        for (int i = (*pArray).len; i >= relPos; --i)
        {
            *((*pArray).pStart + i + 1) = *((*pArray).pStart + i);
        }
        *((*pArray).pStart + relPos) = insertData;
        ++(*pArray).len;
    }
    
    void arrayInsertDatas(Array * pArray, DT data, DT * pInsertDatas, int pInsertDatasLen)
    {
        if (!isValidE(pArray))
        {
            abort();
        }
        DT * pFindData = arraySelectFirstData(strIsEqual, pArray, data);
        if (NULL == pFindData)
        {
            printf("没有找到待插入的位置! 
    ");
            abort();
        }
        int relPos = pFindData - (*pArray).pStart;
        (*pArray).pStart = (DT *)realloc((*pArray).pStart, ((*pArray).len + pInsertDatasLen) * sizeof(DT));
        for (int i = (*pArray).len; i >= relPos; --i)
        {
            *((*pArray).pStart + i + pInsertDatasLen) = *((*pArray).pStart + i);
        }
        memcpy((*pArray).pStart + relPos, pInsertDatas, pInsertDatasLen * sizeof(DT));
        (*pArray).len += pInsertDatasLen;
    }
    
    void arrayDeleteFirstData(Array * pArray, DT data)
    {
        if (!isValidE(pArray))
        {
            abort();
        }
        DT * pFindData = arraySelectFirstData(strIsEqual, pArray, data);
        if (NULL == pFindData)
        {
            printf("没有找到待删除的位置! 
    ");
            abort();
        }
        int relPos = pFindData - (*pArray).pStart;
        for (int i = relPos; i < (*pArray).len - 1; ++i)
        {
            *((*pArray).pStart + i) = *((*pArray).pStart + i + 1);
        }
        --(*pArray).len;
        (*pArray).pStart = (DT *)realloc((*pArray).pStart, (*pArray).len * sizeof(DT));
    }
    
    void arrayDeleteAllDatas(Array * pArray, DT data)
    {
        if (!isValidE(pArray))
        {
            abort();
        }
        for (DT * p = arraySelectFirstData(strIsEqual, pArray, data); NULL != p; p = arraySelectFirstData(strIsEqual, pArray, data))
        {
            arrayDeleteFirstData(pArray, data);
        }
    }
    
    void arrayUpdateFirstData(Array * pArray, DT oldData, DT newData)
    {
        if (!isValidE(pArray))
        {
            abort();
        }
        DT * pFindData = arraySelectFirstData(strIsEqual, pArray, oldData);
        if (NULL == pFindData)
        {
            printf("未能找到待更新的位置! 
    ");
            abort();
        }
        *pFindData = newData;
    }
    
    void arrayUpdateAllDatas(Array * pArray, DT oldData, DT newData)
    {
        if (!isValidE(pArray))
        {
            abort();
        }
        for (DT * p = arraySelectFirstData(strIsEqual, pArray, oldData); NULL != p; p = arraySelectFirstData(strIsEqual, pArray, oldData))
        {
            *p = newData;
        }
    }
    ///main.c
    #include "MyArray.h"
    #include <stdlib.h>
    #include <stdio.h>
    
    int main01(void)
    {
        Array array;
        initArray(&array);
    
        //initArrayWithData(&array, 1);
        //int intArr[5] = { 1, 2, 3, 4, 5 };
        //initArrayWithDatas(&array, intArr, 5);
        //arrayAddData(&array, 6);
        //int intArr1[3] = { 7, 8, 9 };
        //arrayAddDatas(&array, intArr1, 3);
        //showArray(&array, printInt);
    
        initArrayWithData(&array, "123");
        char * pArr[5] = { "abc", "def", "ghi", "jkl", "jkl" };
        initArrayWithDatas(&array, pArr, 5);
        arrayAddData(&array, "opq");
        char * pArr1[3] = { "rst", "uvw", "xyz" };
        arrayAddDatas(&array, pArr1, 3);
        arrayInsertData(&array, "jkl", "AAA");
        char * pArr2[3] = { "BBB", "CCC", "DDD" };
        arrayInsertDatas(&array, "jkl", pArr2, 3);
        arrayDeleteFirstData(&array , "AAA");
        arrayDeleteAllDatas(&array, "jkl");
        arrayUpdateFirstData(&array, "BBB", "EEE");
        arrayUpdateAllDatas(&array, "CCC", "FFF");
        showArray(&array, printString);
        //printf("%s 
    ", *(arraySelectFirstData(strIsEqual, &array, "uvw")));              
        //ResArray resArr = arraySelectAllDatas(strIsEqual, &array, "jkl");
        //for (int i = 0; i < resArr.len; ++i)
        //{
        //  printf("%s 
    ", **(resArr.ppStart + i));
        //}
    
        system("pause");
    }

    程序片段(03):String.h+Array.h+String.c+Array.c+main.c
    内容概要:库中库

    ///String.h
    #pragma once
    #include <stdlib.h>
    
    typedef struct
    {
        char * pAStr;
        int memLen;
    }AString;
    
    typedef struct
    {
        wchar_t * pWStr;
        int memLen;
    }WString;
    
    void setWStrLocale(char const * pStr);
    
    int strIsValidA(void * pStr);
    int strIsValidB(char chr, void * pStr);
    int strIsValidC(char chr, void * pStr);
    
    void initStr(char chr, void * pStr);
    void initStrWithStr(char chr, void * pStr, void const * pInitStr);
    void showStr(char chr, void * pStr);
    ///Array.h
    #pragma once
    
    typedef struct
    {
        int eleSize;
        int len;
        char eleType[12];
        void * pArr;
    }Array;
    
    void initArray(Array * pArr, char eleType[12], int eleSize);
    void initArrayWithDatas(Array * pArr, char eleType[12], int eleSize, void * pInitDatas, int pInitDatasLen);
    void showArray(Array * pArr, char eleType[12]);
    void arrayAddData(Array * pArr, void * pData);
    ///String.c
    #define _CRT_SECURE_NO_WARNINGS
    #include "String.h"
    #include <locale.h>
    #include <string.h>
    #include <stdio.h>
    
    void setWStrLocale(char const * pStr)
    {
        setlocale(LC_ALL, pStr);
    }
    
    int strIsValidA(void * pStr)
    {
        if (NULL == pStr)
        {
            printf("字符串不存在! 
    ");
            return 0;
        }
        return 1;
    }
    
    int strIsValidB(char chr, void * pStr)
    {
        if ('w' == chr)
        {
            WString * pAStr = pStr;
            if (NULL == (*pAStr).pWStr)
            {
                printf("宽字符串无初始化数据! 
    ");
                return 0;
            }
        }
        else
        {
            AString * pWStr = pStr; 
            if (NULL == (*pWStr).pAStr)
            {
                printf("宽字符串无初始化数据! 
    ");
                return 0;
            }
        }
        return 1;
    }         
    
    int strIsValidC(char chr, void * pStr)
    {
        if ('w' == chr)
        {
            WString * pWStr = pStr;
            if (0 == wcslen((*pWStr).pWStr))
            {
                printf("宽字符串没有数据! 
    ");
                return 0;
            }
        }
        else
        {
            AString * pAStr = pStr;
            if (0 == strlen((*pAStr).pAStr))
            {
                printf("窄字符串没有数据! 
    ");
                return 0;
            }
        }
        return 1;
    }
    
    void initStr(char chr, void * pStr)
    {
        if (!strIsValidA(pStr))
        {
            abort();
        }
        if ('w' == chr)
        {
            WString * pWStr = pStr;
            (*pWStr).pWStr = NULL;
            (*pWStr).memLen = 0;
        }
        else
        {
            AString * pAStr = pStr;
            (*pAStr).pAStr = NULL;
            (*pAStr).memLen = 0;
        }
    }
    
    void initStrWithStr(char chr, void * pStr, void * pInitStr)
    {
        if (!strIsValidA(pStr))
        {
            abort();
        }
        if ('w' == chr)
        {
            WString * pWStr = pStr;
            wchar_t * pInitWStr = pInitStr;
            int pWStrLen = wcslen(pInitWStr) + 1;
            (*pWStr).pWStr = (wchar_t *)malloc(pWStrLen * 2);
            wcscpy((*pWStr).pWStr, pInitWStr);
            (*pWStr).memLen = pWStrLen;
        }
        else
        {
            AString * pAStr = pStr;
            char * pInitAStr = pInitStr;
            int pAStrLen = strlen(pInitAStr) + 1;
            (*pAStr).pAStr = (char *)malloc(pAStrLen * sizeof(char));
            strcpy((*pAStr).pAStr, pInitAStr);
            (*pAStr).memLen = pAStrLen;
        }
    }
    
    void showStr(char chr, void const * pStr)
    {
        if (!strIsValidA(pStr) || !strIsValidB(chr, pStr) || !strIsValidC(chr ,pStr))
        {
            abort();
        }
        if ('w' == chr)
        {
            WString * pWStr = pStr;
            wprintf(L"%ls 
    ", (*pWStr).pWStr);
        }
        else
        {
            AString * pAStr = pStr;
            printf("%s 
    ", (*pAStr).pAStr);
        }
    }
    ///Array.c
    #define _CRT_SECURE_NO_WARNINGS
    #include "Array.h"
    #include "String.h"
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    void initArray(Array * pArr, char eleType[12], int eleSize)
    {
        pArr->pArr = NULL;
        pArr->len = 0;
        pArr->eleSize = eleSize;
        strcpy(pArr->eleType, eleType);
    }
    
    void initArrayWithDatas(Array * pArr, char eleType[12], int eleSize, void * pInitDatas, int pInitDatasLen)
    {
        strcpy((*pArr).eleType, eleType);
        (*pArr).eleSize = eleSize;
        if (!strcmp("WString", eleType))
        {
            WString * pWStr = pInitDatas;
            (*pArr).pArr = (WString *)malloc(pInitDatasLen * sizeof(WString));
            memcpy((*pArr).pArr, pWStr, pInitDatasLen * sizeof(WString));
            (*pArr).len = pInitDatasLen;
        }
        else if (!strcmp("AString", eleType))
        {
            AString * pAStr = pInitDatas;
            (*pArr).pArr = (AString *)malloc(pInitDatasLen * sizeof(AString));
            memcpy((*pArr).pArr, pAStr, pInitDatasLen * sizeof(AString));
            (*pArr).len = pInitDatasLen;
        }
    }
    
    void showArray(Array * pArr, char eleType[12])
    {
        if (!strcmp("WString", eleType))
        {
            WString * pWStr = (*pArr).pArr;
            for (int i = 0; i < (*pArr).len; ++i)
            {
                wprintf(L"%ls 
    ", (*(pWStr + i)).pWStr);
            }
            printf("
    ");
        }
        else if (!strcmp("AString", eleType))
        {
            AString * pAStr = (*pArr).pArr;
            for (int i = 0; i < (*pArr).len; ++i)
            {
                printf("%s 
    ", (*(pAStr + i)).pAStr);
            }
            printf("
    ");
        }
    }
    
    void arrayAddData(Array * pArr, void * pData)
    {
        if (!strcmp("WString", (*pArr).eleType))
        {
            (*pArr).pArr = (WString *)realloc((*pArr).pArr, ((*pArr).len + 1)*sizeof(WString));
            WString * pWStr = pData;
            WString * pTmp = (*pArr).pArr;
            *(pTmp + (*pArr).len) = *pWStr;
            ++(*pArr).len;
        }
        else if (!strcmp("AString", (*pArr).eleType))
        {
            (*pArr).pArr = (AString *)realloc((*pArr).pArr, ((*pArr).len + 1)* sizeof(AString));
            AString * pAStr = pData;
            AString * pTmp = (*pArr).pArr;
            *(pTmp + (*pArr).len) = *pAStr;
            ++(*pArr).len;
        }
    }
    ///main.c
    #include "String.h"
    #include "Array.h"
    
    int main01(void)
    {
        setWStrLocale("zh-CN");
        AString  astring;
        WString wstring;
        //initStr('a', &pAStr);
        //initStr('w', &pWStr);
        initStrWithStr('a', &astring, "calc");
        initStrWithStr('w', &wstring ,L"你猜猜");
        showStr('a', &astring);
        showStr('w', &wstring);
    
        system("pause");
    }

    程序片段(04):Row.h+Array.h+init.h+Row.c+Array.c+init.c
    内容概要:数据的管理-增删查改线性存储

    ///Row.h
    #pragma once
    
    typedef struct
    {
        long long QQ;
        char * pStr;
        unsigned char len;
    }Row;
    
    int rowIsValidA(Row * pRow);
    int rowIsValidB(Row * pRow);
    int rowIsValidC(Row * pRow);
    void initRowWithData(Row * pRow, long long QQ, char * pStr);
    void initRowWithPass(Row * pRow, char * pPass);
    void initRowWithStr(Row * pRow, char * pStr);
    void showRow(Row * pRow);
    void rowUpdateRow(Row * pOldRow, Row * pNewRow);
    void rowUpdateRowDeep(Row * pOldRow, Row * pNewRow);
    ///Array.h
    #pragma once
    
    #include "Row.h"
    
    typedef struct
    {
        Row * pRow;
        int len;
    }Array;
    
    void initArray(Array * pArr);
    void initArrayWithStr(Array * pArr, char * pStr);
    
    void showArray(Array * pArr);
    
    void arraySelectDataByFirstQQ(Array * pArr, long long QQ);
    void arraySelectDataByAllQQ(Array * pArr, long long QQ);
    void arraySelectDataByFirstPass(Array * pArr, char * pPass);
    void arraySelectDataByAllPass(Array * pArr, char  * pPass);
    
    void arrayAddData(Array * pArr, Row * pRow);
    void arrayInsertDataByFirstQQ(Array * pArr, long long QQ, Row * pRow);
    void arrayInsertDataByAllQQ(Array * pArr, long long QQ, Row * pRow);
    void arrayInsertDataByFirstPass(Array * pArr, char * pPass, Row * pRow);
    void arrayInsertDataByAllPass(Array * pArr, char * pPass, Row * pRow);
    
    void arrayDeleteAllDatas(Array * pArr);
    void arrayDeleteDataByFirstQQ(Array * pArr, long long QQ);
    void arrayDeleteDataByAllQQ(Array * pArr, long long QQ);
    void arrayDeleteDataByFirstPass(Array * pArr, char * pPass);
    void arrayDeleteDataByAllPass(Array * pArr, char * pPass);
    
    void arrayUpdateDataByFirstQQ(Array * pArr, long long QQ, Row * pRow);
    void arrayUpdateDataByAllQQ(Array * pArr, long long QQ, Row * pRow);
    void arrayUpdateDataByFirstPass(Array * pArr, char * pPass, Row * pRow);
    void arrayUpdateDataByAllPass(Array * pArr, char * pPass, Row * pRow);
    
    int comByQQ(Row * pQQA, Row * pQQB);
    int comByPass(Row * pPassA, Row * pPassB);
    
    void sortByQQ(Array * pArr);
    void sortByPass(Array * pArr);
    ///init.h
    #pragma once
    
    //01.全局变量位于静态区,与程序共存亡:
    //  函数和全局变量的声明能够有多个;
    //  可是定义仅仅能有一个
    char str[1024];
    int num;
    
    int countRow(char * pStr);
    ///Row.c
    #include "Row.h"
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int rowIsValidA(Row * pRow)
    {
        if (NULL == pRow)
        {
            printf("行结构不存在! 
    ");
            return 0;
        }
        return 1;
    }
    
    int rowIsValidB(Row * pRow)
    {
        if (NULL == (*pRow).pStr)
        {
            printf("行结构无初始数据行! 
    ");
            return 0;
        }
        return 1;
    }
    
    int rowIsValidC(Row * pRow)
    {
        if (0 == (*pRow).len)
        {
            printf("行结构无数据行! 
    ");
            return 0;
        }
        return 1;
    }
    
    void initRowWithData(Row * pRow, long long QQ, char * pStr)
    {
        if (!rowIsValidA(pRow))
            abort();
        (*pRow).QQ = QQ;
        int len = strlen(pStr);
        (*pRow).pStr = (char *)malloc((len + 1)*sizeof(char));
        strcpy((*pRow).pStr, pStr);
        (*pRow).len = len + 1;
    }
    
    void initRowWithPass(Row * pRow, char * pPass)
    {
        if (!rowIsValidA(pRow))
            abort();
        (*pRow).QQ = 77025077;
        int initStrLen = strlen(pPass) + 1;
        (*pRow).pStr = (char *)malloc(initStrLen * sizeof(char));
        strcpy((*pRow).pStr, pPass);
        (*pRow).len = initStrLen;
    }
    
    void initRowWithStr(Row * pRow, char * pInitStr)
    {
        if (!rowIsValidA(pRow))
            abort();
        char * p = strstr(pInitStr, "----");
        *p = '';
        sscanf(p, "%lld", &(*pRow).QQ);
        int pInitStrLen = strlen(p + 4);
        (*pRow).pStr = (char *)malloc((pInitStrLen + 1) * sizeof(char));
        strcpy((*pRow).pStr, pInitStr);
        (*pRow).len = pInitStrLen + 1;
    }
    
    void showRow(Row * pRow)
    {
        if (!rowIsValidA(pRow) || !rowIsValidB(pRow) || !rowIsValidC(pRow))
            abort();
        printf("%lld<--->%s 
    ", (*pRow).QQ, (*pRow).pStr);
    }
    
    void rowUpdateRow(Row * pOldRow, Row * pNewRow)
    {
        if (!rowIsValidA(pOldRow) || !rowIsValidA(pNewRow))
            abort();
        *pOldRow = *pNewRow;
    }
    
    void rowUpdateRowDeep(Row * pOldRow, Row * pNewRow)
    {
        if (!rowIsValidA(pOldRow) || !rowIsValidA(pNewRow))
            abort();
        *pOldRow = *pNewRow;
        (*pOldRow).pStr = (char *)malloc((strlen((*pNewRow).len + 1) * sizeof(char)));
        strcpy((*pOldRow).pStr, (*pNewRow).pStr);
    }
    ///Array.c
    #include "Array.h"
    #include <stdlib.h>
    #include <string.h>
    #include "Row.h"
    #include "init.h"
    
    int arrIsValidA(Array * pArr)
    {
        if (NULL == pArr)
        {
            printf("数组不存在! 
    ");
            return 0;
        }
        return 1;
    }
    
    int arrIsValidB(Array * pArr)
    {
        if (NULL == (*pArr).pRow)
        {
            printf("数组未初始化数据! 
    ");
            return 0;
        }
        return 1;
    }
    
    int arrIsValidC(Array * pArr)
    {
        if (0 == (*pArr).len)
        {
            printf("数组无数据! 
    ");
            return 0;
        }
        return 1;
    }
    
    void initArray(Array * pArr)
    {
        if (!arrIsValidA(pArr))
        {
            abort();
        }
        (*pArr).pRow = NULL;
        (*pArr).len = 0;
    }
    
    void initArrayWithStr(Array * pArr, char * pStr)
    {
        if (!arrIsValidA(pArr))
        {
            abort();
        }
        int rowNum = countRow(pStr);
        (*pArr).pRow = (Row *)malloc(rowNum * sizeof(Row));
        (*pArr).len = rowNum;
        int pStrLen = strlen(pStr);
        for (char * p = pStr; p < pStr + pStrLen; p += strlen(p) + 1)
        {//字符串预处理
            if (' ' == *p)
            {
                *p = '';
            }
        }
        int i = 0;
        for (char * p = pStr; p < pStr + pStrLen; p += strlen(p) + 1)
        {
            char * pTmp = (char *)malloc((strlen(p) + 1) * sizeof(char));
            strcpy(pTmp, p);
            initRowWithStr((*pArr).pRow + i++, pTmp);
        }
    }
    
    void showArray(Array * pArr)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        for (int i = 0; i < (*pArr).len; ++i)
        {
            showRow((*pArr).pRow + i);
        }
    }
    
    void arraySelectDataByFirstQQ(Array * pArr, long long QQ)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        for (int i = 0; i < (*pArr).len; ++i)
        {
            if (QQ == (*((*pArr).pRow + i)).QQ)
            {
                printf("找到了首个QQ:%lld, pass:%s 
    ", QQ, (*((*pArr).pRow + i)).pStr);
                printf("数据查找结束! 
    ");
                return;
            }
        }                    
    }
    
    void arraySelectDataByAllQQ(Array * pArr, long long QQ)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        for (int i = 0; i < (*pArr).len; ++i)
        {
            if (QQ == (*((*pArr).pRow + i)).QQ)
            {
                printf("QQ:%lld, pass:%s 
    ", QQ, (*((*pArr).pRow + i)).pStr);
            }
        }
        printf("数据查找结束! 
    ");
    }
    
    void arraySelectDataByFirstPass(Array * pArr, char * pPass)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        for (int i = 0; i < (*pArr).len; ++i)
        { 
            if (NULL != strstr((*((*pArr).pRow + i)).pStr, pPass))
            {
                printf("首次找到了QQ:%lld, pass:%s 
    ", (*((*pArr).pRow + i)).QQ, (*((*pArr).pRow + i)).pStr);
                printf("数据查找结束! 
    ");
                return;
            }
        }
    }
    
    void arraySelectDataByAllPass(Array * pArr, char * pPass)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        for (int i = 0; i < (*pArr).len; ++i)
        {
            if (NULL != strstr((*((*pArr).pRow + i)).pStr, pPass))
            {
                printf("QQ:%lld, pass:%s 
    ", (*((*pArr).pRow + i)).QQ, (*((*pArr).pRow + i)).pStr);
            }
        }
        printf("数据查询完成! 
    ");
    }
    
    void arrayAddData(Array * pArr, Row * pData)
    {
        if (!arrIsValidA(pArr))
            abort();
        (*pArr).pRow = (Row *)realloc((*pArr).pRow, ((*pArr).len + 1) * sizeof(Row));
        *((*pArr).pRow + (*pArr).len) = *pData;
        ++(*pArr).len;
    }
    
    void arrayInsertDataByFirstQQ(Array * pArr, long long QQ, Row * pRow)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidCI(pArr))
            abort();
        int relPos = -1;
        for (int i = 0; i < (*pArr).len; ++i)
        {
            if (QQ == (*((*pArr).pRow + i)).QQ)
            {
                relPos = i;
                printf("找到首个QQ:%lld, pass:%s 
    ", (*(QQ, (*pArr).pRow + i)).pStr);
                break;
            }
        }
        if (-1 == relPos)
        {
            printf("没有找到将要插入的位置! 
    ");
            abort();
        }
        (*pArr).pRow = (Row *)realloc((*pArr).pRow, ((*pArr).len + 1) * sizeof(Row));
        for (int i = (*pArr).len; i >= relPos; --i)
        {
            *((*pArr).pRow + i + 1) = *((*pArr).pRow + i);
        }
        *((*pArr).pRow + relPos) = *pRow;
        ++(*pArr).len;
    }
    
    void arrayInsertDataByAllQQ(Array * pArr, long long QQ, Row * pRow)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        int i = 0;
        int j = 0;
        while (1)
        {
            if (i >= (*pArr).len || j >= (*pArr).len)
            {
                break;
            }
            *((*pArr).pRow + i) = *((*pArr).pRow + j);
            if (QQ == ((*pArr).pRow + i))
            {
                (*pArr).pRow = (Row *)realloc((*pArr).pRow, ((*pArr).len + 1) * sizeof(Row));
                for (int k = (*pArr).len - 1; k >= i; --k)
                {
                    *((*pArr).pRow + k + 1) = *((*pArr).pRow + k);
                }
                *((*pArr).pRow + i) = *pRow;
                ++(*pArr).len;
                i += 2;//跳到下一起点
                j += 2;
            }
            else
            {
                ++j;
                ++i;
            }
        }
    }
    
    void arrayInsertDataByFirstPass(Array * pArr, char * pPass, Row * pRow)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        int relPos = -1;
        for (int i = 0; i < (*pArr).len; ++i)
        {
            if (!strcmp(pPass, (*((*pArr).pRow + i)).pStr))
            {
                relPos = i;
                break;
            }
        }
        if (-1 == relPos)
        {
            abort();
        }
        (*pArr).pRow = (Row *)realloc((*pArr).pRow, ((*pArr).len + 1) * sizeof(Row));
        for (int i = (*pArr).len; i >= relPos; --i)
        {
            *((*pArr).pRow + i + 1) = *((*pArr).pRow + i);
        }
        *((*pArr).pRow + relPos) = *pRow;
        ++(*pArr).len;
    }
    
    void arrayInsertDataByAllPass(Array * pArr, char * pPass, Row * pRow)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        int i = 0, j = 0;
        while (1)
        {
            if (i >= (*pArr).len || j >= (*pArr).len)
            {
                return;
            }
            *((*pArr).pRow + i) = *((*pArr).pRow + j);
            if (!strcmp((*((*pArr).pRow + i)).pStr, pPass))
            {
                (*pArr).pRow = (Row *)realloc((*pArr).pRow, ((*pArr).len + 1) * sizeof(Row));
                for (int k = (*pArr).len; k >= i; --k)
                {
                    *((*pArr).pRow + k + 1) = *((*pArr).pRow + k);
                }
                *((*pArr).pRow + i) = *pRow;
                j += 2;
                i += 2;
            }
            else
            {
                ++j;
                ++i;
            }
        }               
    }
    
    void arrayDeleteAllDatas(Array * pArr)
    {
    if (!arrIsValidA(pArr))
    abort();
    for (int i = 0; i < (*pArr).len; ++i)
    {
        free((*((*pArr).pRow + i)).pStr);
    }
    free((*pArr).pRow);
    (*pArr).pRow = NULL;
    (*pArr).len = 0;
    }
    
    void arrayDeleteDataByFirstQQ(Array * pArr, long long QQ)
    {
        if (!isValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        int relPos = -1;
        for (int i = 0; i < (*pArr).len; ++i)
        {
            if (QQ == (*((*pArr).pRow + i)).QQ)
            {
                relPos = i;
                break;
            }
        }
        if (-1 == relPos)
        {
            abort();
        }
        free((*((*pArr).pRow + relPos)).pStr);
        for (int i = relPos; i < (*pArr).len - 1; ++i)
        {
            *((*pArr).pRow + i) = *((*pArr).pRow + i + 1);
        }
        --(*pArr).len;
        (*pArr).pRow = (Row *)realloc((*pArr).pRow, (*pArr).len * sizeof(Row));
    }
    
    void arrayDeleteDataByAllQQ(Array * pArr, long long QQ)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        int i = 0, j = 0, k = 0;;
        while (1)
        {
            if (i >= (*pArr).len || j >= (*pArr).len)
                break;
            *((*pArr).pRow + i) = *((*pArr).pRow + j);
            if (QQ == (*((*pArr).pRow + i)).QQ)
            {
                free((*((*pArr).pRow + i)).pStr);
                ++j;
                ++k;
            }
            else
            {
                ++i;
                ++j;
            }
        }
        (*pArr).len -= k;
        (*pArr).pRow = (Row *)realloc((*pArr).pRow, (*pArr).len * sizeof(Row));
    }
    
    void arrayDeleteDataByFirstPass(Array * pArr, char * pPass)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        int relPos = -1;
        for (int i = 0; i < (*pArr).len; ++i)
        {
            if (!strcmp(pPass, (*((*pArr).pRow + i)).pStr))
            {
                relPos = i;
                break;
            }
        }
        if (-1 == relPos)
        {
            abort();
        }
        free((*((*pArr).pRow + relPos)).pStr);
        for (int i = relPos; i < (*pArr).len - 1; ++i)
        {
            *((*pArr).pRow + i) = *((*pArr).pRow + i + 1);
        }
        --(*pArr).len;
        (*pArr).pRow = (Row *)realloc((*pArr).pRow, (*pArr).len * sizeof(Row));
    }
    
    void arrayDeleteDataByAllPass(Array * pArr, char * pPass)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        int i = 0, j = 0, k = 0;
        while (1)
        {
            if (i >= (*pArr).len || j >= (*pArr).len)
                break;;
            *((*pArr).pRow + i) = *((*pArr).pRow + j);
            if (!strcmp(pPass, (*pArr).pRow + i))
            {
                free((*((*pArr).pRow + i)).pStr);
                ++k;
                ++j;
            }
            else
            {
                ++i;
                ++j;
            }
        }
        (*pArr).len -= k;
        (*pArr).pRow = (Row *)realloc((*pArr).pRow, (*pArr).len * sizeof(Row));
    }
    
    void arrayUpdateDataByFirstQQ(Array * pArr, long long QQ, Row * pRow)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        for (int i = 0; i < (*pArr).len; ++i)
        {
            if (QQ == (*((*pArr).pRow + i)).QQ)
            {
                rowUpdateRow((*pArr).pRow + i, pRow);
                break;
            }
        }
    }
    
    void arrayUpdateDataByAllQQ(Array * pArr, long long QQ, Row * pRow)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        for (int i = 0; i < (*pArr).len; ++i)
        {
            if(QQ == (*((*pArr).pRow + i)).QQ)
            {
                rowUpdateRow((*pArr).pRow + i, pRow);
            }
        }
    }
    
    void arrayUpdateDataByFirstPass(Array * pArr, char * pPass, Row * pRow)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        for (int i = 0; i < (*pArr).len; ++i)
        {
            if (!strcmp(pPass, (*((*pArr).pRow + i)).pStr))
            {
                rowUpdateRow((*pArr).pRow + i, pRow);
                break;
            }
        }
    }
    
    void arrayUpdateDataByAllPass(Array * pArr, char * pPass, Row * pRow)
    {
        if (!arrIsValidA(pArr) || !arrIsValidB(pArr) || !arrIsValidC(pArr))
            abort();
        for (int i = 0; i < (*pArr).len; ++i)
        {
            if (!strcmp(pPass, (*((*pArr).pRow + i)).pStr))
            {
                rowUpdateRow((*pArr).pRow + i, pRow);
            }
        }
    }
    
    int comByQQ(Row * pDataA, Row * pDataB)
    {
        if (!rowIsValidA(pDataA) || !rowIsValidA(pDataB))
            abort();
        if ((*pDataA).QQ < (*pDataB).QQ)
        {
            return -1;
        }
        else if ((*pDataA).QQ == (*pDataB).QQ)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
    
    int comByPass(Row * pDataA, Row * pDataB)
    {
        return strcmp((*pDataA).pStr, (*pDataB).pStr);
    }
    
    void sortByQQ(Array * pArr)
    {
        qsort((*pArr).pRow, (*pArr).len, sizeof(Row), comByQQ);
    }
    
    void sortByPass(Array * pArr)
    {
        qsort((*pArr).pRow, (*pArr).len, sizeof(Row), comByPass);
    }
    ///init.c
    #include "init.h"
    #include <string.h>
    
    char str[1024]= "521276402----hanlei@19940403 
        286738260----weipei559720 
        501223616----feng66532008 
        77025077----shuidongwo520 
        1340382355----huang.512yang. 
        1061817115----fz62wangyong1983 
        347232860----20080811 
        1028181591----7404850554 
        120539543----0.0123456789 
        754229005----460228214 
        819781633----zmt1993826 
        1319148052----ynu1500621032 
        904972448----zhouxiaowen.520 
        750134133----1292857988 
        77025077----320675 
        379644978----7758521tao 
        346083956----devl1017 
        77025077----5361a749 ";
    int num=0;//定义
    
    int countRow(char * pStr)
    {
        int rowNum = 0;
        for (char * p = strstr(pStr, "----"); NULL != p; p = strstr(p + 4, "----");)
        {
            ++rowNum;
        }
        return rowNum;
    }

    程序片段(05):main.c
    内容概要:劫持方法解决内存泄漏

    #include <stdio.h>
    #include <stdlib.h>
    
    //01.内存泄露:
    //  问题:手动开辟的堆内存空间没有进行及时的手动回收
    //  解决:劫持技术解决+堆内存操作函数包装(函数包装)
    //  原理:等同于引用计数的特点
    //02.防内存泄露原理:
    //  引用技术原理的使用
    typedef struct
    {//单内存块儿
        void * pStart;//首地址
        int memSize;//内存尺寸
    }Mem;
    
    typedef struct
    {//动态数组
        Mem * pMem;//首地址
        int memNum;//内存块数
    }MemArr;
    
    int i = 0;//全局变量-->静态区-->统计内存块儿数
    
    void * myMalloc(size_t size)
    {
        ++i;
        return malloc(size);
    }
    
    void myFree(void * mem)
    {
        free(mem);
        --i;
    }
    
    int main01(void)
    {
        void * pStart1 = myMalloc(14);
        void * pStart2 = myMalloc(14);
        void * pStart3 = myMalloc(14);
        myFree(pStart1);
        printf("%d 
    ", i);
    
        system("pause");
    }
  • 相关阅读:
    DedeCMS系统301重定向配置方法详解
    dedecms批量更新静态时提示:没有该栏目数据 可能缓存的解决方法
    修改织梦dedecms后台默认admin账号的方法
    织梦DeDeCMS动态热点文章排行调用方法
    文件与字符串处理时常见的问题(中文英文)
    基于OpenCV全景拼接(Python)SIFT/SURF
    解决关闭ssh后网页停止服务的方法,利用nohup
    解决宝塔面板没有命令行问题 && 查看宝塔面板项目环境
    python 正则表达式
    JAVAC 不是内部或外部命令
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7106660.html
Copyright © 2011-2022 走看看