/*************************************stack.h********************************/ #define MAXSIZE 32 #define OVERFLOW -1 #define ERROR -2 #define DATATYPE int typedef enum{FALSE, TRUE} BOOL; typedef struct { DATATYPE * data; int top; }STACK; void initStack(STACK * ps) { ps->data = (DATATYPE *)malloc(MAXSIZE*sizeof(DATATYPE)); ps->top = -1; } BOOL empty(STACK * ps) { if (ps->top == -1) { printf("/nstack empty./007"); return TRUE; } return FALSE; } BOOL full(STACK * ps) { if (ps->top == MAXSIZE -1) { printf("stack full./007/n"); return TRUE; } return FALSE; } void push(STACK * ps, DATATYPE element) { if (!full(ps)) { ps->data[++ps->top] = element; } } DATATYPE pop(STACK *ps) { if (empty(ps)) { return OVERFLOW; } return ps->data[ps->top--]; } DATATYPE getTop(STACK * ps) { if (!empty(ps)) return ps->data[ps->top]; return ERROR; } void clearStack(STACK * ps) { ps->top = -1; } void destroy(STACK * ps) { free(ps->data); } /*****************************conver.c******************************/ #include <stdio.h> #include <stdlib.h> #include "stack.h" int main() { STACK stack; int num; int temp; initStack(&stack); printf("please input a number:"); scanf("%d", &num); while(num) { push(&stack, num%2); num = num/2; } while(!empty(&stack)) { printf("%d", pop(&stack)); } return 0; }
原文地址:http://hthinker.blog.51cto.com/5611549/1575153