zoukankan      html  css  js  c++  java
  • 数字转汉字

    全局常量的声明

    const char * chnNumChar [10] = {"", "", "", "", "", "", "", "", "", ""};
    const char * sectionChar [4] = {"", "", "亿", "万亿"};
    const char * weightChar [4] = {"", "", "", ""};
    
    typedef long long ILONG;

    转化四位及其四位以下的整数

    string section2chn(ILONG onum)
    {
        string  res("");
        int weightindex = 0;
        bool iszero = false;
        while (onum > 0)
        {
            if (onum % 10)
                res = string(chnNumChar[onum % 10]) + weightChar[weightindex] + (iszero?chnNumChar[0]:"") + res;
            else if (!res.empty())
                iszero = true;
            ++ weightindex;
            onum /= 10;
        }
        return res;
    }

    转化整数

    string num2chn(ILONG onum)
    {
        string  res("");
        int sectionindex = 0;
        bool isneedzero = false;
        while (onum > 0)
        {
            if (onum % 10000)
                res = section2chn(onum % 10000) + sectionChar[sectionindex] + (isneedzero?chnNumChar[0]:"") + res;
            else if (!res.empty())
                isneedzero = true;
    
            if (onum % 10000 < 1000)
                isneedzero = true;
            ++ sectionindex;
            onum /= 10000;
        }
        return res;
    }

    测试

    #include <iostream>
    #include <string>
    #include <random>
    
    
    int main()
    {
        ILONG temnum = 6000000181;
        cout
            << temnum
            << "		"
            << num2chn(temnum)
            << endl;
        
        random_device rd;
        for (int i = 0; i < 1000; ++ i)
        {
            ILONG temnum = rd() ;
            cout
                << temnum
                << "		"
                << num2chn(temnum)
                << endl;
        }
    
        return 0;
    }
    出自datakv
  • 相关阅读:
    进阶实验2-3.3 两个有序链表序列的交集 (20分)
    习题2.7 弹球距离 (15分)
    习题2.2 数组循环左移 (20分)
    习题2.1 简单计算器 (20分)
    习题2.5 两个有序链表序列的合并 (15分)
    习题2.4 递增的整数序列链表的插入 (15分)
    线性表和链表
    C. Boboniu and Bit Operations
    D
    C. Good Subarrays
  • 原文地址:https://www.cnblogs.com/datakv/p/5606229.html
Copyright © 2011-2022 走看看