69,369
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n,k,i,j,m=0;
char str[38] = {0};
printf("请输入要转换的10进制数n和要转换的进制k:");
scanf("%d%d",&n,&k);
while(n)
{
str[m++] = (n%k)["0123456789ABCDEF"];
n/=k;
}
str[m]='\0';
printf("转换前为:%s\n",str);
for(i=0,j=m-1;i<j; str[i]^=str[j]^=str[i]^=str[j], i++,j--);
printf("转换后为:%s\n",str);
system("PAUSE");
return 0;
}
/* 反转字符串 */
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--)
{
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
/* 转换int到char[] */
void itoa(int n, char s[])
{
int i, sign;
/* 记录符号位 */
if ((sign = n) < 0)
n = -n;
i = 0;
/* 把个位、十位……依次存到s[]中 */
do
{
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
/* 把符号位存到最后 */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
/* 反转字符串 */
reverse(s);
}
char s[10] = {0};
int n = 0;
int Int = 65536;
do
{
s[n++] = (Int % 10) + '0';
} while ( Int /= 10 );
n--;
for ( int i = 0; i < n; i++, n-- )
{
char t = s[n];
s[n] = s[i];
s[i] = t;
}
printf("%s\n",s);