请实现一个函数,给定一个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;
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。