读书人

数据结构顶用栈实现数制转换

发布时间: 2012-11-03 10:57:44 作者: rapoo

数据结构中用栈实现数制转换
我在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 10typedef 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语言

热点推荐