程序写完了,但是有一点小瑕疵:
(1)亿以上的数字没有进行处理;
(2)会出现“一十”,这样不该出现的问题;
(3)还有其他可能的问题。
下面是源码:
#include <iostream>
#include <stack>
using namespace std;
void NO_TO_ZH(int Number = 0)
{
int count_0 = 0;
int n_mod = 0;
int tmp_mod = 0;
stack<char*> s;
char *A[] = {"零", "一","二","三","四","五","六","七","八","九"};
char *B[] = {"十","百","千","万","亿"};
while(Number)
{
tmp_mod = Number % 10;
Number = Number / 10;
++n_mod;
if(tmp_mod == 0)
{
++count_0;
if(count_0 == n_mod)
{
continue;
}
else if(count_0 == 1)
{
s.push(A[tmp_mod]);
if(n_mod == 5)
s.push(B[n_mod - 2]);
}
}
else
{
count_0 = 0;
if(n_mod > 1)
{
if( n_mod >= 6 && n_mod <= 8)
s.push(B[n_mod - 6]);
else if( n_mod == 9)
s.push(B[n_mod - 5]);
else
s.push(B[n_mod - 2]);
}
if(!s.empty() && s.top() == B[0] && tmp_mod == 1)
continue;
s.push(A[tmp_mod]);
}
}
while(!s.empty())
{
cout<<s.top();
s.pop();
}
}
int main()
{
NO_TO_ZH(9909);
return 0;
}