关于C++十进制转为二进制问题

qiang_top 2009-09-23 07:14:45
#include<iostream.h>;

void main()
{
int a;
int count=1,i=1;
int b[20],c[20];
cout<<"Please input a Integer number";
cin>>a;
if(a>500000||a<-500000){
cout<<"The number is out of bond!";
}
else{ if(a>=0)b[0]=0;
else b[0]=1;
while(a!=0){
b[count] = a %2;
a = a/2;
count++;
}
count--;
c[0] = b[0];
for(count;count>=1;count--){
c[i] = b[count];
i++;
}
cout<<c<<endl;

}



}
本人是菜鸟中的菜鸟,对C++更是几乎不懂,但求高手赐教,我这程序问题出在哪儿,怎么改,感激不尽啊!
...全文
281 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgjxwl 2009-09-23
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

void main()
{
int a;
int count=1,i=31,j = 1;
int b[32] = {0};
int c[32] = {0};
cout <<"Please input a Integer number"<<endl;
cin>>a;
if(a>500000||a <-500000)
{
cout <<"The number is out of bond!";
}
else
{
if(a>=0)
b[0]=0;
else
{
b[0]=1;
a = -a;
}

while(a!=0)
{
b[count] = a %2;
a = a/2;
count++;
}

c[0] = b[0];
j = 1;
for(i = 31;j < count;--i,++j)
{
c[i] = b[j];
}
for(i = 0; i < 32; ++i)
{
cout<<c[i];
}
cout<<endl;
}
}


大致改了下。。。
wanjingwei 2009-09-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qiang_top 的回复:]
引用 2 楼 coderofvc 的回复:
你想干嘛。。。你把c里的每一位输出就可以了啊。。
请问一下,可不可以像string一样把char数组一下子全部输出?必须得用for循环一个一个输出吗?
[/Quote]
整型数组必须这样
qiang_top 2009-09-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 coderofvc 的回复:]
你想干嘛。。。你把c里的每一位输出就可以了啊。。
[/Quote]请问一下,可不可以像string一样把char数组一下子全部输出?必须得用for循环一个一个输出吗?
thy38 2009-09-23
  • 打赏
  • 举报
回复
不小心所致
CoderOfVC 2009-09-23
  • 打赏
  • 举报
回复
你想干嘛。。。你把c里的每一位输出就可以了啊。。
wanjingwei 2009-09-23
  • 打赏
  • 举报
回复

#include <iostream>;
using namespace std;

void main()
{
int a;
int count=1,i=1;
int b[20],c[20];
cout <<"Please input a Integer number";
cin>>a;
if(a>500000||a <-500000){
cout <<"The number is out of bond!";
}
else{ if(a>=0)b[0]=0;
else b[0]=1;
while(a!=0){
b[count] = a %2;
a = a/2;
count++;
}
count--;
c[0] = b[0];
for(count;count>=1;count--){
c[i] = b[count];
i++;
}
for(int j=0;j<i;j++)
cout <<c[j];

}



}

数组输出要循环一个个的输出

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧