// 读出0~9999范围的数
void read_group(const char *str, char **buf, int len, UINT flags)
{
char *buf_start = *buf;
BOOL zero = TRUE; // 当前是否为连续的0
BOOL zero_only = TRUE; // 字符串中是否只出现了0
int d, place;
int i;
static const char *list = "零一二三四五六七八九";
if (flags & RG_ZEROLEADING)
{
/* 如果不足1000, 则始终以"零"字开头 */
zero = FALSE;
if (len <= 3)
{
append(buf, "零");
zero = TRUE;
}
}
/* 遍历字符串 */
for (i = 0; i < len; i++)
{
// 忽略连续的0
if (zero)
{
if (str[i] == '0')
continue;
else
zero = FALSE; // 出现了非0数字时, 停止忽略
}
d = str[i] - '0'; // 当前位数字
place = len - i; // 当前为第几位
if (d > 0)
zero_only = FALSE; // 字符串中出现了非零数字
if (!(d == 1 && place == 2 && *buf == buf_start))
{
strncpy(*buf, list + 2 * d, 2); // 输出当前数字的汉字形式
*buf += 2;
}
if (d != 0) // 不允许"零千"、"零百"等这样的说法出现
{
switch (place)
{
case 4:
append(buf, "千");
break;
case 3:
append(buf, "百");
break;
case 2:
append(buf, "十");
break;
}
}
else
zero = TRUE; // 忽略接下来的0
}
if (*buf == buf_start) // 若buf为空字符串
{
if (!(flags & RG_IGNOREZEROVALUE)) // RG_IGNOREZEROVALUE要求0000对应空字符串
append(buf, "零"); // 最终内容为"零"
}
else if (zero && (!zero_only || ((flags & RG_ZEROLEADING) && (flags & RG_IGNOREZEROVALUE))))
*buf -= 2; // 去掉字符串末尾多余的"零", 如"四十零", zero表示字符串以"零"字结尾
// 一般情况下, 如果整个字符串只有一个"零"字(zero_only), 就不去掉
// 但如果同时设置了RG_ZEROLEADING和RG_IGNOREZEROVALUE选项, 那么即使字符串只有一个"零"字也要将其去掉, 变成空字符串
**buf = '\0';
}