#include <stdlib.h>
#include <stdio.h>
#define SIZE 25
int main (void)
{
int d, b, c;
printf(" Enter an integer and press 'enter':\n");
scanf("%d" , &d);
printf(" Enter the desired base and press 'enter':\n");
scanf("%d" , &b);
if (b < 2) {
printf(" Your base is to low! \n")
} else {
while (d != 0) {
int radix;
radix = d % b;
d = d / b;
char basechars[] = "0123456789ABCDEF";
printf("%c" , basechards[radix]);
}
}
return 0;
}
此程序提示用户输入小数和基数,以将该小数转换为已选择的基数。然而,转换以相反的顺序打印,我需要它定期打印。示例:输入:112,然后输入16,结果为07而不是70。
答案 0 :(得分:1)
您可以将每个数字存储在数组中:
} else {
char arr[32];
int counter = 0;
while (d != 0) {
int radix;
radix = d % b;
d = d / b;
char basechars[] = "0123456789ABCDEF";
arr[counter++] = basechars[radix];
}
if (counter == 0)
arr[counter++] = '0';
arr[counter++] = '\0';
print_rev(arr);
printf("\n");
}
然后使用递归函数打印字符串(它将反转输出):
void print_rev(const char *s)
{
if (*s) {
print_rev(s + 1);
printf("%c", *s);
}
}
或直接:
} else {
char arr[32];
int counter = 0;
while (d != 0) {
int radix;
radix = d % b;
d = d / b;
char basechars[] = "0123456789ABCDEF";
arr[counter++] = basechars[radix];
}
if (counter == 0) {
printf("0");
else {
while (counter--)
printf("%c", arr[counter]);
}
printf("\n");
}
答案 1 :(得分:0)
此版本执行动态内存分配,因此您无需事先指定转换后的字符串的长度。
数字可以随意大小,在二进制转换中尤为重要。我还检查了基数16,这是上限。
int main (void)
{
int d, b, c, i = 1;
char *converted = malloc(i);
printf(" Enter an integer and press 'enter':\n");
scanf("%d" , &d);
printf(" Enter the desired base and press 'enter':\n");
scanf("%d" , &b);
if (b < 2) {
printf(" Your base is to low! \n");
return 1;
} else if (b > 16) {
printf(" Your base is to high! \n");
return 1;
} else {
while (d != 0) {
int radix;
radix = d % b;
d = d / b;
char basechars[] = "0123456789ABCDEF";
converted = realloc(converted, i++);
*(converted +i - 1) = basechars[radix];
}
}
i--;
while(i != 0) {
printf("%c", converted[i]);
--i;
}
free(converted);
printf("\n");
return 0;
}
答案 2 :(得分:0)
你也忘记了负数。
} else if (b > 16) {
printf(" Your base is too high! \n");
} else {
const char basechars[] = "0123456789ABCDEF";
char arr[LONG_BIT]; // d is an integer, so LONG_BIT holds
// enough characters for a binary representation.
int counter = 0;
int negative_flag = 0;
if (d < 0) {
d = -d;
negative_flag = 1;
}
do {
int digit = d % b;
d = d / b;
arr[counter++] = basechars[digit];
} while (d != 0);
if (negative_flag) {
printf ("-");
}
while (counter--) {
printf ("%c", arr[counter]);
}
printf ("\n");
}
答案 3 :(得分:0)
提供递归方法。
确定是否需要从更有效的数字打印额外的chars
,递归调用辅助函数,然后然后打印数字最低有效数字。
代码应该注意处理&#34; 0&#34;作为有效的输入。以下使用负值来处理INT_MIN
。
static void PrintDigits(int x, int base) {
static const char basechars[] = "0123456789ABCDEF";
if (x <= -base) {
PrintDigits(x/base, base);
}
putchar(basechars[-(x%base)]);
}
void PrintInt(int x, int base) {
if (base < 2 || base > 16) {
printf(" Your base is out of range! \n");
} else {
if (x < 0) putchar('-');
else x = -x;
PrintDigits(x, base);
}
putchar('\n');
}
int main(void) {
PrintInt(65535, 16);
PrintInt(65535, 10);
PrintInt(65535, 2);
PrintInt(INT_MAX, 10);
PrintInt(0, 10);
PrintInt(-1, 16);
PrintInt(INT_MIN, 10);
int d, b;
printf(" Enter an integer and press 'enter':\n");
scanf("%d" , &d);
printf(" Enter the desired base and press 'enter':\n");
scanf("%d" , &b);
PrintInt(d, b);
return 0;
}
FFFF
65535个
1111111111111111个
2147483647个
0
-1
-2147483648
即使使用基数2,递归深度也不会超过int
的位宽。