数字转化为汉字表示

自己试着写了些,测试了一下没发现问题,欢迎批评指正。
#include <iostream>
#include <stack>
using namespace std;
void changenum(long num,char**a,int &len_a); //将数字num转化为汉字表示,len_a+1表示转换后a中汉字的实际个数
void main()
{
	long num;
	cin>>num;
	char *a[100];
	int i=0;
	int len_a;
	changenum(num,a,len_a);
	while(i<=len_a)
	{
		cout<<*(a+i);
		i++;
	}
	cout<<endl;

}
void changenum(long num,char**a,int &len_a)
{
	if(num==0)
	{
		*a="零";
		len_a=0;
		return;
	}
	stack<int> s;
	long temp=num;
	int factor=10;
	char *str1[]={"壹","贰","叁","肆","伍","陆","柒","捌","玖"};
	char *str2[]={"拾","佰","仟","万","拾","佰","仟","亿"};
	int ssize,i;
	while(temp>=10)
	{
		s.push(temp%10);
		temp=temp/10;
	}
	if(temp%10>0)
		s.push(temp);

	ssize=s.size();
	i=0;
	while(!s.empty())
	{
		if(s.top()!=0)
		{
			if(ssize>=2)
			{
				*(a+i)=str1[s.top()-1];
				i++;
				*(a+i)=str2[ssize-2];
				i++;

				ssize--;
				s.pop();
			}

			else
			{
				*(a+i)=str1[s.top()-1];
				i++;
				s.pop();
				ssize--;
			}
		}
		else
		{
			while(ssize>=0&&s.top()==0)
			{
				if(ssize==0)
					break;
				else
				{
					if(ssize==9)
					{
						*(a+i)=str2[ssize-2];
						i++;
					}
					if(ssize==5)
					{
						*(a+i)=str2[ssize-2];
						i++;
					}
					s.pop();
					ssize--;
				}
			}
			*(a+i)="零";
			i++;
		}
	}
	len_a=i-1;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值