将1000以下(不包括1000)的数子用字符数组的形式输出
如把int arr[]={65,66,67,78}转换成一个字符数组输出,输出结果为65666778
#include <stdio.h>
int main(int argc, const char * argv[]) {
int arr[]={65,66,67,78};//可以是任意个元素,但元素必须是1000以内的整型正数
int len = sizeof(arr) / sizeof(int);
char ch[3*len];//定义一个字符数组
int j = 0;
for(int i = 0; i < len; i++)
{
int ge = arr[i] % 10;
int shi = arr[i] / 10 % 10;
int bai = arr[i] /100;
switch(bai)//将百位的数子变成字符存放倒ch字符数组中
{
case 0:
break;
case 1:
ch[j] = '1';
j++;
break;
case 2:
ch[j] = '2';
j++;
break;
case 3:
ch[j] = '3';
j++;
break;
case 4:
ch[j] = '4';
j++;
break;
case 5:
ch[j] = '5';
j++;
break;
case 6:
ch[j] = '6';
j++;
break;
case 7:
ch[j] = '7';
j++;
break;
case 8:
ch[j] = '8';
j++;
break;
case 9:
ch[j] = '9';
j++;
break;
}
switch(shi)//将十位的数子变成字符存放倒ch字符数组中
{
case 0 :
if(bai != 0)
{
ch[j] = '0';
j++;
}
break;
case 1:
ch[j] = '1';
j++;
break;
case 2:
ch[j] = '2';
j++;
break;
case 3:
ch[j] = '3';
j++;
break;
case 4:
ch[j] = '4';
j++;
break;
case 5:
ch[j] = '5';
j++;
break;
case 6:
ch[j] = '6';
j++;
break;
case 7:
ch[j] = '7';
j++;
break;
case 8:
ch[j] = '8';
j++;
break;
case 9:
ch[j] = '9';
j++;
break;
}
switch(ge)//将个位的数子变成字符存放倒ch字符数组中
{
case 0:
if(bai != 0 || shi != 0)
{
ch[j] = '0';
j++;
}
case 1:
ch[j] = '1';
j++;
break;
case 2:
ch[j] = '2';
j++;
break;
case 3:
ch[j] = '3';
j++;
break;
case 4:
ch[j] = '4';
j++;
break;
case 5:
ch[j] = '5';
j++;
break;
case 6:
ch[j] = '6';
j++;
break;
case 7:
ch[j] = '7';
j++;
break;
case 8:
ch[j] = '8';
j++;
break;
case 9:
ch[j] = '9';
j++;
break;
}
}
ch[j] = '\0';//给将'\0'存放到字符数组ch末尾
printf("%s",ch);
return 0;
}
|
|