資料結構-棧-進位制轉換
阿新 • • 發佈:2018-12-27
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; typedef struct sqstack{ int *base; int *top; int sizestack; }sqstack; void creat_stack(sqstack &a){ a.base=(int*)malloc(100*sizeof(int)); a.top=a.base; a.sizestack=100; } void push(sqstack &a,int e){ *a.top++=e; } void pop(sqstack &a){ if (a.base==a.top){ return ; }else { a.top--; } } int get_top(sqstack a){ if (a.top==a.base){ return -1; }else { return *(a.top-1); } } bool isempty(sqstack a){ if (a.base==a.top){return 1; }else { return 0; } } int main(){ sqstack a; creat_stack(a); int num; scanf("%d",&num); while(num!=0){ push(a,num%2); num/=2; } while(!isempty(a)){ printf("%d ",get_top(a)); pop(a); } return 0; }