zoukankan      html  css  js  c++  java
  • 【 数据结构(C语言)】栈的应用(一)数值转换

    1.十进制转换到d进制

    /**
    **  2017.11.4
    **  Ahthor:799
    **  数制转换
    **
    ***/
    #include <bits/stdc++.h>
    using namespace std;
    #define STACK_INIT_SIZE 100 /// 存储空间初始分配
    #define STACK_INCREMENT 10 /// 存储空间分配增量
    #define ElemType int
    #define Status int
    #define OK 1
    #define ERROR -1
    #define OVERFLOW -2
    typedef struct
    {
        ElemType *base;
        ElemType *top;
        int stacksize;
    } SqStack;
    Status InitStack (SqStack &S)
    {
        S.base = (ElemType * )malloc(STACK_INIT_SIZE * sizeof(ElemType));
        if (!S.base) exit(OVERFLOW);
        S.top = S.base;
        S.stacksize = STACK_INIT_SIZE;
        return OK;
    }
    Status DestroyStack(SqStack &S)
    {
        for (ElemType *index = S.base; index != S.top; index ++)
        {
            free(index);
        }
        free(&S);
        return OK;
    }
    Status ClearStack(SqStack &S)
    {
        for (ElemType *index = S.base+1; index != S.top; index ++)
        {
            free(index);
        }
        S.top = S.base;
        return OK;
    }
    Status IsEmptyStack(SqStack &S)
    {
        if (S.base == S.top) return true;
        else return false;
    }
    Status LengthStack(SqStack &S)
    {
        return S.top - S.base;
    }
    ElemType  GetTop(SqStack &S)
    {
        if (S.top == S.base) return ERROR;
        ElemType e = *(S.top - 1);
        return e;
    }
    Status Push(SqStack &S,ElemType &e)
    {
        if (S.top - S.base >= S.stacksize)
        {
            S.base = (ElemType *) realloc (S.base,(S.stacksize + STACK_INCREMENT) * sizeof(ElemType));
            if (!S.base)exit(OVERFLOW);
            S.top = S.base + S.stacksize;
            S.stacksize += STACK_INCREMENT;
        }
        *S.top = e;
        S.top ++;
        return OK;
    }
    Status Pop(SqStack &S, ElemType &e)
    {
        if (S.top == S.base) return ERROR;
        e = * --S.top;
        return OK;
    }
    Status TrverseStack(SqStack &S)
    {
        for (ElemType *index = S.base; index != S.top; index ++)
        {
            cout<<*index<<" ";
        }
        cout<<endl;
        return OK;
    }
    int main()
    {
        SqStack * St = (SqStack *) malloc(sizeof(SqStack));
        InitStack (*St);
        int n,d;
        cin>>n>>d;
        while (n)
        {
            int tmp = n%d;
            Push(*St,tmp);
            n /= d;
        }
        while (!IsEmptyStack(*St))
        {
            int num;
            Pop(*St,num);
            cout<<num;
        }
        cout<<endl;
        return 0;
    }
    
    
    
    
  • 相关阅读:
    Nginx开发从入门到精通
    Nginx配置文件(nginx.conf)配置详解
    转贝叶斯推断及其互联网应用(一):定理简介
    Vim 使用入门快捷键
    从贝叶斯定理说开去
    转特征值和特征向量
    第四章 特征值与特征向量
    numpy基础入门
    python range函数与numpy arange函数
    转悠望南山 Python闲谈(二)聊聊最小二乘法以及leastsq函数
  • 原文地址:https://www.cnblogs.com/sxy-798013203/p/7783111.html
Copyright © 2011-2022 走看看