zoukankan      html  css  js  c++  java
  • 栈——十进制转八进制

    顺序栈的基本操作

    //"SqStack.h"
    #include<iostream>
    using namespace std;

    #define SElemType int
    #define MAXSIZE 100

    typedef struct{
        SElemType *base;
        SElemType *top;
        int stacksize;
    }SqStack;

    string InitStack(SqStack &S){
        S.base = new SElemType[MAXSIZE];
        S.top = S.base;
        S.stacksize=MAXSIZE;
        return "OK";
    }

    string Push(SqStack &S,SElemType e){
        if(S.top-S.base == S.stacksize) return "ERROR";
        *S.top=e;
        S.top++;
        return "OK";
    }

    string pop(SqStack &S,SElemType &e){
        if(S.base == S.top) return "ERROE";
        S.top--;
        e = *S.top;
        return "OK";
    }

    SElemType GetTop(SqStack S){
        if(S.top != S.base){
            return *(S.top-1);
        }

    int StackEmpty(SqStack S){
        if(S.top == S.base) return 1;
        return 0;
    }
    #include<iostream>
    #include"SqStack.h"
    using namespace std;
    
    /*
        十进制转八进制
    */
    
    int main(){
    
        SqStack S ;
        InitStack(S);
    
        cout <<"Input the number to conver";
        int N;
        cin >> N;
    
      
    
        while(N){
            Push(S,N%8);                //将除8余数存进栈中
            N = N/8;
        }
        while (!StackEmpty(S))
        {
            SElemType e;
            pop(S,e);                   //按栈的顺序逐个输出  
            cout<<e;
        }
        system("pause");
        return 0;
    }
  • 相关阅读:
    蓝桥杯--算法训练 未名湖边的烦恼 (递归)
    hdoj--5606--tree(并查集)
    山东理工oj--1912--IP地址(水题)
    zzulioj--1634--Happy Thanksgiving Day
    45.java异常处理之抛出异常处理
    43.Java异常概要
    42.Java内部类
    41.Java特征之一多态
    40.Java接口
    39.Java值交换
  • 原文地址:https://www.cnblogs.com/LuMinghao/p/14002580.html
Copyright © 2011-2022 走看看