注意事项:
八千万八千(√),八千万零八千(×)
输入为0时单独判断
#include <iostream> #include <cstring> using namespace std; int main() { char out_num[10][5] = { "yi","er","san","si","wu","liu","qi","ba","jiu" }; char out[3][7] = { " Shi"," Bai"," Qian" }; char N[11]; bool first = true; bool print_zero = true;//Wan与Qian cin >> N; if (N[0] == '-') { cout << "Fu"; first = false; for (int i = 0; i < strlen(N); ++i) { N[i] = N[i + 1]; } } int len = strlen(N); int zero = 0;//是否积累“零” int dif; for (int i = 0; i < len; ++i) { dif = len - i; if (dif < 5) { if (N[i] != '0') { if (zero == 1) { cout << " ling"; zero = 0; } if (!first)cout << " "; first = false; cout << out_num[N[i] - '0' - 1]; if (dif >= 2) cout << out[dif - 2]; } else { if (i == 0) { cout << "ling"; return 0; } zero = 1; } } else if (dif == 5) { if (N[i] != '0') { if (zero == 1) { cout << " ling"; zero = 0; } if (!first)cout << " "; first = false; cout << out_num[N[i] - '0' - 1]; } if (!(N[len - 6] == '0' && N[len - 7] == '0' && N[len - 8] == '0')) { cout << " Wan"; zero = 0; } } else if (dif > 5 && dif < 9) { if (N[i] != '0') { zero = 0; if (!first)cout << " "; first = false; cout << out_num[N[i] - '0' - 1]; if ((dif + 2) % 4 >= 0) cout << out[(dif + 2) % 4]; } else { zero = 1; } } else if (dif == 9) { if (!first)cout << " "; first = false; cout << out_num[N[i] - '0' - 1] << " Yi"; } } return 0; }