qq_38284913 2017-04-12 15:21 采纳率: 100%
浏览 831
已采纳

能帮我看一下我的代码错哪了吗?

十进制正整数化为八进制
#include//2012
#include
#include /* malloc()等 /
#include /
INT_MAX等 /
#include /
EOF(=^Z或F6),NULL /
#include /
atoi() /
#include /
floor(),ceil(),abs() /
/
函数结果状态代码 /
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
typedef int Status; /
Status是函数的类型,其值是函数结果状态代码,如OK等 /
typedef int Boolean; /
Boolean是布尔类型,其值是TRUE或FALSE */

#define STACK_INIT_SIZE 10 /* 存储空间初始分配量 /
#define STACKINCREMENT 2 /
存储空间分配增量 /
typedef int SElemType; /
定义栈元素类型为整型 /
typedef struct SqStack
{
SElemType *base; /
在栈构造之前和销毁之后,base的值为NULL /
SElemType *top; /
栈顶指针 /
int stacksize; /
当前已分配的存储空间,以元素为单位 /
} SqStack; /
顺序栈 */

Status InitStack(SqStack S)
{
/
构造一个空栈S /
(*S).base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!(*S).base)
exit(OVERFLOW); /
存储分配失败 */
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return OK;
}

Status Push(SqStack S,SElemType e)
{
/
插入元素e为新的栈顶元素 /
if((*S).top-(*S).base>=(*S).stacksize) /
栈满,追加存储空间 /
{
(*S).base=(SElemType *)realloc((*S).base,((*S).stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!(*S).base)
exit(OVERFLOW); /
存储分配失败 */
(*S).top=(*S).base+(*S).stacksize;
(*S).stacksize+=STACKINCREMENT;
}
*((*S).top)++=e;
return OK;
}

Status Pop(SqStack S,SElemType *e)
{
/
若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR /
if((*S).top==(*S).base)
return ERROR;
*e=
--(*S).top;
return OK;
}

Status StackEmpty(SqStack S)
{
/* 若栈S为空栈,则返回TRUE,否则返回FALSE */
if(S.top==S.base)
return TRUE;
else
return FALSE;
}

void conversion(int n) /* 算法3.1 /
{
/
对于输入的任意一个非负十进制整数,打印输出与其等值的八进制数 /
SqStack s;
SElemType e;
InitStack(&s); /
初始化栈 /
while(n) /
当n不等于0 /
{
Push(&s,n%8); /
入栈n除以8的余数(8进制的低位) /
n=n/8;
}
while(!StackEmpty(s)) /
当栈不空 /
{
Pop(&s,&e); /
弹出栈顶元素且赋值给e /
printf("%d",e); /
输出e */
}
printf("\n");
}

int main()
{
int n;
while(~scanf("%d",&n))
{
conversion(n);
}
return 0;
}

  • 写回答

1条回答 默认 最新

  • qq_38284913 2017-04-12 15:28
    关注

    问错了,那是答案。下面才是
    #include//2012非负十进制化八进制
    #include
    #include//含有OVERFLOW的值
    #define OK 1
    #define ERROR 0
    #define STACK_INIT_SIZE 10//初始分配
    #define STACKINCRTMENT 2//增量
    typedef int Status;
    typedef struct SqStack
    {
    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 push(SqStack *S,int e)//入栈
    {
    if((*S).top-(*S).base>=(*S).stacksize)
    {
    (*S).base=(int *)realloc((*S).base,((*S).stacksize+STACKINCRTMENT)*sizeof(int));
    if(!(*S).base)
    exit(OVERFLOW);
    (*S).top=(*S).base+(*S).stacksize;
    (*S).stacksize+=STACKINCRTMENT;
    }
    *((*S).top)++=e;
    return 0;
    }

    Status pop(SqStack S,int *e)//出栈
    {
    if((*S).base==(*S).top)
    return ERROR;
    *e=
    --(*S).top;
    return OK;
    }

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

    void conversion(int n)//十化八
    {
    SqStack s;
    int e;
    InitStack(&s);
    while(n)
    {
    push(&s,n%8);
    n=n/8;
    }
    while(!StackEmpty(s))
    {
    pop(&s,&e);
    printf("%d",e);
    }
    printf("\n");
    }

    int main(void)
    {
    int n;
    while(scanf("%d",&n)!=EOF)
    {
    conversion(n);
    }
    return 0;
    }
    #include//2012非负十进制化八进制
    #include
    #include//含有OVERFLOW的值
    #define OK 1
    #define ERROR 0
    #define STACK_INIT_SIZE 10//初始分配
    #define STACKINCRTMENT 2//增量
    typedef int Status;
    typedef struct SqStack
    {
    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 push(SqStack *S,int e)//入栈
    {
    if((*S).top-(*S).base>=(*S).stacksize)
    {
    (*S).base=(int *)realloc((*S).base,((*S).stacksize+STACKINCRTMENT)*sizeof(int));
    if(!(*S).base)
    exit(OVERFLOW);
    (*S).top=(*S).base+(*S).stacksize;
    (*S).stacksize+=STACKINCRTMENT;
    }
    *((*S).top)++=e;
    return 0;
    }

    Status pop(SqStack S,int *e)//出栈
    {
    if((*S).base==(*S).top)
    return ERROR;
    *e=
    --(*S).top;
    return OK;
    }

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

    void conversion(int n)//十化八
    {
    SqStack s;
    int e;
    InitStack(&s);
    while(n)
    {
    push(&s,n%8);
    n=n/8;
    }
    while(!StackEmpty(s))
    {
    pop(&s,&e);
    printf("%d",e);
    }
    printf("\n");
    }

    int main(void)
    {
    int n;
    while(scanf("%d",&n)!=EOF)
    {
    conversion(n);
    }
    return 0;
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?