你的运行环境是什么?我的怎么就是乱码?
小小战士,战士中的战斗机!
#include<stdio.h> char * chinese_number(int a, char * strcn) { char * b = "个十百千万亿"; char * c = "零一二三四五六七八九"; int s[16] = {0}, n, i, t; for(n = 0; a; a /= 10) s[n++] = a % 10; for(i = 0, s[n] = 1; n--;) { if(s[n]) { if(s[n + 1] == 0) { strcn[i++] = c[0]; strcn[i++] = c[1]; } strcn[i++] = c[s[n] * 2]; strcn[i++] = c[s[n] * 2 + 1]; if(t = n % 4) { strcn[i++] = b[t * 2]; strcn[i++] = b[t * 2 + 1]; } } if(n) { if(n % 8 == 0) { strcn[i++] = b[10]; strcn[i++] = b[11]; } else if(n % 4 == 0) { strcn[i++] = b[8]; strcn[i++] = b[9]; } } } strcn[i] = '\0'; if(!i) { strcn[0] = c[0]; strcn[1] = c[1]; strcn[2] = '\0'; } return strcn; } int main() { char s[128]; int a; printf("Try to input a number : "); scanf("%d", &a); puts(chinese_number(a, s)); return 0; }
#include<stdio.h> #include<string.h> char * chinese_number(int a, char * strcn) { char * b = "个十百千万亿"; char * c = "零一二三四五六七八九"; int s[16] = {0}, n, i, t; //另s[]保存了输入数字的每位元素 //例:输入a=12345 则s[0..4]分别为5,4,3,2,1 i=0; memset(s,0,sizeof(s)); while(a) { s[i]=a % 10; a=a/10; i++; } n=i; i=0; while(1) { if(n==-1) break; n--; if(s[n]!=0) { if(s[n+1] == 0) { strcn[i++] = c[0]; strcn[i++] = c[1]; } strcn[i++] = c[s[n] * 2]; strcn[i++] = c[s[n] * 2 + 1]; if(t = n % 4) { strcn[i++] = b[t * 2]; strcn[i++] = b[t * 2 + 1]; } } if(n!=0) { if(n % 8 == 0) { strcn[i++] = b[10]; strcn[i++] = b[11]; } else if(n % 4 == 0) { strcn[i++] = b[8]; strcn[i++] = b[9]; } } } strcn[i] = '\0'; if(i==0) { strcn[0] = c[0]; strcn[1] = c[1]; strcn[2] = '\0'; } return strcn; } int main() { char s[128]; int a; printf("Try to input a number : "); scanf("%d", &a); puts(chinese_number(a, s)); return 0; }