做了十几道题唯一一次弄出排行第一的答案,得炫耀一下
思路很简单

  1. 每一位只要数字不为0|1其单位必须输出
  2. 1000000为例,将之看作10 0000. 0000共10位(序号为0-9位),从第0位开始处理:
    • 0位和第1位单位为空'',值恒为0,其余单位见代码units数组。
    • 若位置position40则此处单位必须输出(代码中的position值实际上相当于本文中position-2),此处的0必不输出,因此元、万位不输出0但必须输出单位;
    • 当前位置的前一位置出现过0,则当前的0不输出,因此拾、佰、仟位不输出0。
    • 若位置position41说明是位,则此处若为1不输出。
  3. 位低的位即使满足输出的条件也不输出。
  4. 若当前处理到的位置单位为,但组装出的字符串仍为空,则先在字符串中装入
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct CH Ch;
struct CH
{
    char ch[4];
    Ch *next;
    /* data */
};
int main()
{
    char number[10][4] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    char unit[11][4] = {"分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿"};
    // char unit[13][4] = {"","","分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿"};
    char integer[4] = "整";
    long long num = 0;
    double value;
    int position;
    int zero = 0;
    int posnum = 0;
    Ch *ptr = 0;
    Ch *header = 0;
    while (scanf("%lf", &value) != EOF)
    {
        position = 0;
        // position = 2;
        // 不加会变成0.28
        num = (value + 0.0001) * 100;
        zero = 0x02;
        while (num)
        {
            // 上一个非0,当前不是起始位,方可输出0
            // 不是十位方可输出1
            // 当前是起始位,或当前位不为0方可输出单位
            if (!header && position > 1)
            {
                ptr = (Ch *)malloc(sizeof(Ch));
                ptr->next = header;
                header = ptr;
                strcpy(ptr->ch, "整");
            }
            posnum = num % 10;
            if ((0x01 & zero) || posnum)
            {
                ptr = (Ch *)malloc(sizeof(Ch));
                ptr->next = header;
                header = ptr;
                strcpy(ptr->ch, unit[position]);
            }
            if (posnum == 0)
            {
                if (!zero && position > 1)
                {
                    ptr = (Ch *)malloc(sizeof(Ch));
                    ptr->next = header;
                    header = ptr;
                    strcpy(ptr->ch, number[posnum]);
                }
            }
            else if (posnum == 1)
            {
                if (position != 3 && position != 7)
                {
                    ptr = (Ch *)malloc(sizeof(Ch));
                    ptr->next = header;
                    header = ptr;
                    strcpy(ptr->ch, number[posnum]);
                }
            }
            else
            {
                ptr = (Ch *)malloc(sizeof(Ch));
                ptr->next = header;
                header = ptr;
                strcpy(ptr->ch, number[posnum]);
            }
            num /= 10;
            position++;
            if (posnum)
            {
                zero &= 0xfd;
            }
            else
            {
                zero |= 0x02;
            }
            if ((position + 2) % 4 == 0)
            {
                zero |= 0x01;
            }
            else
            {
                zero &= 0xfe;
            }
        }
        printf("人民币");
        while (header)
        {
            ptr = header->next;
            printf("%s", header->ch);
            free(header);
            header = ptr;
        }
        printf("\n");
    }
    return 0;
}