十进制数到8进制数的转换——java

import java.util.Scanner;

public class Main {
    private static int []notes = new int[10000];

    /*
    * 删除栈顶元素并返回
    * */
    private static int pop (Stact stact) {
        return notes[-- stact.top];
    }
    /*
    * top获取栈顶元素
    * */
    private static int top (Stact stact) {
        return notes[stact.top - 1];
    }
    /*
    * 向栈中添加一个元素
    * */
    private static void push (Stact stact, int e) {
        notes[stact.top ++] = e;
    }
    /*
    * 十进制数到8进制数的转换
    * */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt(); //要转化的数据
        int n = m;
        Stact stact = new Stact();   //创建一个栈
        while (n != 0) {
            push(stact,n % 8);
            n = n / 8;
        }
        System.out.print(m + "的八进制表示是:  ");
        while (stact.top != stact.base) {
            System.out.print(pop(stact));
        }
    }
    static class Stact {
        int base;
        int top;
        int stactSize;
        public Stact () {
            this.top = 0;
            this.base = 0;
            this.stactSize = notes.length;
        }
    }
}

发布了57 篇原创文章 · 获赞 55 · 访问量 1957

猜你喜欢

转载自blog.csdn.net/qq_40561126/article/details/104252319