用顺序栈将十进制变为二进制
#include <stdio.h> #include <stdlib.h> #define M 100 typedef struct { int data[M]; int top; }a; void InitStack(a *s) { s->top = -1; } int Push(a *s,int e) { if (s->top == M-1) { printf("栈满\n"); return 0; } s->top++; s->data[s->top]=e; return 1; } int Empty(a *s) { return(s->top==-1); } int Pop(a *s,int *e) { if(Empty(s)) { printf("\n a is free"); return 0; } *e=s->data[s->top]; s->top--; return 1; } void Conversion(int N) { int e; a *s = (a *)malloc(sizeof(a)); InitStack(s); while(N) { Push(s,N%2); N=N/2; } while(!Empty(s)) { Pop(s,&e); printf("%d",e); } } int main() { int m; printf("输入需要转换的数值: "); scanf("%d",&m); printf("转换为二进制值为: "); Conversion(m); printf("\n"); }