#include<iostream>
#define Init 20
#define Increment 10
using namespace std;
struct stack
{
int *top;
int *base;
int initstack;
};
void Initstack(stack &s)//建立堆栈
{
s.base=(int*)malloc(Init*sizeof(int));
if(!s.base)cout<<"the stack of built is error! "<<endl;
s.top=s.base;
s.initstack=Init;
}
void Push(stack &s,int e)//入栈
{
if(s.top-s.base>=s.initstack)
{
s.base=(int *)realloc(s.base,sizeof(int)*(s.initstack+Increment));
if(!s.base)cout<<"restore is fail! "<<endl;
s.top=s.base+s.initstack;
s.initstack+=Increment;
}
*s.top++=e;
}
void Pop(stack &s)//出栈
{ int m;
if(s.top==s.base)cout<<"error!"<<endl;
m=*--s.top;
cout<<m<<endl;
}
bool Elemty(stack &s)//判断是否为空
{
if(s.top==s.base)return 0;
else return 1;
}
int main()
{ int num;
stack sta;
Initstack(sta);
cout<<"the number you want to convent: "<<endl;
cin>>num;
while(num!=0)
{
Push(sta,num%2);
num=num/2;
}
cout<<"output the convent number is: "<<endl;
while(Elemty(sta))
Pop(sta);
return 0;
}
#define Init 20
#define Increment 10
using namespace std;
struct stack
{
int *top;
int *base;
int initstack;
};
void Initstack(stack &s)//建立堆栈
{
s.base=(int*)malloc(Init*sizeof(int));
if(!s.base)cout<<"the stack of built is error! "<<endl;
s.top=s.base;
s.initstack=Init;
}
void Push(stack &s,int e)//入栈
{
if(s.top-s.base>=s.initstack)
{
s.base=(int *)realloc(s.base,sizeof(int)*(s.initstack+Increment));
if(!s.base)cout<<"restore is fail! "<<endl;
s.top=s.base+s.initstack;
s.initstack+=Increment;
}
*s.top++=e;
}
void Pop(stack &s)//出栈
{ int m;
if(s.top==s.base)cout<<"error!"<<endl;
m=*--s.top;
cout<<m<<endl;
}
bool Elemty(stack &s)//判断是否为空
{
if(s.top==s.base)return 0;
else return 1;
}
int main()
{ int num;
stack sta;
Initstack(sta);
cout<<"the number you want to convent: "<<endl;
cin>>num;
while(num!=0)
{
Push(sta,num%2);
num=num/2;
}
cout<<"output the convent number is: "<<endl;
while(Elemty(sta))
Pop(sta);
return 0;
}