#include
#include
#include
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define ERROR 0
#define TRUE 1
typedef int ElemType;
typedef struct{
ElemType *base;
ElemType *top;
int Stacksize;
}sqStack;
ElemType e;
sqStack s;
void InitStack(sqStack *s)
{ s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if( !s->base )
{
exit(0);
}
s->top = s->base;
s->Stacksize = STACK_INIT_SIZE;
}
void Pop(sqStack *s,ElemType *e)
{
if( s->top == s->base ) { return; }
*e = *--(s->top); }
void Push(sqStack *s, ElemType e) {
if( s->top - s->base >= s->Stacksize ) { s->base = (ElemType *)realloc(s->base, (s->Stacksize + STACKINCREMENT)*sizeof(ElemType));
if( !s->base ) { exit(0); }
} *(s->top) = e; s->top++;
}
int StackEmpty(sqStack s)
{ if( s.top - s.base )
{ return ERROR; }
else { return TRUE; } }
int GetTop(sqStack *s,ElemType *e)
{
if( s->top - s->base )
{ return ERROR; }
*e = *s->top ++; return TRUE; }
void conversion(int N) { while(N) { Push(&s,N%2);
N=N/2;
}
getchar();
while( !StackEmpty(s) )
{ Pop(&s,&e);
printf("%d",e);
}
free(s.base);
} int main()
{
sqStack s;
ElemType e;
int N;
InitStack(&s);
scanf("%d",&N);
conversion(N);
}
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define ERROR 0
#define TRUE 1
typedef int ElemType;
typedef struct{
ElemType *base;
ElemType *top;
int Stacksize;
}sqStack;
void InitStack(sqStack *s){
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if( !s->base )
{
exit(0);
}
s->top = s->base;
s->Stacksize = STACK_INIT_SIZE;
}
ElemType Pop(sqStack *s)
{
if( s->top == s->base ) {
return;
}
return *--(s->top);
}
void Push(sqStack *s, ElemType e) {
if( s->top - s->base >= s->Stacksize ) {
s->base = (ElemType *)realloc(s->base, (s->Stacksize + STACKINCREMENT)*sizeof(ElemType));
if( !s->base ) {
exit(0);
}
}
*(s->top) = e;
s->top++;
}
int StackEmpty(sqStack s)
{
if( s.top - s.base ){
return ERROR;
}
else {
return TRUE;
}
}
int GetTop(sqStack *s,ElemType *e)
{
if( s->top - s->base )
{
return ERROR;
}
*e = *s->top ++;
return TRUE;
}
void conversion(int N) {
sqStack s;
InitStack(&s);
while(N) {
Push(&s,N%2);
N=N/2;
}
while( !StackEmpty(s) )
{
printf("%d",Pop(&s));
}
free(s.base);
}
int main()
{
sqStack s;
ElemType e;
int N;
InitStack(&s);
scanf("%d",&N);
conversion(N);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define ERROR 0
#define TRUE 1
typedef int ElemType;
typedef struct{
ElemType *base;
ElemType *top;
int Stacksize;
}sqStack;
void InitStack(sqStack *s){
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if( !s->base )
{
exit(0);
}
s->top = s->base;
s->Stacksize = STACK_INIT_SIZE;
}
ElemType Pop(sqStack *s)
{
if( s->top == s->base ) {
return;
}
return *--(s->top);
}
void Pop1(sqStack *s,ElemType e)
{
if( s->top == s->base ) {
return;
}
e = *--(s->top);
printf("%d",e);
}
void Push(sqStack *s, ElemType e) {
if( s->top - s->base >= s->Stacksize ) {
s->base = (ElemType *)realloc(s->base, (s->Stacksize + STACKINCREMENT)*sizeof(ElemType));
if( !s->base ) {
exit(0);
}
}
*(s->top) = e;
s->top++;
}
int StackEmpty(sqStack s)
{
if( s.top - s.base ){
return ERROR;
}
else {
return TRUE;
}
}
int GetTop(sqStack *s,ElemType *e)
{
if( s->top - s->base )
{
return ERROR;
}
*e = *s->top ++;
return TRUE;
}
void conversion(int N) {
sqStack s;
InitStack(&s);
while(N) {
Push(&s,N%2);
N=N/2;
}
ElemType e;
while( !StackEmpty(s) )
{
Pop1(&s,e);
//printf("second-------");
//printf("%d",Pop(&s));
}
free(s.base);
}
int main()
{
sqStack s;
ElemType e;
int N;
InitStack(&s);
scanf("%d",&N);
conversion(N);
return 0;
}
问题就出在pop1 ,,,