Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
788 B
7
Indexable
Never
#include <iostream>
#define MAX_STACK 1000
using namespace std;

typedef struct {
	int info[MAX_STACK];
	int n;
} Stack;

void InitStack(Stack& S){
	S.n = 0;
}

bool IsFull(Stack S){
	return S.n == MAX_STACK;
}

bool IsEmpty(Stack S){
	return S.n == 0;
}

int Top(Stack S){
	if (IsEmpty(S)) return -1;
	return S.info[S.n-1];
}

void Push(Stack& S, int val){
	if(IsFull(S)) return;
	S.info[S.n] = val;
	S.n++;
}

int Pop(Stack& S){
	if(IsEmpty(S)) return -1;
	S.n--;
	return S.info[S.n];
}

void Change(Stack& S, int val){
	int m = val;
	int R, X;
	do{
		R = m%2;
		Push(S,R);
		m /= 2;
	}while(m != 0);
	
	while(!IsEmpty(S)){
		X = Pop(S);
		cout << X << " ";
	}
}

int main(){
	Stack s;
	InitStack(s);
	Change(s, 9);
	return 0;
}