参考代码:
#include<iostream> #include<string> using namespace std; int main() { string num; string read = ""; string s; int d = 0;//用来依次访问单位数组中的每一个单位 string danwei[5] = { "","shi ","bai ","qian ","wan "}; cin >> num; for (int i = num.length() - 1, sign = 0;i >= 0;i--, sign++)//从个位数向依次向高位读,sign自增可记录当前位置 { if ((sign == 4 || sign == 8)) { //对于十万以上的数,单位特殊处理 d = 0; sign == 4 ? s = "wan " : s = "yi "; } switch (num[i]-'0') { case 0: read = s + read;d++; if (num[i] - '0' == 0 && (sign == 0 || sign == 4 || sign == 8))//个位、万位、亿位的0不读 break; if (num[i + 1] - '0' != 0 ) { //连续的0不读 read = "ling " + read; } break; case 1: if (sign == 5 || sign == 9) //十万位和十亿位的1不读 read = danwei[d++] + s + read; else read = "yi " + danwei[d++] + s + read; break; case 2:read = "er " + danwei[d++] + s + read;break; case 3:read = "san " + danwei[d++] + s + read;break; case 4:read = "si " + danwei[d++] + s + read;break; case 5:read = "wu " + danwei[d++] + s + read;break; case 6:read = "liu " + danwei[d++] + s + read;break; case 7:read = "qi " + danwei[d++] + s + read;break; case 8:read = "ba " + danwei[d++] + s + read;break; case 9:read = "jiu " + danwei[d++] + s + read;break; default: break; } s = ""; } cout << read << endl; return 0; }
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题7.4 (C++代码)(和输入10个数排序没有区别,直接sort即可)浏览:2138 |
C二级辅导-公约公倍 (C语言代码)浏览:2158 |
矩阵转置 (C语言代码)浏览:1565 |
矩阵乘法 (C++代码)浏览:1662 |
printf基础练习2 (C语言代码)浏览:690 |
C语言训练-亲密数 (C语言代码)浏览:697 |
C语言程序设计教程(第三版)课后习题9.10 (C语言代码)浏览:866 |
1124题解浏览:630 |
大家好,我是验题君浏览:604 |
上车人数 (C语言代码)浏览:753 |
张晓琪 2021-04-15 09:33:13 |
有一说一 超喜欢作者这个思路
张晓琪 2021-04-15 09:49:37 |
if(read=="yi wan ") read="yi "; 应该加在那个for循环里面的开头