思路分析:

只需要用一个变量来存储余数,并入栈即可。另外需要注意一下maxsize-1才是栈满;p.data[p.top]代表的是datatype x的值。 下面是代码实现:

#include <iostream>

using namespace std;
const int maxsize = 16;
typedef int datatype;

typedef  struct {
	datatype data[maxsize];
	int top;
}sqstack;

void init_sqstack(sqstack* sq) {
	sq->top = -1;
}

int empty_sqstack(sqstack* sq) {
	if (sq->top == -1)return 1;
	else return 0;
}

int push_sqstack(sqstack* sq, datatype x) {
	if (sq->top == maxsize - 1) {
		cout << "栈满,不能进栈!\n";
		return 0;
	}
	sq->data[++sq->top] = x;
	return 1;
}

int pop_sqstack(sqstack* sq, datatype* x) {
	if (sq->top == -1) {
		cout << "栈空,不能退栈!\n";
		return 0;
	}
	*x = sq->data[sq->top--];
	return 1;
}

int gettop_sqstack(sqstack* sq, datatype* x) {
	if (sq->top == -1) {
		cout << "栈空,不能退栈!\n";
		return 0;
	}
	*x = sq->data[sq->top];
	return 1;
}

void print(sqstack* sq, datatype* x) {
	while (sq->top != -1) {
		*x = sq->data[sq->top--];
		cout << *x;
	}
}

int  main() {
	int n,a;
	cin >> n;
	sqstack p;
	init_sqstack(&p);
	while (n!=0) {
		a = n % 2;
		n /= 2;
		push_sqstack(&p, a);
	}
	while (p.top != maxsize-1) {
		push_sqstack(&p, 0);
	}
	print(&p,&p.data[p.top]);
	return 0;
}