数字转汉字

摘要:
chnNumChar[0]:“”)+res;elseif(!res.empty())为零=真;++权重指数;onum/=10;}返回符;}转换整数stringnum2chn(ILONGonnum){string(“”);intsectionindex=0;boolisneedzzero=false;而(onum˃0){if(onum%10000)res=section2chn(onum%10000)+sectionChar[sectionindex]+(isneedzero?ChnNumChar[0]:“”)+res;elseif(!res.empty())isneedzero=true;if(onum%10000˂1000)isneedezero=true;+sectionindex;onum/=10000;}返回符;}测试#include#include#includeintmain(){ILONGtemnum=60000000181;cout˂˂temnum˂˂“”˂˂num2chn(temnum)˂˂endl;random_devicerd;for(inti i=0;i˂1000;++i){ILONGtemnum=rd();cout˂˂temnum˂˂“”˂num2chnreturn0;}

全局常量的声明

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;
}

免责声明:文章转载自《数字转汉字》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Arcgis创建SDE_Geometry、SDO_Geometry的区别【转】Winform里面的缓存使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

转:将字符串或表达式直接转为C#可执行代码的办法

原文地址:http://blog.csdn.net/qwlovedzm/article/details/6292147 近日有个项目有不少的计算公式,而且要求能定制,如何能将字符串或表达式直接转为C#的可执行代码就好了。 经过从网上查阅资料,发现有一个开源的工具类,将它修改后改为如下效果: [c-sharp]view plaincopyprint?...

【转】MUD教程--巫师入门教程2

简单的人物原则上只要有 set_name<名字> 、 combat_exp <经验>就行了,当然我们总得稍微多添一点了。inherit NPC;void create(){set_name(<中文名>, ({ <英文id> }) );set("title", <头衔>);set("gender",...

json字符串转Map、json数组

json数组转map public static void main(String[] args){ String strArr = "[{"0":"zhangsan","1":"lisi","2":"wangwu","3":"maliu"}," + "{"00":"zhangsan","11"...

ASP.NET VS2013 Office 转 PDF

本文适用于VS2013 项目中的Word转换为PDF、Excel转换为PDF、PPT转换为PDF 0.一种更加简单方便的方法     1.本页所用的方法在本机测试时基本不会出现问题,只是偶尔PPT转PDF失败,需要重新添加dll,但是在服务器环境就会出现各种错误,需要各种配置。 2.http://www.cnblogs.com/moonache/p/49...

C#对XML、JSON等格式的解析 (转)

C#对XML、JSON等格式的解析 一、C#对XML格式数据的解析 1、用XMLDocument来解析 XmlDocument xmlDocument = newXmlDocument(); xmlDocument.Load("test.xml"); //创建新节点 XmlElement nn = xmlDocument.CreateElem...

【转】一个URL编码和解码的C++类

下面的代码实现了一个用于C++中转码的类strCoding。里面有UTF8、UNICODE、GB2312编码的互相转换。 .H文件: #pragma once #include <iostream> #include <string> #include <windows.h> using namespace std;...