C++ Code
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<bits/stdc++.h>
#define MaxStackSize 1000
using namespace std;
typedef struct{
int stack[MaxStackSize];
int top;
}SeqStack;
void StackInitiate(SeqStack *S){
}
int StackNotEmpty(){
}
int StackPush(SeqStack *S,int *x){
if(S->top >= MaxStackSize){
cout<<"堆栈已满无法插入!\n";
return 0;
}else{
S->stack[S->top] = *x;
S->top ++;
return 1;
}
}
int StackPop(SeqStack *S,int *d){
if(S->top<=0){
return 0;
} else{
S->top--;
*d = S->stack[S->top];
return 1;
}
}
int stackTop(SeqStack *S,int *d){
if(S->top<=0){
cout<<"堆栈已空!\n";
return 0;
}else{
*d = S->stack[S->top-1];
return 1;
}
}
void toBinary(int s){
SeqStack *S = (SeqStack*)malloc(sizeof(SeqStack));
int t,i,q;
while(s!=0){
t = s%2;
s = s/2;
i = StackPush(S,&t);
if(i==0) return;
}
while(S){
if(!StackPop(S,&q)) return;
cout<<q;
}
}
int main(){
SeqStack *myS = (SeqStack*)malloc(sizeof(SeqStack));
StackInitiate(myS);
toBinary(123);
return 0;
}
|