//十进制转二进制输出(利用栈来实现)C代码(written by myself):
#include "stdio.h" int push(int*,int ,int*); int pop(int*,int*,int*); void exit(int );
void main(void) { static int a[20]; int top=0,b,t,receive[20],n=0,i; int *p=receive; while(1) { printf("请输入一个十进制的数:\n"); scanf("%d",&b); if(b==0) { printf("0\n"); continue; } while(b) { t=b%2; b=b/2; push(a,t,&top); } while(!(top==0)) { pop(a,p,&top); n++; p++; } for(i=0;i<n;i++) { printf("%d",receive); } printf("\n"); p-=n; n=0; } }
int pop(int *a,int *receive,int *top) { if(*(top)==0) { printf("栈空\n"); return 0; } else { (*top)--; *receive=*(a+*(top)); return 1; } }
int push(int *a,int n,int *top) { if((*top)==20) { printf("栈满\n"); return 0; } else { *(a+(*top))=n; (*top)++; return 1; } }
|