c语言——用中文读出数字

输入一个整形数,然后按汉语的习惯,将其读出来并输出。如1052,读作:一千零五十二。
输入样例:
1052
输出样例:
一千零五十二

代码打了很多补丁,可读性不强;

假设输入最大值为千万,先输出“XX”万,再使用同样思路输出“XX”万“XX”;

输出“XX”时,只需要考虑以下情况:2222、2022、2002、2000、2010

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char num[10],c[10][4] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"},r[20];
    int h = 0,l;
    gets(num);
    l = strlen(num);
    int nh = strlen(num),nl = 0,flag = 0,k;
    if(l > 4)
    {
        int nh = l - 4 - 1,nl = 0,flag = 0,k;
        switch(l-4)
        {
        case 4:
            k = num[nl++]-'0';
            //r[h++] = c[num[nl++]-'0'];
            printf("%s",c[k]);
            r[h++] = '千';
            printf("%s", "千");
            for(int i = nl; i <= nh; i++)
            {
                if(num[i] != '0') flag = 1;
            }
            if(!flag)
            {
                nl += 3;
                break;
            }
        case 3:
            k = num[nl++]-'0';
            //r[h++] = c[num[nl++]-'0'];
            for(int i = nl; i <= nh; i++)
            {
                if(num[i] != '0') flag = 1;
            }
            if(!flag)
            {
                nl += 2;
                break;
            }
            printf("%s",c[k]);
            if(num[nl - 1] != '0')
                printf("%s","百");

        case 2:
            if(l > 6 && num[nl] == '0' && num[nl-1] == '0')
            {
                nl++;
            }
            else
            {
                k = num[nl++]-'0';
                //r[h++] = c[num[nl++]-'0'];
                if(k != 1)
                printf("%s",c[k]);
                if(num[nl - 1] != '0')
                    printf("%s","十");
            }

        case 1:
            if(num[nl] != '0')
            {
                k = num[nl++]-'0';
            //r[h++] = c[num[nl++]-'0'];
            printf("%s",c[k]);
            }else nl++;

        }
        printf("%s","万");

        switch(4)
        {

        case 4:
            nh = strlen(num);
            k = num[nl++]-'0';
            flag = 0;
            //r[h++] = c[num[nl++]-'0'];
            printf("%s",c[k]);
            r[h++] = '千';
            printf("%s", "千");
            for(int i = nl; i < nh; i++)
            {
                if(num[i] != '0') flag = 1;
            }
            if(!flag)
            {
                nl += 3;
                break;
            }
        case 3:
            k = num[nl++]-'0';
            //r[h++] = c[num[nl++]-'0'];
            for(int i = nl-1; i < nh; i++)
            {
                if(num[i] != '0') flag = 1;
            }
            if(!flag)
            {
                nl += 2;
                break;
            }
            printf("%s",c[k]);
            if(num[nl - 1] != '0')
                printf("%s","百");

        case 2:
            if(num[nl] == '0' && num[nl-1] == '0')
            {
                nl++;
            }
            else
            {
                k = num[nl++]-'0';
                //r[h++] = c[num[nl++]-'0'];
                if(k != 1)
                printf("%s",c[k]);
                if(num[nl - 1] != '0')
                    printf("%s","十");
            }

        case 1:
            if(num[nl] != '0')
            {
                k = num[nl++]-'0';
            //r[h++] = c[num[nl++]-'0'];
            printf("%s",c[k]);
            }else nl++;

        }

    }else
    {
         int nh = strlen(num);
        switch(strlen(num))
        {

        case 4:
            nh = strlen(num);
            k = num[nl++]-'0';
            //r[h++] = c[num[nl++]-'0'];
            printf("%s",c[k]);
            r[h++] = '千';
            printf("%s", "千");
            for(int i = nl; i < nh; i++)
            {
                if(num[i] != '0') flag = 1;
            }
            if(!flag)
            {
                nl += 3;
                break;
            }
        case 3:
            k = num[nl++]-'0';
            //r[h++] = c[num[nl++]-'0'];
            for(int i = nl-1; i < nh; i++)
            {
                if(num[i] != '0') flag = 1;
            }
            if(!flag)
            {
                nl += 2;
                break;
            }
            printf("%s",c[k]);
            if(num[nl - 1] != '0')
                printf("%s","百");

        case 2:
            if( num[nl] == '0' && num[nl-1] == '0')
            {
                nl++;
            }
            else
            {
                k = num[nl++]-'0';
                //r[h++] = c[num[nl++]-'0'];
                if(k != 1)
                printf("%s",c[k]);
                if(num[nl - 1] != '0')
                    printf("%s","十");
            }

        case 1:
            if(num[nl] != '0')
            {
                k = num[nl++]-'0';
            //r[h++] = c[num[nl++]-'0'];
            printf("%s",c[k]);
            }

        }
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值