用栈来写把十进制数转化为二进制数
#include<iostream>#include<stdlib.h>
#define S_size 100
using namespace std;
typedef struct aa
{
int top;
int a[S_size];
}LinkStack;
void Initstack(LinkStack *S)
{//初始化栈
S->top=-1;
}
void Clearstack(LinkStack *S)
{//清空栈
S->top=-1;
}
void Pushstack(LinkStack *S,int *elem)
{//进栈
if(S->top==S_size-1)
{
cout<<"上溢出!"<<endl;
exit(1);
}
else
{
S->top++;
S->a[S->top]=*elem;
}
}
void Popstack(LinkStack *S,int *w)
{//出栈
if(S->top==-1)
{
cout<<"下溢出!"<<endl;
exit(1);
}
else
{
*w=S->a[S->top];
S->top--;
}
}
int Gettop(LinkStack *S)
{//取栈顶元素
if(S->top==-1)
{
cout<<"下溢出!"<<endl;
exit(1);
}
else
return S->a[S->top];
}
int Emptystack(LinkStack *S)
{//判断栈是否为空
if(S->top==-1)
return 1;
else
return 0;
}
void transformstack(long m)
{//转化为二进制数
LinkStack *P;
Initstack(P);
int *b,*c;
while(m!=0)
{
*b=m%2;
Pushstack(P,b);
m=m/2;
}
while(!Emptystack(P))
{
Popstack(P,c);
cout<<(*c);
}
cout<<"\n"<<endl;
}
int main()
{
long n;
cin>>n;
transformstack(n);
return 0;
}
//不知道为什么没有输出,求大神帮忙