注意点:1,中间位为零的情况处理(可能输出ling,可能不需要输出ling);2,负号的处理;3,结果为零的处理;4,空格的处理。
#include <iostream>
#include <string>
using namespace std;
char cnumber[10][8]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
char digit[5][8]={"","Shi","Bai","Qian"};
char danwei[3][7]={"","Wan","Yi"};
int main(){
string s;
getline(cin,s);
bool firststring=false;//表示是否输出了第一个字符串
if(s[0]=='-'){
s.erase(s.begin());
if(s.size()>0){
printf("Fu"); //避免输出 负零的情况
firststring=true;
}
}
while(s[0]=='0'){
s.erase(s.begin()); //删除前导零
}
if(s.size()==0){
printf("ling");
return 0; //零
}
for(int i=0;i<s.size();i++){
int temp=s.size()-1-i;
if(s[i]!='0'){ //对应位不为零的情况
if(firststring) printf(" %s",cnumber[s[i]-'0']); //一二三四
else{
printf("%s",cnumber[s[i]-'0']);
firststring=true;
}
}
else{ //对应位为零的情况
if(temp%4==0){
if(temp/4!=0) printf(" %s",danwei[temp/4]); //处在万位或亿位的零
continue;
}else{ ////处在非万位及亿位的零
int j=i;
for(;j<s.size();j++){
if(s[j]=='0'){
int x=s.size()-1-j;
if(x/4!=0&&x%4==0) printf(" %s",danwei[x/4]); //一串连续的零中若有万位
}else{
printf(" ling"); //发现第一个不为零的位置
i=j-1;
break;
}
}
if(j>=s.size()) break;//后面的位数均为 零
else continue;
}
}
if(temp%4!=0) printf(" %s",digit[temp%4]); //个十百千
else if(temp/4!=0) printf(" %s",danwei[temp/4]); //亿,万
}
return 0;
}