#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef struct stack
{
int *top;
int *bottom;
}stack;
void Creat(stack &S)
{
S.top = new int[MAXSIZE];
S.bottom = S.top;
if(!S.top) {
cout << "未成功创建栈" << endl;
}
}
void CreatStack(stack &S, int e){
if (S.bottom-S.top != MAXSIZE) {
*(S.bottom) = e;
S.bottom++;
}
}
void PrintStack(stack S)
{
if(S.top == S.bottom) {
cout << "栈为空" << endl;
}
while(S.top != S.bottom) {
S.bottom--;
cout << *(S.bottom) << " ";
}
}
int main(){
stack S;
Creat(S);
int n;
cin >> n;
while (n != 0) {
CreatStack(S, n % 2);
n = n / 2;
}
PrintStack(S);
return 0;
}
栈 (输出二进制)
最新推荐文章于 2024-11-04 23:24:03 发布