Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5412032
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: C/C++

2009-12-05 19:11:44

/*
*    函数原形    char * GetInput(char* _pchInput, int _nLen, int _nShow);
*    表头文件    #include
*                        #include
*    函数描述    用户输入读取函数,从终端读取用户的输入。
*        参 数    _pchInput,存放读取的用户输入,要有足够的空间来容纳用户输入
*                        _nLen,限制用户输入的最大长度
*                        _nShow,标识是否回显,0--不回显, 1--回显输入
*     返回值    成功,返回 _pchInput 的字符串起始地址。     
*        作 者    武立强
*        时    间    2009-02-18
*        注    意    \b Backspace \n Newline \r Carriage return
*                        getch()从终端读取一个字符,不回显.
*/

char * GetInput(char* _pchInput, int _nLen, int _nShow)
{
    assert( NULL != _pchInput );

    int nCourrent = 0;
    char* pFirst = _pchInput;

    while(1)
    {
        *_pchInput = getch();
        
        if( '\n' == *_pchInput || '\r' == *_pchInput )
        {    
            *_pchInput = '\0';
            break;
        }
        else if( '\b' == *_pchInput && 0 == nCourrent )
        {
            continue;        
        }
        else if( '\b' == *_pchInput && 0 == _nShow )
        {
            _pchInput--;
            nCourrent--;
            continue;
        }
        else if( '\b' == *_pchInput )
        {
            _pchInput--;
            nCourrent--;
            printf("\b \b"); // 注意细节,中间有个空格

            continue;
        }
        else if( nCourrent >= _nLen )
        {
            continue;    
        }
    
        // 是否回显

        if( 0 != _nShow )
        {
            printf("%c", *_pchInput);
        }    
        _pchInput++;
        nCourrent++;
    }
    _pchInput = NULL;

    return pFirst;
}

/*
*    函数原形    char * StringFilter(char * pStr, const char ch);
*    表头文件    #include
*    函数描述    字符串过滤,直接修改原串! 将字符串 pStr 中所有的 ch字符过滤掉。
*                        用于对单个字符的过滤,效率很高。
*        参 数    pStr,要过滤的字符串。
*                        ch,要过滤掉的字符。
*     返回值    成功,返回过滤后字符串 pStr 的首地址。
*        作 者    武立强
*        时    间    2009-02-17
*        备 注    StringFilter(pStr, ' '); ---用于过滤所有空格,很经典!
*/

char * StringFilter(char * pStr, char ch)
{
    assert( NULL != pStr );
    
    char * pStrFirst = pStr;
    int nCount = 0;

    // 自己偶尔发现的一个算法,很经典!

    while( '\0' != *pStr )
    {
        if( ch == *pStr )
        {
            nCount++;
        }
        else
        {    
            *(pStr - nCount) = *pStr; // 向前移动

        }
        pStr++;
    }

    *(pStr - nCount) = '\0'; // 别忘了结束


    return pStrFirst;
}

/*
*    定义函数 char * StringReplace(char* _pDest, const char* _pSrc, const char* _pKey, const char* _pReplace);
*    表头文件 #include
*    函数描述    字符串替换函数, 在字符串 _pSrc 中查找 _pKey 串,并将其替换成 _pReplace 串, 将结果保存在_pDest中
*                        _pSrc 要查找的原串
*                        _pKey 要查找的关键串
*                        _pReplace 要替换的内容
*                        _pDest 保存处理后的字符串,要有足够的空间来容纳处理后的字符串
*                        
*     返回值     返回 _pDest    的字符串起始地址。
*        作 者 武立强
*        时    间    2009-02-08
*        注    意    就两种情况,--匹配成功,---匹配失败
*                        StringReplace(strDest, strSrc, " ", ""); ---实现过滤字符串空格的作用
*/

char * StringReplace(char* _pDest, const char* _pSrc, const char* _pKey, const char* _pReplace)
{
    assert( NULL != _pDest && NULL != _pSrc && NULL != _pKey && NULL != _pReplace );
    
    // const char * 不能通过指针改变所指向的字符串的值

    const char* pKeyFirst = NULL;
    const char* pFirstFind = NULL; // 标记找到的第一个字符的位置

    const char* pLastFind = NULL;    //标记找到的最后一个字符的位置

    const char* pReplaceFirst = NULL;
    char* pDestFirst = NULL;

    int nFirstFind = 0 ; //第一个字符是否找到, 0--没找到, 1--找到

    int nLastFind = 0;    // 最后一个字符

    
    pKeyFirst = _pKey;
    pReplaceFirst = _pReplace;
    pDestFirst = _pDest;

    while( '\0' != *_pSrc )
    {
        // 逻辑比较部分, 确保完全匹配查找关键串

        if( 0 == nFirstFind && *_pSrc == *_pKey )
        {
            nFirstFind = 1;
            pFirstFind = _pSrc;
            _pKey++;
        }
        // 匹配成功

        else if( 1 == nFirstFind && *_pSrc == *_pKey && '\0' == *(_pKey+1) )
        {
            nLastFind = 1;
            pLastFind = _pSrc;
            _pKey = pKeyFirst;
        }
        else if( 1 == nFirstFind && *_pSrc == *_pKey )
        {
            _pKey++;
        }    
        // 部分匹配,查找失败,要进行补救----- 将第一个字符移过去,从第二个字符开始从新匹配.

        else if( 1 == nFirstFind && *_pSrc != *_pKey )
        {    
            *_pDest++ = *pFirstFind++;
            _pSrc = pFirstFind;
            nFirstFind = 0;
            pFirstFind = NULL;
            _pKey = pKeyFirst;
        }
                
        // 找到,替换为目标串

        if( 1 == nFirstFind && 1 == nLastFind )
        {
            while( '\0' != *_pReplace )
            {
                *_pDest++ = *_pReplace++;
            }

            nFirstFind = 0;
            nLastFind = 0;
            _pReplace = pReplaceFirst;
            _pKey = pKeyFirst;
        }
        // 没找到

        else if( 0 == nFirstFind && 0 == nLastFind )
        {
            *_pDest = *_pSrc;
            _pDest++;
        }
        // 针对一个字符替换为另一个字符的情况

        else if( 1 == nFirstFind && '\0' == *(_pKey) )
        {
            while( '\0' != *_pReplace )
            {
                *_pDest++ = *_pReplace++;
            }

            nFirstFind = 0;
            nLastFind = 0;
            _pReplace = pReplaceFirst;
            _pKey = pKeyFirst;
        }
        // 最后一次循环了,还没完成---匹配失败

        else if( 1 == nFirstFind && 0 == nLastFind && '\0' == *(_pSrc + 1) )
        {
            for(int i=0; i<=(_pSrc - pFirstFind+1); i++ )
            {
                *_pDest++ = *pFirstFind++;
            }
            nFirstFind = 0;
            pFirstFind = NULL;
            _pKey = pKeyFirst;
        }
    
        _pSrc++;
    }
    *_pDest = '\0';

    return pDestFirst;
}

/*
*    定义函数 char * StringFind(char* _pSrc, const char* _pKey);
*    表头文件 #include
*    函数描述    从字符串 _PSrc 中查找 _Pkey, 如果找到,返回第一个找到的地址,
*                        如过没有找到,返回NULL。
*                        _pSrc 要查找的原串
*                        _pKey 要查找的关键串
*     返回值     成功,返回第一个找到的字首地址。没有找到,返回NULL。
*        作 者 武立强
*        时    间    2009-02-09
*/

char * StringFind(char* _pSrc, const char* _pKey)
{
    assert( NULL != _pSrc && NULL != _pKey );

    int nFirstFind = 0 ; //第一个字符是否找到, 0--没找到, 1--找到

    int nLastFind = 0;    // 最后一个字符

    char* pFirstFind = NULL; // 标记找到的第一个字符的位置

    const char* pKeyFirst = _pKey;

    while( '\0' != *_pSrc )
    {
        // 逻辑比较部分, 确保完全匹配查找关键串

        if( 0 == nFirstFind && *_pSrc == *_pKey )
        {
            nFirstFind = 1;
            pFirstFind = _pSrc;
            _pKey++;
        }
        // 匹配成功

        else if( 1 == nFirstFind && *_pSrc == *_pKey && '\0' == *(_pKey+1) )
        {
            return pFirstFind;
        }
        else if( 1 == nFirstFind && *_pSrc == *_pKey )
        {
            _pKey++;
        }    
        // 部分匹配,匹配失败

        else if( 1 == nFirstFind && *_pSrc != *_pKey )
        {    
            nFirstFind = 0;
            pFirstFind = NULL;
            _pKey = pKeyFirst;
        }

        // 针对一个字符的情况

        if( 1 == nFirstFind && '\0' == *(_pKey) )
        {
            return pFirstFind;
        }
        _pSrc++;
    }

    return NULL;
}

/*
*    定义函数 char * PrintKeyValue(char* _pchDest, const char* _pchSrc, const char* _pchKey);
*    表头文件 #include
*    函数描述    固定格式字符串解析函数。
*                        字符串的格式:"@ID:123456\n@TITLE:nba\n@DATE:2009-01-02\n@AUTHOR:zhansan\n"
*                        _pchSrc 要查找的原串。
*                        _pchKey 要匹配的关键串。
*                        _pchDest 查找的结果。
*     返回值     返回 _pchDest    的字符串起始地址。找不到返回 NULL
*        作 者 武立强
*        时    间    2009-02-10
*        注    意 应用示例:当查找DATE 时返回 2009-01-02
*/

char * PrintKeyValue(char* _pchDest, const char* _pchSrc, const char* _pchKey)
{
    assert( NULL != _pchDest && NULL != _pchSrc && NULL != _pchKey );
    
    int nStartFind = 0;
    char* pchDestStart = NULL;
    const char* pchKeyStart = NULL; // 指向关键字(参数)的开始地址;


    pchKeyStart = _pchKey;
    pchDestStart = _pchDest;

    while( '\0' != *_pchSrc )
    {
        // 匹配成功

        if( 1 == nStartFind && ':' == *_pchSrc && '\0' == *_pchKey )
        {
            _pchSrc++;
            while( '@' != *_pchSrc && '\0' != *_pchSrc )
            {
                *_pchDest++ = *_pchSrc++;
            }
            *_pchDest = '\0';
            _pchDest = NULL;
            _pchSrc = NULL;

            return pchDestStart;
        }
        // 逻辑处理

        else if( '@' == *_pchSrc )
        {
            nStartFind = 1;
        }
        // 匹配失败

        else if( 1 == nStartFind && ':' == *_pchSrc && '\0' != *_pchKey )
        {
            nStartFind = 0;
            _pchKey = pchKeyStart;
        }
        // 匹配失败

        else if( 1 == nStartFind && '\0' == *_pchKey && ':' != *_pchSrc )
        {
            nStartFind = 0;
            _pchKey = pchKeyStart;
        }
        // 匹配失败

        else if( 1 == nStartFind && *_pchSrc != *_pchKey )
        {
            nStartFind = 0;
            _pchKey = pchKeyStart;
        }
        else if( 1 == nStartFind && *_pchSrc == *_pchKey )
        {
            _pchKey++;
        }

        _pchSrc++;
    }

    return NULL;
}

/*
*    定义函数 int IsNumberString(const char* _pStr);
*    表头文件 #include
*    函数描述    测试字符串是否是一个数字串
*     返回值     如果是数字串返回0,否则返回-1
*        作 者 武立强
*        时    间    2009-02-13
*/

int IsNumberString(const char* _pStr)
{
    assert( NULL != _pStr );
    while( '\0' != *_pStr )
    {
        if( *_pStr > '9' || *_pStr < '0' )
        {
            return (-1);
        }
        _pStr++;
    }
    return 0;
}

/*
*    函数原形 char * StringReverse(char* _pchDest, const char* _pchSrc);
*    表头文件 #include
*    函数描述    将字符串 _pchSrc 到序反转, 结果存放在 _pchDest 中, _pchDest 要有足够的空间
*                        来容纳处理后的字符串
*     返回值     _pchDest 字符串的首地址
*        作 者 武立强
*        时    间    2009-02-13
*/

char * StringReverse(char* _pchDest, const char* _pchSrc)
{
    assert( NULL != _pchDest && NULL != _pchSrc );
    const char* pSrcFirst = _pchSrc;
    char * pDestFirst = _pchDest;

    // 将指针定位到字符串的结尾 '\0' 处

    while( '\0' != *_pchSrc++ ) ;
    
    _pchSrc--;
    while( _pchSrc-- >= pSrcFirst )
    {
        *_pchDest++ = *_pchSrc ;
    }
    *(_pchDest-1) = '\0';

    return pDestFirst;
}


*color: rgb(0, 0, 204); color: rgb(0, 0, 204);color: rgb(0, 0, 204);span style=color: rgb(0, 0, 204); StringReverse
阅读(1313) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~