[指点]自己写的把int型十进制数转换为相应的二进制数

编译环境:WIN-TC version 1.9.1 通过

//*******************************************************************
//* 程 序 名: Change.cpp
//* 作 者: a13007788
//* 编制时间: 2006年3月29日 V1.0
//* 主要功能: 十进制转换为二进制
//*******************************************************************
#include "stdio.h"
#include "conio.h"
main()
{
int Num = 0; //存放输入的整数
int Top = 0;
int s[16] = {0}; //存放二进制的数组
printf("Now,please input anumber:");
scanf("%d",&Num);
printf("%dD is equal ",Num);
//如果输入的数为负数时,置标志位为1
if(Num < 0)
{
s[15] = 1;
Num = -Num; //换算为正整数
}
//在要转换的数不为0的情况下,开始转换
while((Num != 1) && (Num != 0))
{
s[Top] = Num % 2; //转换结果放入数组中
Num = (int)(Num / 2); //显式调用强制类型转换为int
Top++;
}
if(Num != 0)s[Top] = 1; //结果不为0,则第1位定为1
Top = 15;
//输出转换后的二进制数
while(Top != -1)
{
printf("%d",s[Top]);
Top--;
}
printf("B");
getch();
}

希望给些指导

[此贴子已经被作者于2006-4-8 12:17:40编辑过]----------------解决方案--------------------------------------------------------

用itoa(decimal,buffer,2)就可以了,第一个是10进制数,第2个是字符叔祖

----------------解决方案--------------------------------------------------------

我的意思是想得到时间复杂度更小的算法。。。。

----------------解决方案--------------------------------------------------------

时间复杂度小就必须要占更多的内存,有简单的你不用

Num = (int)(Num / 2); //显式调用强制类型转换为int

(int)有必要吗,Num已经定义为INT类型,NUM/2可能是FLOAT类型吗?整数除法的结果是截去结果的小数部分

----------------解决方案--------------------------------------------------------

以下是引用拼命王在2006-4-8 12:59:00的发言:

时间复杂度小就必须要占更多的内存,有简单的你不用

Num = (int)(Num / 2); //显式调用强制类型转换为int

(int)有必要吗,Num已经定义为INT类型,NUM/2可能是FLOAT类型吗?整数除法的结果是截去结果的小数部分

这个我知道,只是为了给自己养个好习惯。有些事不写写怎么知道原理呢?你给的那个函数又是怎么转换的?

----------------解决方案--------------------------------------------------------

这个转换没有道理

而且如果你会把整型变量前加‘i'的话,更不用了

----------------解决方案--------------------------------------------------------

以下是引用abingchem在2006-4-8 17:48:00的发言:

这个转换没有道理

而且如果你会把整型变量前加‘i'的话,更不用了

听不懂你在说的什么?什么把整型变量前加“i”????

什么叫没有道理?没有道理却转换成功了哪又是为什么?

----------------解决方案--------------------------------------------------------

#include 
#include
typedef int SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
int InitStack(SqStack S)
{
S.base=(SElemType *)malloc (8*sizeof(SElemType));
if(!S.base){
printf("分配内存失败!");
exit(-1);
}
S.top=S.base;
S.stacksize=0;
return 1;
}
int Gettop(SqStack S,SElemType e)
{
if(S.top==S.base);
printf("是空栈!无法输出元素!");
e=*(S.top-1);
return 1;
}
int Push(SqStack S,SElemType e)
{
if(S.top-S.base>=S.stacksize){
printf("栈满!另分配空间");
S.base=(SElemType *)realloc(S.base,(8+7)*sizeof(SElemType));
if(!S.base){
printf("分配失败!");
exit(-1);
}
S.top=S.base+S.stacksize;
S.stacksize+=7;
}
*S.top++=e;
++S.stacksize;
return 1;
}
int pop(SqStack S,SElemType e)
{
if(StackEmpty(S))
return 0;
e=*--S.top;
--S.stacksize;
return 1;
}
int StackEmpty(SqStack S)
{
if(S.stacksize==0)
return 1;
return 0;
}
main()
{
SElemType Num,e ;
SqStack S;
InitStack(S);
printf("Now,please input anumber:");
scanf("%d",&Num);
printf("%dD is equal ",Num);
while(Num)
{
Push(S,Num % 2);
Num = Num / 2;
}
if(!StackEmpty){
pop(S,e);
printf("%d",e);
}
return 1;
}

哪位大哥给我看看这个程序调不出来!!

调了很久 就不行

----------------解决方案--------------------------------------------------------

晕死!首先,你的程序写得太难看。不知道你用的什么编译器,但是好点的起码都有自动的格式控制吧?你的代码写得太乱了……

其实错误很简单,还是多了一个“;”而已。

#include 
#include
typedef int SElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
int InitStack(SqStack S)
{
S.base=(SElemType *)malloc (8*sizeof(SElemType));
if(!S.base)
{
printf("分配内存失败!");
exit(-1);
}
S.top=S.base;
S.stacksize=0;
return 1;
}
int Gettop(SqStack S,SElemType e)
{
if(S.top==S.base);//这个多了个“;”
printf("是空栈!无法输出元素!");
e=*(S.top-1);
return 1;
}
int Push(SqStack S,SElemType e)
{
if(S.top-S.base>=S.stacksize)
{
printf("栈满!另分配空间");
S.base=(SElemType *)realloc(S.base,(8+7)*sizeof(SElemType));
if(!S.base)
{
printf("分配失败!");
exit(-1);
}
S.top=S.base+S.stacksize;
S.stacksize+=7;
}
*S.top++=e;
++S.stacksize;
return 1;
}
int pop(SqStack S,SElemType e)
{
if(StackEmpty(S))
return 0;
e=*--S.top;
--S.stacksize;
return 1;
}
int StackEmpty(SqStack S)
{
if(S.stacksize==0)
return 1;
return 0;
}
main()
{
SElemType Num,e ;
SqStack S;
InitStack(S);
printf("Now,please input anumber:");
scanf("%d",&Num);
printf("%dD is equal ",Num);
while(Num)
{
Push(S,Num % 2);
Num = Num / 2;
}
if(!StackEmpty)
{
pop(S,e);
printf("%d",e);
}
return 1;
}

行了,调试成功。

----------------解决方案--------------------------------------------------------

我的结果是这样[IMG]C:\Documents and Settings\long\桌面\cuowu.bmp[/IMG]

----------------解决方案--------------------------------------------------------