- #include <iostream>
- #include <string>
- #include <stdlib.h>
- using namespace std;
- string ConvertMoneyCaps(long double moneySum)
- {
- long int temp_i = (long int)moneySum; /**//* 整数部分 */
- float temp_f = moneySum - temp_i; /**//* 小数部分 */
- int digit = 0, i, j, k, num_i;
- string money("");
- char num[20], *p;
- char name[][3] = {"元","拾","佰","仟","万","亿"};
- char numchar[][3] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
- ltoa(temp_i, num, 10); /**//* 整数部分转换成字符串后在处理 */
- p = num;
- digit = strlen(num); /**//* 整数部分位数 */
- /**//*-------- 处理整数部分 start--------*/
- for(i = 1; i <= digit; i ++)
- {
- k = (digit - i) % 4;
- if(isdigit(*p))
- {
- num_i = *p & 0xF; /* 把字符转换成数字, 比如 '0'-> 0,'1' -> 1*/
- /**//*-------- 转换数字开始 ---------*/
- if(num_i)
- {
- money = money+ numchar[num_i];
- }
- else
- {
- if(k && (*(p + 1) &0xF))
- money += "零";
- }
- /**//*-------- 转换数字结束 -------*/
- /**//*--------- 添加计数单位开始 ----*/
- if(k)
- {
- if(num_i)
- money = money + name[k];
- }
- else
- {
- j = digit - i;
- if(j)
- money = money + name[j/4 + 3];
- else
- money += "元";
- }
- /**//*-------- 添加计数单位结束 --------*/
- p++;
- }
- else
- {
- money = "遇到非数字退出!";
- return money;
- }
- }
- /**//*-------- 处理整数部分 End --------*/
- /**//*-------- 处理小数部分 start--------*/
- if(temp_f> 0.01)
- {
- if((int)(temp_f*10)) money = money + numchar[(int)(temp_f*10)] + "角";
- if((int)(temp_f*100)%10) money = money + numchar[(int)(temp_f*100)%10] + "分";
- }
- /**//*-------- 处理小数部分 End--------*/
- money += "整";
- return money;
- }
- int main()
- {
- long double x = 33.20;
- cout <<"please input the money:";
- cin>> x;
- cout << "Convert Money Caps:";
- string money = ConvertMoneyCaps(x);
- cout << money <<endl;
- return 0;
- }
来源: http://www.bubuko.com/infodetail-2654692.html