数据结构代码笔记:数值转换

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ET;

#define MAXSIZE 100
///栈/
//栈的存储结构
typedef struct stack
{
    ET *bottom;//栈底指针
    ET *top;//栈顶指针
    int stacksize;//栈可用最大容量
}stack;

//栈的初始化
Status Initstack(stack *S)
{
    S->bottom = (ET *)malloc(sizeof(ET)*MAXSIZE);
    S->top = S->bottom;
    S->stacksize = MAXSIZE;
    return OK;
}

//入栈
Status Push(stack *S, ET e)
{
    if(S->top - S->bottom == S->stacksize) return ERROR;
    *(S->top++) = e;
    return OK;
}

//出栈
Status Pop(stack *S, ET *e)
{
    if(S->bottom==S->top) return ERROR;
    *e = *(--S->top);
    return OK;
}

//取栈顶元素
ET GEttop(stack S)
{
    if(S.bottom!=S.top) return *(S.top-1);
}

//测试是否为空栈
Status Isempty(stack *S)
{
    if(S->bottom == S->top) return ERROR;
    else return OK;
}

//销毁栈
Status Destroystack(stack *S)
{
    if(S->bottom == NULL) return ERROR;
    //free(S->bottom);
    S->bottom=S->top=NULL;
    return OK;
}

//清空栈
Status Clearstack(stack *S)
{
    if(S->bottom==NULL) return ERROR;
    S->bottom = S->top;
    return OK;
}

//遍历栈
void Browsestack(stack S)
{
    int j = 0;
    ET *p;
    p = S.bottom;
    printf("the stack is ");
    while(p != S.top)
    {
        printf("%c ",*p);
        p++;
    }
    printf("\n");
}

int main(void)
{
    int num;
    printf("please input a decimal number:");
    scanf("%d",&num);

    stack S;
    Initstack(&S);
    while(num>=8)
    {
        Push(&S,num%8);
        num /= 8;
    }
    
    printf("%d",num);
    while(Isempty(&S))
    {
        ET e;
        Pop(&S,&e);
        printf("%d",e);
    }

    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值