// 讀出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';
}