有符號整數(int)的漢語讀法。

        


請實現一個函數,給定一個32爲有符號整數(int 類型),函數輸出該數字符合漢語習慣的讀法。例如:10086 讀作 " 一萬零八十六"。

#include<iostream>
#include<list>
#include<string>
#include<stdlib.h>
#include<math.h>
using namespace std;
void m_itoa(int num,list<char> &vt)
{
    int n=num; 
    if(num<0)
        n=abs(num);
    while(n>0)
    {
        vt.push_front(n%10+'0');
        n/=10;
    }
}
int main()
{
    string n1[]={"零","一","二","三","四","五","六","七","八","九"};
    string n2[]={"個","十","百","千","萬","十","百","千","億","十"};
    int num;
    cin>>num;
    if(num<0)
        cout<<"負";
    list<char> vt;
    m_itoa(num,vt);
    list<char>::iterator str;
    int i=vt.size();
    for(str=vt.begin();str!=vt.end();--i,++str)
    {
        if(*str=='0')
        {
            if(i>8)
                cout<<n2[8];
            else if(i<8 && i>4)
                cout<<n2[4];
                        while(*str=='0')
                        {
                                str++;i--; 
                        }
            if(str==vt.end())
                break;
            else        
                cout<<n1[0];
        }
        cout<<n1[*str-'0'];
        if(i>1)
                cout<<n2[i-1];
    }
    cout<<endl;
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章