数字转化为汉字读法 如123 读作一百二十三

粗一分析,如果真只这样的读的话,这好解决,关键是怎么样处理有0的情况,要分段处理。分 析千到个位的读法。
num_per_four函数处理千位到个位四位的法。分析出现0的所有情况。用条件语句,来限制出现的情况,得到想要的结 果。

num_chinese函数,先做与一万比较,如果小于一万,直接调用 num_per_four函数。如果大于一万,截取万位前面放进num_per_four里直接画出,中间输出万,如果此数不是万位,然后截取千位到个位 用函数输出,否则输出完毕。

 

粗一分析,如果真只这样的读的话,这好解决,关键是怎么样处理有0的情况,要分段处理。分析千到个位的读法。 num_per_four函数处理千位到个位四位的法。分析出现0的所有情况。用条件语句,来限制出现的情况,得到想要的结果。 num_chinese函数,先做与一万比较,如果小于一万,直接调用 num_per_four函数。如果大于一万,截取万位前面放进num_per_four里直接画出,中间输出万,如果此数不是万位,然后截取千位到个位用函数输出,否则输出完毕。 #include <iostream> #include <string> using namespace std; string str[10] = {"零","一","二","三","四","五","六","七","八","九"}; string wei[4] = {"", "十", "百", "千"}; void num_per_four(int num ) { //特处理0 if(num == 0) { cout<<"零";return; } int strm[10]; int base = 1000; int curr = 3; //特处理个位和十位为都为0 if( num > 1000 && num%1000 <=9 && num%1000 >= 1) { cout<<str[num /base]<<"千零"; cout<<str[num%1000]; return ; } while( num / base ==0 ) curr-- , base = base / 10; //把数字转化为对应的 while(base > 0) { int t = num /base; cout<<str[t]; if( t != 0) { cout<<wei[curr]; } num = num % base; if( num == 0 ) { break; } base = base / 10; curr--; } } void num_chinese(int num) { if( num / 10000 > 0) { num_per_four(num/10000); cout<<"万"; if(num %10000 != 0) { if(num %10000 <1000) cout<<"零"; num_per_four(num%10000); } else { return ; } } else { num_per_four(num); } cout<<endl; } int main() { int number ; cout << "请输入一个大于0且小于10亿的整数:" << endl; while (cin >> number && number < 1000000000) { num_chinese(number) ; } cout << "EXIT"; system("pause"); return 0; } 先要思考,考虑要表达的意思,程序要达到的目的。思考算法,寻求便捷的算法。

 

 

先要思考,考虑要表达的意思,程序要达到的目的。思考算 法,寻求便捷的算法。

你可能感兴趣的:(算法,String,System,stdstring)