请实现一个函数,给定一个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; }
时间: 2024-10-06 11:59:39