1082 Read Number in Chinese (25 分)

本文介绍了一种使用C++实现将数字转换为汉字的方法。通过定义一系列字符串数组,分别对应0到9的汉字表示,以及十、百、千等单位的汉字表示,再通过读取输入的数字字符串,逐位进行转换并输出相应的汉字,实现了从数字到汉字的精确转换。此程序还考虑了负数的情况,能够正确处理带符号的整数。

 

2019年2月13日

1:03

#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

using namespace std;



int main()

{



    string s;

    string c[11] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};

    string d[4] = {"", "Shi", "Bai", "Qian"};

    string e[3] = {"", "Wan", "Yi"};

    while(cin >> s)

    {

        int j = -1;

        if(s[0] == '-')

            cout << "Fu" << " ", j = 0;

        int len;

        len = s[0] == '-' ? s.size() - 1 : s.size();

        int t = len % 4;

        int f = 1, flag = 0;

        int k = (len  - 1) / 4;

        if(len == 1)

        {

            cout << c[++j] << endl;

            continue;

        }

        while(len != 0)

        {

             //cout << len << endl;

             j++;

             len--;

             t = len % 4;



             //cout << s[j] << endl;

             if(s[j] != '0')

             {

                 flag = 1;

                 if(f == 1)

                 {

                     cout << c[s[j] - '0'];

                     f = 0;

                 }

                 else

                 {

                    cout << " " << c[s[j] - '0'];



                 }

                 if(d[t] != "")

                    cout << " " << d[t];

             }

             else

             {

                  //cout << s[j] << endl;

                  while(s[j] == '0' && t != 0)

                  {

                      j++;

                      len--;

                      t = len % 4;

                      //cout << j <<" ";

                  }

                  if(t != 0)

                  {

                      //cout << t << endl;

                      cout << " " << c[0] ;

                  }

                  else if(s[j - 1] == '0' && s[j] != '0')

                      cout << " " << c[0];

                  if(s[j] != '0' )

                  {



                      cout << " " << c[s[j] - '0'];

                      if(d[t] != "")

                        cout<< " " << d[t] ;



                  }

             }

             if(t == 0 && e[k] != "" && flag == 1)

             {

                 //cout <<k << endl;

                 flag = 0;

                 cout << " " << e[k--] ;

             }

        }

        cout << endl;

    }

    return 0;

}

/*

-123456789

100800

123456789

-100800

100000000

10101010

9

-1

100000

108638

24508

23125000

*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值