首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

用栈实现数制转换 帮忙这个对不解决方法

2012-04-09 
用栈实现数制转换帮忙这个对不#include stdio.h#include stdlib.h#define TRUE 1#define FALSE 0#defi

用栈实现数制转换 帮忙这个对不
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0

#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef char ElemType;
typedef struct{
int *base;
int *top;
int stacksize;

}SqStack;

Status InitStack(SqStack &S){
S.base = (int *)malloc(STACK_INIT_SIZE * sizeof(int));
if(!S.base) exit (OVERFLOW);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;

}

Status GetTop(SqStack S,int &e){
if(S.top == S.base)
return ERROR;
e = *(S.top-1);
return OK;

}

Status Push(SqStack &S,int e){
if(S.top - S.base>= S.stacksize){
S.base = (int *) realloc (S.base ,(S.stacksize + STACKINCREMENT) * sizeof(int));
if(!S.base) exit (OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize +=STACKINCREMENT;
}
*S.top ++ = e;
return OK;

}

Status Pop(SqStack &S,int &e){
if(S.top ==S.base)
return OK;
e=*--S.top;
return ERROR;
}


Status StackEmpty(SqStack S){
if(S.top ==S.base)
return OK;
else
return ERROR;
}

void conversion(){
SqStack S;
int e;
InitStack(S);
scanf("%d",N);
while(N!=0){
Push(S,N % 2);
N= N / 2;

}


while(!StackEmpty(S)){
Pop(S,e);
printf("%d",e);
}
printf("\n");

}

void main(){
int x;
printf("输入要转换为二进制的整数:");
scanf("%d",&x);
printf("十进制%d转换为二进制后是:",x);
conversion();
printf("\n");

}


[解决办法]

C/C++ code
#include <stdio.h>#include <stdlib.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define OVERFLOW -2#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */typedef char ElemType;typedef struct{    int *base;    int *top;    int stacksize;}SqStack;Status InitStack(SqStack &S){    S.base = (int *)malloc(STACK_INIT_SIZE * sizeof(int));    if(!S.base) exit (OVERFLOW);    S.top = S.base;    S.stacksize = STACK_INIT_SIZE;    return OK;}Status GetTop(SqStack S,int &e){    if(S.top == S.base)        return ERROR;    e = *(S.top-1);    return OK;}Status Push(SqStack &S,int e){    if(S.top - S.base>= S.stacksize){        S.base = (int *) realloc (S.base ,(S.stacksize + STACKINCREMENT) * sizeof(int));        if(!S.base) exit (OVERFLOW);        S.top = S.base + S.stacksize;        S.stacksize +=STACKINCREMENT;    }    *S.top ++ = e;    return OK;}Status Pop(SqStack &S,int &e){    if(S.top ==S.base)        return OK;    e=*--S.top;    return ERROR;}Status StackEmpty(SqStack S){    if(S.top ==S.base)        return OK;    else        return ERROR;}void conversion(int N){    SqStack S;    int e;    InitStack(S);    //scanf("%d",N);    while(N!=0){        Push(S,N % 2);        N= N / 2;    }    while(!StackEmpty(S)){        Pop(S,e);        printf("%d",e);    }    printf("\n");}void main(){    int x;    printf("输入要转换为二进制的整数:");    scanf("%d",&x);    printf("十进制%d转换为二进制后是:",x);    conversion(x);    printf("\n");}
[解决办法]
C/C++ code
S.base = (int *) realloc (S.base ,(S.stacksize + STACKINCREMENT) * sizeof(int));if(!S.base) exit (OVERFLOW);
------解决方案--------------------


要传指针进去,代码如下:

[code=C/C++][/code]
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0

#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef char ElemType;
typedef struct{
int *base;
int *top;
int stacksize;

}SqStack;

Status InitStack(SqStack *S){
S->base = (int *)malloc(STACK_INIT_SIZE * sizeof(int));
if(!S->base) exit (OVERFLOW);
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
return OK;

}

Status GetTop(SqStack S,int e){
if(S.top == S.base)
return ERROR;
e = *(S.top-1);
return OK;

}

Status Push(SqStack *S,int e){
if(S->top - S->base>= S->stacksize){
S->base = (int *) realloc (S->base ,(S->stacksize + STACKINCREMENT) * sizeof(int));
if(!S->base) exit (OVERFLOW);
S->top = S->base + S->stacksize;
S->stacksize +=STACKINCREMENT;
}
*(S->top ++) = e;
return OK;

}

Status Pop(SqStack *S,int *e){
if(S->top ==S->base)
return OK;
*e=*(--S->top);
return ERROR;
}


Status StackEmpty(SqStack S){
if(S.top ==S.base)
return OK;
else
return ERROR;
}

void conversion(int N){
SqStack S;
int e;
InitStack(&S);
//scanf("%d",N);
while(N!=0){
Push(&S,N % 2);
N= N / 2;

}


while(!StackEmpty(S)){
Pop(&S,&e);
printf("%d",e);
}
printf("\n");

}

void main(){
int x;
printf("输入要转换为二进制的整数:");
scanf("%d",&x);
printf("十进制%d转换为二进制后是:",x);
conversion(x);
printf("\n");

}

热点排行