将一个整数(数字或长字符串形式)转换成中文输出

程序写完了,但是有一点小瑕疵:

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值