数据结构顶用栈实现数制转换
数据结构中用栈实现数制转换
我在VS2012里用C语言写了一段用栈实现数制转换的代码,可是运行都最后总是不显示转换后的结果,麻烦各位大神帮忙找下一下问题出在了哪里。谢了!
#include <stdio.h>
#include <malloc.h>
using namespace std;
#define stack_init_size 100
#define stackincrement 10
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;
void InitStack(SqStack &S)
{
S.base=(int*)malloc(stack_init_size*sizeof(int));
S.top=S.base;
S.stacksize=stack_init_size;
}
int StackEmpty(SqStack S)
{
if(S.top=S.base)
return 1;
else
return 0;
}
void Push(SqStack &S,int e)
{
*S.top=e;
S.top++;
}
int Pop(SqStack &S,int &e)
{
if(!StackEmpty(S))
{
S.top--;
e=*S.top;
}
return 1;
}
void conversion(SqStack &S,int N)
{
while(N)
{
Push(S,N%8);
N=N/8;
}
while(!StackEmpty(S))
{
int e;
Pop(S,e);
printf("%d",e);
}
}
int main()
{
int N;
SqStack S;
scanf("%d",&N);
InitStack(S);
conversion(S,N);
}
------解决方案--------------------
我在VS2012里用C语言写了一段用栈实现数制转换的代码,可是运行都最后总是不显示转换后的结果,麻烦各位大神帮忙找下一下问题出在了哪里。谢了!
#include <stdio.h>
#include <malloc.h>
using namespace std;
#define stack_init_size 100
#define stackincrement 10
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;
void InitStack(SqStack &S)
{
S.base=(int*)malloc(stack_init_size*sizeof(int));
S.top=S.base;
S.stacksize=stack_init_size;
}
int StackEmpty(SqStack S)
{
if(S.top=S.base)
return 1;
else
return 0;
}
void Push(SqStack &S,int e)
{
*S.top=e;
S.top++;
}
int Pop(SqStack &S,int &e)
{
if(!StackEmpty(S))
{
S.top--;
e=*S.top;
}
return 1;
}
void conversion(SqStack &S,int N)
{
while(N)
{
Push(S,N%8);
N=N/8;
}
while(!StackEmpty(S))
{
int e;
Pop(S,e);
printf("%d",e);
}
}
int main()
{
int N;
SqStack S;
scanf("%d",&N);
InitStack(S);
conversion(S,N);
}
------解决方案--------------------
- C/C++ code
#include <iostream> using namespace std; #define stack_init_size 100 #define stackincrement 10 typedef struct { int *base; int *top; int stacksize; }SqStack; void InitStack(SqStack &S) { S.base=(int*)malloc(stack_init_size*sizeof(int)); S.top=S.base; S.stacksize=stack_init_size; } int StackEmpty(SqStack S) { if(S.top==S.base) return 1; else return 0; } void Push(SqStack &S,int e) { *S.top=e; S.top++; } int Pop(SqStack &S,int &e) { if(!StackEmpty(S)) { S.top--; e=*S.top; } return 1; } void conversion(SqStack &S,int N) { while(N) { Push(S,N%8); N=N/8; } while(!StackEmpty(S)) { int e; Pop(S,e); printf("%d",e); } } int main() { int N; SqStack S; scanf("%d",&N); InitStack(S); conversion(S,N); }