// money.cpp : 定义控制台应用程序的入口点。
//
//
//题目描述
//读入一个浮点数值,将其转化为金额的中文大写形式,最大单位到亿
//例如:
// 123.45转化为“壹佰贰拾叁元肆角伍分”。
//
//1)当金额为整数时,只表示整数部分,省略小数部分,并添加“整”字。
// 例如:123表示为“壹佰贰拾叁元整”
//
//2)当金额中有连续的0时(含一个0),只需写一个“零”即可。
// 例如:10005表示为“壹万零伍元整”
//
//3)10元缩写为“拾元整”。
//实现showChineseMoney函数
//用到的大写数字及单位: 分角拾佰仟万亿 零壹贰叁肆伍陆柒捌玖
#include <string.h>
#include <stdio.h>
#define STRLEN 256
char* g_LutNum[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"} ;
char* g_LutMon[] = {"分","角","元","拾","佰","仟","万","拾","佰","仟","亿"} ;
void showChineseMoney(double money)
{
int iDot;
int iZeroNum=0;
char tmp[STRLEN];
memset(tmp, 0 ,STRLEN);
sprintf(tmp, "%lf", money);
for(iDot = 0; (tmp[iDot]!='.' && tmp[iDot]!='\0'); iDot++);
char strDec[STRLEN];
memset(strDec, 0 ,STRLEN);
char strWhl[STRLEN];
memset(strWhl, 0 ,STRLEN);
/* 处理整数部分 */
bool zeroFlag = false;
int num;
int i = 0;
while (iDot--)
{
num = tmp[i++] - '0';
if(0 != num && zeroFlag) /* 加零 */
{
strcat(strWhl, g_LutNum[0]);
zeroFlag = false;
}
if(0 == num && (iDot == 4 || iDot == 0)) /* 加万与元 */
{
strcat(strWhl, g_LutMon[iDot+2]);
zeroFlag = false;
}
if(0 == num) /* 是零则跳过*/
{
zeroFlag = true;
iZeroNum++;
continue;
}
strcat(strWhl, g_LutNum[num]);
strcat(strWhl, g_LutMon[iDot+2]);
}
iDot = i;
/* 处理小数部分 */
if('\0' == tmp[iDot])
{
strcat(strDec, "整");
}
else
{
strcat(strDec, g_LutNum[int(tmp[iDot+1]-'0')]);
/* 处理角 */
if(0 != int(tmp[iDot+1]-'0'))
{
strcat(strDec, g_LutMon[1]);
}
/* 处理分 */
if(0 != int(tmp[iDot+2]-'0'))
{
strcat(strDec, g_LutNum[int(tmp[iDot+2]-'0')]);
strcat(strDec, g_LutMon[0]);
}
}
strcat(strWhl, strDec);
/* 处理情况3 */
if(iDot == iZeroNum +1 && tmp[0] == '1')
{
printf("%s\n", strWhl+2);
}
else
{
printf("%s\n", strWhl);
}
}
double input[] = {100, 123.45f, 400004, 12021, 123456789.2, 333.00, 103050709.09, 10203};
int main(int argc, char* argv[])
{
for (int i = 0; i < sizeof(input) / sizeof(double); i++)
{
showChineseMoney(input[i]);
//showChineseMoney(12021);
}
return 0;
}