数字转中文表示

此处表达式不超过万。

注意的地方:1000,可以1001,即0的情况 (首,中,尾),11 ,12等,10 20 等情况!

此处未考虑负数情况,可以加以判断,并且在开始加入负

#include <iostream>
#include <string>
#include <vector>

using namespace std;

string int_to_num(int num);

int main()
{
	int num;
	cin>>num;
	
	cout<<int_to_num(num)<<endl;
	return 0;
}




string int_to_num(int num)
{
	string num_str[] = {"零","一","二","三","四","五","六","七","八","九"};
	string base_str[] = {"","十","百","千","万" };
	
	string res;
	int count = 0;

	if(num == 0) return num_str[0];
	int num_low[10];

	while(num != 0)
	{
		
		int num_digit = num%10;
		num = num/10;

		num_low[count] = num_digit;

		if(num_digit  == 0 && count == 4 )//字母头为0
		{
			if(num_low[count-1] == 0)//高位连续为0知道万位!!
			{
				res = res.substr(num_str[0].size(),res.size()-num_str[0].size());
			}
			
			break;
		}
		if(num_digit  == 0&&count >= 1 && num_low[count-1] != 0 )//防止1000,可以1001,即0的情况
		{
			res = num_str[num_digit]+res;
		}
		else if(num_digit  == 1&&count == 1&&num == 0)//防止11,12等情况
		{
			res = base_str[num_digit]+res;
		}
		else
		{
			if(num_digit > 0)//必须大于0
			{
				res = num_str[num_digit]+base_str[count]+res;
			}
			
		}
		count++;
		if(count >= 5)break;
	}

	return res;

}
如果代码有bug,希望各位指正,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值