一楼在扯淡,二楼说的好。三楼写的是用除二算。我这里给你列出来我的吧。有两种办法。
法一:
/**这个是带响儿的,用数字的位运算
附带判断功能**/
#include
#include
#include
#include
void effect(int c)// use to achieve the one and zero effect.
{
if(c==1)
{
printf("\a");// beeper
}
else
_sleep(500);// pause
}
int main()
{
char str[12];
unsigned int num=0;// the input number
int i;
int a;// string length.
int x;// the bit arithmetic number
int flag=0;
printf("The number: ");
/*check if the input is illegal*/
gets(str);
a=strlen(str);// string length.
for(i=0; i {
if (!isdigit(str[i]) )
{
printf("您输入的不是数字,请重新输入.\n");
printf("The number: ");
gets(str);
a=strlen(str);
i=-1;
}
}
/*transform the str to un_int */
for(i=0; i {
num=num+(str[i]-48)*pow(10,i);//using math method.
}
/*output the number*/
for(i=31; i>=0; i--)
{
x=num>>i;
if(flag==1 || x==1)// use the flag to cut off the very beginning zero.
{
printf("%d", x&1);
effect(x&1);
flag=1;
}
}
printf("\n");
return 0;
}
法二:
直接使用itoa函数做。转成二进制的字符串
char *_itoa( int value, char *string, int radix );
这是函数原型
这是MSDN的EX
Example
/* ITOA.C: This program converts integers of various
* sizes to strings in various radixes.
*/
#include
#include
void main( void )
{
char buffer[20];
int i = 3445;
long l = -344115L;
unsigned long ul = 1234567890UL;
_itoa( i, buffer, 10 );
printf( "String of integer %d (radix 10): %s\n", i, buffer );
_itoa( i, buffer, 16 );
printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
_itoa( i, buffer, 2 );
printf( "String of integer %d (radix 2): %s\n", i, buffer );
_ltoa( l, buffer, 16 );
printf( "String of long int %ld (radix 16): 0x%s\n", l,
buffer );
_ultoa( ul, buffer, 16 );
printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul,
buffer );
}
Output
String of integer 3445 (radix 10): 3445
String of integer 3445 (radix 16): 0xd75
String of integer 3445 (radix 2): 110101110101
String of long int -344115 (radix 16): 0xfffabfcd
String of unsigned long 1234567890 (radix 16): 0x499602d2
这是我的作业 给你参考吧 只能输入正整数,所以你要想能装换负整数,就先判断下正负,正的直接输出,负的就把结果加个负号就行!
#include"iostream.h"
long change(int);
int main()
{
int n;
cout<<"请输入一个十进制整数:";
cin>>n;
cout<
}
long change(int n)
{
int a,b=1;
long c;
while (n!=0)
{
a=(n%2==0)?0:1;
if(b==1)c=a;
else {for(int j=1;j<=b-1;j++) a=a*10;c=c+a;}
b++;
if(a==0) n=n/2;
else n=(n-1)/2;
}
return c;
}
用不着这么麻烦自己算
#include
#include
#include
using std::bitset;
using namespace std;
int main()
{
int nInput;
cin>>nInput;
cout<
}
1楼给我们的教训就是给别人写例子自己不先执行一遍后果是严重的x.xb
LZ要的是win32 app程序还是win32 console程序?