zoukankan      html  css  js  c++  java
  • 杂记:阿拉伯数字转中文大写数字

      之前面试过程中碰到一道笔试题,要求将阿拉伯数字的金额转换成中文大写数字,类似发票上的写法。当时只是简单的将小数点之前的数字转换为汉字,并没有实现小数点之后的数字以及当数字中间出现多个0的情况。

      现将完善后的代码记录下来,不足之处还望各位不吝赐教。

      1 #include <iostream>
      2 #include <string>
      3 
      4 #include <math.h>
      5 
      6 
      7 using namespace std;
      8 
      9 const char *afterDecimals[] = { "","","" };
     10 const char *chineseNum[] = {"","","","","","","","","",""};
     11 const char *beforeDecimals[] = { "","","",""};
     12 const char *step[] = { "","","亿","","亿","","亿" };
     13 
     14 bool isZero(long double num)
     15 {
     16     if (num > -0.0000001 && num < 0.0000001)
     17         return true;
     18     else
     19         return false;
     20 }
     21 
     22 int getLength(long double num)
     23 {
     24     int length = 0;
     25 
     26     //num零值判断
     27     if (isZero(num))
     28         return 1;
     29     else
     30     {
     31         /*
     32         while ((long long)num)
     33         {
     34             length++;
     35             num /= 10;
     36         }
     37         return length;
     38         */
     39         return (int)(log10(num) + 1);
     40     }
     41 }
     42 
     43 string change(long double num)
     44 {
     45     string str;
     46     char buf[64] = { 0 };
     47     sprintf_s(buf, "%0.0f", (num*100));
     48     int bufLength = strlen(buf);
     49     cout << "buf = " << buf << endl;
     50     int length = getLength(num);
     51     bool is0 = false;
     52 
     53     //小数点之前
     54     for (int i = 0; i < length; i++)
     55     {
     56         int j = buf[i] - '0';
     57         if (j == 0)
     58             is0 = true;
     59         else if (j > 0 && is0)//处理遇到连续多个0的情况
     60         {
     61             str += chineseNum[0] + string(chineseNum[j]) + beforeDecimals[(length - i - 1) % 4];
     62             is0 = false;
     63         }
     64         else
     65         {
     66             str += chineseNum[j];
     67             str += beforeDecimals[(length - i-1) % 4];
     68         }
     69         if ((length - i-1) % 4 == 0)
     70         {
     71             str += step[(length - i-1) / 4];
     72             is0 = false;
     73         }
     74     }
     75         
     76 
     77     //小数点以后
     78     for (int i = 0; i < bufLength; i++)
     79     {
     80         char ch = buf[i];
     81         if (i >= length)
     82             str += chineseNum[atoi(&ch)] + string(afterDecimals[i + 1 - length]);
     83         
     84         else if (i == length-1)
     85             str += string(afterDecimals[0]) + string(".");
     86         else
     87             continue;        
     88     }
     89     return str;
     90 }
     91 
     92 int main(int argc, char* argv[])
     93 {
     94     
     95     long double num = 123456789.236;
     96     cout << "num = 123456789.236" << endl;
     97     string str = change(num);
     98     cout << str << endl;
     99     
    100 
    101 
    102     getchar();
    103     return 0;
    104 }
    View Code

      其中change(long double num)函数为主体,之所以采用long double类型是为了转换大于99999999的数字。

  • 相关阅读:
    完善例题3.2的日期类mydate
    杨辉三角形
    求100以内的素数
    九九乘法表
    实现计算机界面
    完善3.2例题
    杨辉三角法
    素数程序
    九九乘法表
    杨辉三角
  • 原文地址:https://www.cnblogs.com/lianshuiwuyi/p/7762024.html
Copyright © 2011-2022 走看看