最高到千亿,精确到分
#include <iostream>
#include <string>
using namespace std;
int main() {
const char* const upperNum[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
const char* const upperStep[] = { "", "万", "亿" };
const char* const bfPointUnit[] = { "", "拾", "佰", "仟" };
string temp;
string coutstr = "";
cin >> temp;
int lengthTotal = temp.length();
int lengthBfPoint = lengthTotal;
int localPos;
int pointPos = temp.find('.');
bool hasZero = false;
bool hasPoint = false;
bool zeroPassUnit = true;
hasPoint = pointPos == -1 ? false : true;
lengthBfPoint = hasPoint ? pointPos : lengthTotal;
localPos = lengthBfPoint - 1;
//转换小数点前数字
for (int i = 0; i < lengthBfPoint; ++i) {
int num = temp[i] - char('0');
if (num == 0)
hasZero = true;
else if (num > 0 && hasZero) {
coutstr += upperNum[0];
hasZero = false;
}
if (num > 0) {
coutstr = coutstr + upperNum[num] + bfPointUnit[localPos % 4];
zeroPassUnit = true;
}
if (localPos % 4 == 0&&zeroPassUnit) {
int stepPos = localPos / 4;
coutstr += upperStep[stepPos];
zeroPassUnit = false;
}
localPos--;
}
coutstr += "元";
if (!hasPoint || pointPos == lengthTotal - 1)
coutstr += "整";
else {
//转换小数点后数字
int jiao = temp[pointPos + 1] - char('0');
int fen;
if (pointPos + 2 < lengthTotal)
fen = temp[pointPos + 2] - char('0');
else
fen = 0;
if (jiao == 0 && fen == 0)
coutstr += "整";
else {
coutstr = coutstr + upperNum[jiao] + "角";
if (fen > 0)
coutstr = coutstr + upperNum[fen] + "分";
}
}
cout << coutstr << endl;
system("pause");
return 0;
}