我只是在编写一个程序将十进制数更改为另一个基数(2<=基数<=16(。
运行程序并打印正确答案后,我遇到了一个错误,内容为:"程序已停止工作"。你能看看我的代码并找到它悬垂的地方吗???!!!我真的很困惑。
这是我的代码:
int decimal, radix, pow = 1, temp;
printf("enter your number and the 2 <= radix <= 16n");
scanf("%d%d",&decimal, &radix);
temp = decimal;
for(; temp >= radix; temp /= radix)// finding the greatest exponent of radix in decimal
pow *= radix;
while(pow >= 1){
decimal -= temp * pow;
if(temp == 10)
printf("A");
else if(temp == 11)
printf("B");
else if(temp == 12)
printf("C");
else if(temp == 13)
printf("D");
else if(temp == 14)
printf("E");
else if(temp == 15)
printf("F");
else
printf("%d",temp);
pow /= radix;
temp = decimal / pow;
}
puts("");
我认为问题是因为"temp = decimal/pow",但我该如何解决它?
在循环
结束时计算temp = decimal / pow;
时检查pow
是否为 0 while
pow /= radix;
if (pow > 0)
{
temp = decimal / pow;
}