Skip to content

Instantly share code, notes, and snippets.

@sdvcrx
Created August 17, 2013 14:40
Show Gist options
  • Save sdvcrx/6257202 to your computer and use it in GitHub Desktop.
Save sdvcrx/6257202 to your computer and use it in GitHub Desktop.
数制转换
#include <stdio.h>
#define MAXSIZE 25
#define OK 1
#define ERROR 0
typedef int Status;
typedef int SElemType;
typedef struct {
SElemType data[MAXSIZE];
int top;
} SqStack;
void InitStack(SqStack *S);
Status StackEmpty(SqStack *S);
Status StackFull(SqStack *S);
Status Push(SqStack *S, SElemType e);
Status Pop(SqStack *S, SElemType *e);
void MultiBaseOutput(int N, int B);
void InitStack(SqStack *S){
S->top = -1;
}
Status StackEmpty(SqStack *S){
return (S->top == -1);
}
Status StackFull(SqStack *S){
return (S->top == MAXSIZE - 1);
}
Status Push(SqStack *S, SElemType e){
if(StackFull(S))
return ERROR;
S->data[++S->top] = e;
return OK;
}
Status Pop(SqStack *S, SElemType *e){
if(StackEmpty(S))
return ERROR;
*e = S->data[S->top--];
return OK;
}
void MultiBaseOutput(int N, int B){
int i;
SqStack S;
InitStack(&S);
while(N){
Push(&S, N%B);
N = N / B;
}
while(!StackEmpty(&S)){
Pop(&S, &i);
printf ("%d", i);
}
printf("\n");
}
int main(void){
int a;
scanf("%d", &a);
MultiBaseOutput(a, 2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment