目的:将输入的数字转为二进制,接着将转换后的元素入栈。全部入栈完再把元素一个个出栈,出栈的结果即为转换完的结果
我的问题:运行无报错,但输入数字运行没有结果
#include<stdio.h>
#include<malloc.h>
#include<math.h>
#include<string.h>
#include "process.h"
#define SIZE 100// 存储空间初始分配
#define STACKINCREMENT 10// 存储空间分配增量
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int SElemType;
typedef struct
{
SElemType *base;//栈底
int *top;//栈顶
int stacksize;//栈容量
} SqStack;
SqStack *S; //定义全局变量
//初始化栈
int InitStack(SqStack *S) {
S->base=(SElemType*)malloc(SIZE*sizeof(SElemType));
if(!S->base)exit(OVERFLOW);
S->top=S->base;
S->stacksize=SIZE;
return OK;
}
//入栈操作
void Push(SqStack *S, int e){
if(S->top-S->base>=S->stacksize){
S->base=(SElemType*)realloc(S->base,(S->stacksize+10)*sizeof(SElemType));
if(!S->base)exit (OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize+=10;
}
S->top=&e;
S->top++;
}
//判空操作
Status Stackempty(SqStack *S){
if(S->top==S->base) return ERROR;
return OK;
}
// 出栈操作
void Pop(SqStack *S){
SElemType *e;
if(S->top==S->base) printf("ERROR");
else *e=*--S->top ;
printf("%d",*e);
}
//数制转换函数
void conversion(SElemType N, SqStack *S){
InitStack(S);
while(N>=0){
Push(S,N%2);
N=N/2;}
while(!Stackempty(S)){
Pop(S);
}
}
//主函数
int main(){
SElemType N;
printf("输入要转换的数字N\n");
scanf("%d",&N);
printf("转换结果为:");
conversion(N,S);
return 0;
}