#include <stdio.h>
#include<stdlib.h>
int main()
{
int Type,a,b[10],i=0,c=0;
printf("请输入转换类型 1:十进制转二进制;2:二进制转十进制;
");
scanf("%d",&Type);
printf("请输入原数据
");
scanf("%d",&a);
switch(Type)
{
case 1:
if(a==0)
{
printf("二进制为:0
");
break;
}
while(a!=0)
{
b[i]=a%2;
a/=2;
i++;
}
printf("二进制为:");
i--;
for(;i>=0;i--)
printf("%d
",b[i]);
break;
case 2:
if(a==0)
{
printf("十进制为:0
");
break;
}
while(a!=0)
{
b[i]=a%10;
a/=10;
i++;
}
i--;
for(;i>=0;i--)
c=c*2+b[i];
printf("十进制为:%d
",c);
break;
}
}