c语言有关的问题:
# include
include
include
void is_trans(char *number,int type);
void is_trans2(int sum);
void main()
{
char number[1000];
int type;
printf("请输入你要输入的数字:");
gets(number);
printf("请输入你要输入的数的进制:");
scanf("%d",&type);//为什么当gets和scanf互换一下位子,输入完scanf后,gets就直接跳过了呢??
is_trans(number,type);
}
void is_trans(char *number,int type)
{
int i,len,sum,q;
len = strlen(number);
sum = 0;
for (i = 0;number[i] != '\0';i++)
{
if (type == 8||10)
break;
switch (number[i])
{
case 'A': number[i] = 10;break;
case 'B': number[i] = 11;break;
case 'C': number[i] = 12;break;
case 'D': number[i] = 13;break;
case 'E': number[i] = 14;break;
case 'F': number[i] = 15;break;
}
}
for (i = 0;number[i] != '\0';i++)
{
q = len-i-1;//为什么当输入的是7和8,最后这个number[i]在调试的时候显示的值是55'7',最后sum直接就等于55了,这是为什么呢??
sum = sum + (number[i]*pow(type,q));
}
is_trans2(sum);
}
void is_trans2(int sum)
{
int i,a[1000],n,p;
n = 0;
for (i = 0;sum != 0;i++)
{
p = sum % 2;
sum = sum / 2;
a[i] = p;
n++;
}
for (i = n - 1;i >= 0;i--)
{
printf("%d",a[i]);
}
}