[算法题] 人民币大小写转换(阿拉伯数字和汉字转换)

来源:互联网 发布:怎么取消备案域名 编辑:程序博客网 时间:2024/06/05 07:10
复制代码/*******************************************************************************Project Code  : AccountFile name     : Author        : LatteDescription   : 阿拉伯数字转为中文字符串Function List :--------------------------------------------------------------------------------History:Date            Author          Modification20140703        Latte           created file.*******************************************************************************/#include "stdafx.h"#include <string>#include <iostream>using namespace std;#define MAX 100string g_strDigit[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};string g_strUnit[] = {"圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿",                       "拾", "佰", "仟", "万", "拾", "佰"};string g_strUnit2[] = {"角", "分"};/*******************************************************************************Func Name       : ReplaceSubStrDate Created    : 2014-07-03Author          : LatteDescription     : 将源字符串strOrig中第一个匹配strSub的子串部分替换为strReplaceInput           :                   string &strOrig,                   string strSub,                   string strReplaceOutput          :                   string &strOrigReturn          :                   intCaution         : 返回值如果为-1,则表示替换失败或未找到替换项*******************************************************************************/int ReplaceSubStr(string &strOrig, string strSub, string strReplace){    int pos = (int)strOrig.find(strSub);    int length = (int)strSub.length();    if (pos >= 0)    {        strOrig.replace(pos, length, strReplace);        return 0;    }    return -1;}/*******************************************************************************Func Name       : NumToChineseStrDate Created    : 2014-07-03Author          : LatteDescription     :                   将人民币double数值转化为人民币汉字stringInput           :                   double moneyOutput          : Return          :                   stringCaution         :*******************************************************************************/string NumToChineseStr(double money){    int i               = 0;    int ret             = 0;    int length          = 0;    char *p             = NULL;    char *pcDecimal     = NULL; //保存小数部分字符    char czNumber[MAX]  = {0};  //保存完整数字部分字符    string strResult;    cout << "======================================" << endl;    cout << money << endl;    //判断是否为小数    if (money < 0)    {        strResult = "不支持读负数";           return strResult;       }    //将数字转为数字字符串,利用sprintf_s的正则转换    sprintf_s(czNumber, MAX, "%.15lg", money);    printf("[No.0]%s\n", czNumber);     //如果数字是太大或太小的数,因为已经转为科学计数,所以会含有e字符    p = strchr(czNumber,'e');      if (NULL!=p)     {        strResult = "不支持读太大或太小的数";        return strResult;    }    p = strchr(czNumber, '.');      if (NULL != p)     {               p[0] = 0;            pcDecimal = p + 1;       }        length = (int)strlen(czNumber);      for (i = 0; i<length; i++)     {                if ('0' == czNumber[i] && 0 != ((length-1-i) % 4))        {            strResult += g_strDigit[czNumber[i] - '0'];        }         else         {            strResult += g_strDigit[czNumber[i] - '0'] + g_strUnit[length-1-i];        }       }    cout << "[No.1]把数字直接替换为汉字: \n" << strResult << endl;    //把strResult中的所有"零零"子串替换为"零"    while (1)    {        ret = ReplaceSubStr(strResult, "零零", "零");        if (ret < 0)        {            break;        }    }    cout << "[No.2]替换所有零零为零: \n" << strResult << endl;    ReplaceSubStr(strResult, "零亿", "亿");    ReplaceSubStr(strResult, "零万", "万");    if (strResult != "零圆")    //如果整数部分全为0,则不要去除元单位前面的零    {        ReplaceSubStr(strResult, "零圆", "圆");    }    cout << "[No.3]去除零亿、零万、零圆前面的零: \n" << strResult << endl;    //小数精确到两位数,即精确到单位分    if (NULL != pcDecimal)     {        //如果小数部分有数值而整数部分为0,则删除字符串中的零元        if (strResult == "零圆")        {            strResult.clear();        }        i = 0;        while (1)         {                       if (0 == pcDecimal[i] || i >= 2)                 break;               strResult += g_strDigit[pcDecimal[i] - '0'] + g_strUnit2[i];            i++;              }       }    cout << "[No.4]小数精确到两位数,即精确到单位分: \n" << strResult << endl;    return strResult;}int main(void){    //cout << "Result: " << NumToChineseStr(0.00) << endl;    //cout << "Result: " << NumToChineseStr(-345.67) << endl;    //cout << "Result: " << NumToChineseStr(1000.0) << endl;    cout << "Result: " << NumToChineseStr(130040600090.012) << endl;    return 0;}

这里写图片描述

0 0
原创粉丝点击