读书人

进制转化(栈)有关问题

发布时间: 2012-04-11 17:42:33 作者: rapoo

进制转化(栈)问题

C/C++ code
#include <stdio.h>#include <malloc.h>#include <stdlib.h>#define OK 0#define ERROR 1#define STOCKINT 100#define STOCKINCREASE 10//-----------------------------------------------typedef int Status;typedef int SElemType;typedef int ElemType;typedef struct {    SElemType * base;    SElemType * top;    int stocksize;}SqStack;//-------------------------------create functionStatus createstock( SqStack &S );               //create stockStatus pushstock( SqStack &S, int mode );            // push Status popstock( SqStack &S, int &div );                // pop Status destorystock( SqStack &S );                    // destoryStatus stockempty( SqStack &S );                    // is empty?void convert( SqStack &S );int main(){    SqStack S;    if( createstock( S ) == OK )        printf( "create success\n" );    else        exit( 0 );    convert( S );    return 0;}Status createstock( SqStack &S ){    S.base = ( SElemType * )malloc( sizeof(SElemType) * STOCKINT );    S.top = S.base;    S.stocksize = STOCKINT;    if( S.base )        return OK;    else        return ERROR;    }void convert( SqStack &S ){    int num, e;    puts( "now you are entering the function" );    printf( "please tell me a number (10 to 2):" );    scanf( "%d", &num );    while( num )    {        pushstock( S, num % 8);        num /= 8;//        printf( "%d", num( S ) );    }    printf( "after convert:");    while( stockempty( S ) )    {        popstock( S, e);        printf( "%d", e );        }}Status pushstock( SqStack &S, int mode )                        //push{    if( S.top - S.base >= S.stocksize )    {        S.base = ( SElemType * )realloc( S.base,             sizeof(SElemType) * (STOCKINCREASE + S.stocksize) );        if( !S.base )            exit( 0 );        S.top = S.base + S.stocksize;        S.stocksize += STOCKINCREASE;    }     *(S.top) = mode;        S.top ++;    return OK;}Status popstock( SqStack &S, int &div ){    if( S.top == S.base )        return ERROR;  //empty stack!!    div =  * S.top-- ;}Status destorystock( SqStack &S ){    free( S.base );    free( S.top );    S.stocksize = 0;    return OK;}Status stockempty( SqStack &S ){    if( S.top == S.base)    return OK;    else    return ERROR;    } 



急着交作业...我用断点调试发现,怎么余数怎么也进不了栈,希望各位帮下忙~~

[解决办法]

如需要阅读该回复,请登录或注册CSDN!

读书人网 >软件架构设计

热点推荐