#include <stdio.h>
void convert1(int);
char int2char(int);
void inverse(char[]);
void convert(int); //书上编写的函数,不过我分析的还有些迷糊。
int main(int argc, char *argv[])
{
int number;
printf("please input a number:");
scanf("%d",&number);
convert1(number); //调用我写的函数,虽然复杂,还算能使用,书上要求用递归,这点我没有做到。
system("pause");
return 0;
}
void convert1(int n)
{
char ch[10];
int m = 0;
int i = n,j,k;
do
{
j = i % 10;
ch[m++] = int2char(j);
i = (i - j) / 10;
}while (i);
ch[m] = '\0';
inverse(ch); //begin inverse.
puts(ch);
}
char int2char(int n)
{
char c;
if (n >= 0 && n <= 9)
{
switch (n)
{
case 0:
c = '0';
break;
case 1:
c = '1';
break;
case 2:
c = '2';
break;
case 3:
c = '3';
break;
case 4:
c = '4';
break;
case 5:
c = '5';
break;
case 6:
c = '6';
break;
case 7:
c = '7';
break;
case 8:
c = '8';
break;
case 9:
c = '9';
break;
}
}
else
{
c = ' ';
}
return c;
}
void inverse(char ch[])
{
int begin = 0, end = strlen(ch) - 1, mid = end / 2;
char c;
do
{
c = ch[begin];
ch[begin ++] = ch[end];
ch[end --] = c;
}while (begin < mid);
}
void convert(int n)
{
int i;
if ((i = n / 10) != 0)
{
convert(i);
}
putchar(n % 10 + '0');
}
|