程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言問答 >> c語言問題,能進來幫助下嗎??

c語言問題,能進來幫助下嗎??

編輯:C語言問答

c語言問題,能進來幫助下嗎??

怎麼輸入小寫金額數把它換成大寫的??比如:1325 要換成壹仟三佰貳拾伍

最佳回答:

/**
 * @brief 將源字符串中的小寫金額轉換為大寫格式
 *
 * @param dest 目的字符串
 * @param src  小寫金額字符串
 * @return
 * - NULL 源字符串的格式錯誤,返回NULL
 * - 非NULL 目的字符串的首地址
 * @note 轉換根據:中國人民銀行會計司編寫的最新《企業、銀行正確辦理支付結算
 *       指南》的第114頁-第115頁
 */
char* chineseFee( char* dest, char* src )
{
    enum
    {
        START,                 //開始
        MINUS,                 //負號
        ZEROINT,               //0整數
        INTEGER,               //整數
        DECIMAL,               //小數點
        DECIMALfRACTION,       //小數位
        END,                   //結束
        ERROR                  //錯誤
    } status = START;
    struct
    {
        int minus;             //0為正,1為負
        int sizeInt;
        int sizeDecimal;
        int integer[10];
        int decimal[10];
    } feeInfo;
    char* NumberChar[] =
        { "零", "壹", "貳", "三", "肆", "伍", "陸", "柒", "捌", "玖" };
    char* UnitChar[] =
        { "整", "圓", "拾", "佰","仟", "萬", "拾", "佰", "仟", "億",
          "拾", "佰", "仟", "萬億", "拾", "佰", "仟", "億億",
          "角", "分", "負", "人民幣" };
         
    int      i, j,size;             //循環變量
    int      zeroTag        = 0,    //0標志
             decZeroTag     = 0;
     
    char*    pDest          = dest;
    char*    pSrc           = src;
   
    int*     pInt           = feeInfo.integer;
    int*     pDec           = feeInfo.decimal;
   
    //初始化
    feeInfo.sizeInt        = 0;
    feeInfo.sizeDecimal    = 0;
    feeInfo.minus          = 0;
   
    //分析字符串
    while( 1 )
    {
        switch ( *pSrc )
        {
            case '-' :
                status = ( status == START ) ? MINUS : ERROR;
                feeInfo.minus = ( status == MINUS ) ? 1 : 0;
                break;
            case '1' :
            case '2' :
            case '3' :
            case '4' :
            case '5' :
            case '6' :
            case '7' :
            case '8' :
            case '9' :
            case '0' :
                if ( *pSrc == '0' && status == ZEROINT )//|| status == START ) )
                {
                    status = ERROR;
                    break;
                }
                if ( status == MINUS || status == START || status == INTEGER )
                {
                    if ( *pSrc == '0' && ( status == MINUS || status == START ) )
                        status = ZEROINT;
                    else
                        status = INTEGER;
                    *pInt = (*pSrc) - 48;
                    ++pInt;
                    ++feeInfo.sizeInt;
                }
                else if ( status == DECIMAL || status == DECIMALfRACTION )
                {
                    status = DECIMALfRACTION;
                    *pDec = (*pSrc) - 48;
                    ++pDec;
                    ++feeInfo.sizeDecimal;
                }
                else
                {
                    status =ERROR;
                }
                break;
            case '.' :
                status = ( status == INTEGER || status == ZEROINT )
                         ? DECIMAL : ERROR;
                break;
            case '' :
                status = ( status == INTEGER || status == DECIMALfRACTION
                           || status == ZEROINT ) ? END : ERROR;
                break;
            default :
                status = ERROR;
        }
        if ( status == END )
            break;
        else if ( status == ERROR )
            return NULL;
       
        ++pSrc;
    }
   
    //只有1位小數時,設置百分位為0,使下面代碼不需要區分這兩種情況
    if ( feeInfo.sizeDecimal == 1 )
    {
        feeInfo.decimal[ 1 ] = 0;
        ++feeInfo.sizeDecimal;
    }
    //判斷是否需要打印小數部分,有小數部且十分位和百分位不都為0
    //需要打印小數部時,zeroTag設為0,否則設為1
    if ( feeInfo.sizeDecimal == 0                                 //沒有小數
        || ( !feeInfo.decimal[ 0 ] && !feeInfo.decimal[ 1 ] ) )   //小數部都為0
        decZeroTag = 1;
    else
        decZeroTag = 0;
       
    //printf( "int size: %d    decimal size: %d ", feeInfo.sizeInt, feeInfo.sizeDecimal );
   
    strcpy( pDest, UnitChar[ 21 ] );                  //初始化目標字符串-人民幣
   
    if ( feeInfo.minus ) strcat( pDest, UnitChar[ 20 ] );    //負號
       
    //處理整數部分
    size = feeInfo.sizeInt;
    for( i = 0; i < size; ++i )
    {
        j = size - i - 1 & 0x3;                              //j = 0時為段尾
        if ( feeInfo.integer[ i ] == 0 && j )                //處理非段尾0
        {
            zeroTag = 1;
        }
        else if ( feeInfo.integer[ i ] == 0 && !j )          //處理段尾0
        {
            if ( feeInfo.sizeInt == 1 && decZeroTag )        //特殊處理個位0
                strcat( pDest, NumberChar[ feeInfo.integer[ i ] ] );
            if ( feeInfo.sizeInt != 1 || decZeroTag )
                strcat( pDest, UnitChar[ size - i ] );
            zeroTag = 0;
        }
        else                                                 //處理非0
        {
            if ( zeroTag )
            {
                strcat( pDest, NumberChar[ 0 ] );
                zeroTag = 0;
            }
            strcat( pDest, NumberChar[ feeInfo.integer[ i ] ] );
            strcat( pDest, UnitChar[ size - i ] );
            if ( !j ) zeroTag = 0;                      //如果是段尾,設為非標志
        }
    }
   
    if ( decZeroTag )
    {
        strcat( pDest, UnitChar[ 0 ] );//沒有小數部,打印"整"字符
    }
    else
    {
        //十分位
        if ( feeInfo.decimal[ 0 ] )
        {
            strcat( pDest, NumberChar[ feeInfo.decimal[ 0 ] ] );
            strcat( pDest, UnitChar[ 18 ] );
        }
        else if ( feeInfo.sizeInt != 1 || feeInfo.integer[ 0 ] )
        {
            strcat( pDest, NumberChar[ feeInfo.decimal[ 0 ] ] );
        }
       
        //百分位不為0時
        if ( feeInfo.decimal[ 1 ] )   
        {
            strcat( pDest, NumberChar[ feeInfo.decimal[ 1 ] ] );
            strcat( pDest, UnitChar[ 19 ] );
        }
    }
    return dest;
}
 
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved