栈实现十进制转化为其它进制java

使用到的类:

  • 线性表类:LinearList
  • 顺序栈类:SeqStack ,栈接口:Stack
  • 进制转化类:TransformDecimal
  • 主类

  • 这里栈是用线性表存储数据的,而线性表的存储本质就是数组

运行结果:

在这里插入图片描述


1. 进制转化类

package Class.Stack.DecimalTrans;

import Class.LinearList.LinearList;
import Class.Stack.SeqStack.SeqStack;

public class TransformDecimal {
    private SeqStack seqStack;  // 初始的空栈

    public TransformDecimal(int length) {
        LinearList linearList = new LinearList(length);
        SeqStack seqStack = new SeqStack(linearList);
        this.seqStack = seqStack;
    }

    public String hexadecimal(int number) {
        /*
            65        66
            A --> 10   B --> 11  C --> 12 ....
         */
        int target = 65;
        int accumulate = 0;
        while (number > target - 55) {
            target++;
            accumulate++;
        }
        char result = (char) ('A' + accumulate);
        return "" + result;
    }

    public StringBuffer transform(int beTran, int radix) {
        StringBuffer str = new StringBuffer("");  // 使用字符串输出最终结果
        int remainder; // 余数
        int result = beTran; // 每次的结果
        while (result != 0) {
            int temp = result;
            result = result / radix;
            remainder = temp % radix;
            seqStack.push(remainder);
        }
        // 很容易犯错的地方:栈会随着pop进行而减小,导致循环错误
        int times = seqStack.length();
        for (int i = 0; i < times; i++) {
            int temp = seqStack.pop();
            if (temp >= 10) {
                str.append(this.hexadecimal(temp));
            } else {
                str.append(temp);
            }
        }
        return str;

    }

}


2. 线性表类

在进制转化中并不是所有的方法都用到了

package Class.LinearList;

public class LinearList {
    public int length = 0;
    private int[] arr;
    private static final int MIN_CAPACITY = 3;

    // 构造方法一
    public LinearList(int length) {
        if (length >= MIN_CAPACITY) {
            arr = new int[length];
        } else {
            System.out.println("要求线性表最小长度为3!");
        }
    }

    // 构造方法二
    public LinearList() {
        this(MIN_CAPACITY);
    }
    // 构造方法三
    public LinearList(int []arr) {
        if(arr.length <= MIN_CAPACITY) {
            throw new RuntimeException("队列长度过小!");
        }
        this.arr = arr;
        length = arr.length;
    }

    public int getCapacity() {
        return arr.length;
    }

    //判断线性表是否为空
    public boolean isEmpty() {
        return length == 0;
    }

    // 判断线性表是否为满
    public boolean isFull() {
        return length == arr.length;
    }

    // 返回线性表长度
    public int size() {
        return length;
    }

    // 返回首个与key相等的元素的索引,查找不成功,返回null(这里是0)
    public int keySearch(int key) {
        if (isEmpty()) {
            throw new RuntimeException("线性表为空!");
        }
        for (int i = 0; i < length; i++) {
            if (arr[i] == key) {
                return i;
            }
        }
        System.out.println("线性表中没有该元素");
        return 0;
    }

    // 删除首个与key相等的元素的
    public void keyRemove(int key) {
        if (isEmpty()) {
            System.out.println("线性表为空!");
            return;
        }
        for (int i = 0; i < length; i++) {
            if (arr[i] == key) {
                arr[i] = 0;
                length--;
                return;
            }
        }
        System.out.println("线性表中没有该元素");
    }

    // 插入value,作为第n个元素
    public void insert(int value, int n) {
        if (isFull()) {
            System.out.println("线性表已满");
            return;
        }
        // 插入位置
        if (n >= 1 && n <= length + 1) {
            for (int i = length; i >= n; i--) {
                arr[i] = arr[i - 1];
            }
            arr[n - 1] = value;
        } else if (n > length + 1) {
            System.out.println("插入位置过大,自动插入到尾部");
            this.insert(value, length + 1);
        } else if (n < 1) {
            System.out.println("插入位置过小,自动插入到头部");
            this.insert(value, 1);
        }
        length++;
    }

    // 在尾部插入value元素
    public void tailInsert(int value) {
        this.insert(value, length + 1);
    }

    // 删除第n个元素
    public int remove(int n) {
        if (isEmpty()) {
            throw new RuntimeException("线性表为空!");
        }
        if (n >= 1 && n <= length) {
            int temp = arr[n - 1];
            for (int i = n - 1; i <= length - 2; i++) {
                arr[i] = arr[i + 1];
            }
            arr[length - 1] = 0;
            length--;
            return temp;
        } else {
            throw new RuntimeException("删除位置有误");
        }
    }

    // 顺序栈需求--->删除最后的元素(栈的顶部)并返回
    public int tailPop() {
        int tail = remove(length);
        return tail;
    }

    // 设置第n个元素为value
    public void setValue(int value, int n) {
        if (isEmpty()) {
            System.out.println("当前线性表为空");
        }
        if (n > length || n < 1) {
            System.out.println("访问越界");
            return;
        } else {
            arr[n - 1] = value;
        }
    }

    //
    public void showInfo() {
        for (int i = 0; i < length; i++) {
            System.out.print(arr[i] + "\t");
        }
        System.out.println("");
    }

    //
    public int getInt(int index) {
        return arr[index];
    }

}


3. 栈接口和线性栈类

基于上面的线性表类

package Class.Stack.SeqStack;

import Class.LinearList.LinearList;
import Class.Stack.Stack;

// 顺序栈, 即用顺序表存储元素, 使用前面顺序表中编好的方法.
public class SeqStack implements Stack {
    private LinearList linearList;

    public SeqStack(LinearList linearList) {
        this.linearList = linearList;
    }

    // 栈是否为空-->顺序表是否为空
    @Override
    public boolean isEmpty() {
        return linearList.isEmpty();
    }
    // 入栈--->往顺序表尾端插入数据
    @Override
    public void push(int num) {
        linearList.tailInsert(num);
    }
    // 出栈--->删除顺序表尾端元素并返回
    @Override
    public int pop() {
        return linearList.tailPop();
    }
    // 查看顶端元素--->查看顺序表尾端元素
    @Override
    public int peek() {
        return linearList.getInt(linearList.length - 1);
    }

    public int length() {
        return linearList.size();
    }
}

package Class.Stack;

public interface Stack {
    public abstract boolean isEmpty();
    public abstract void push(int num);  // 入栈
    public abstract int pop(); // 出栈, 并得到该元素
    public abstract int peek(); // 返回顶端元素
}


4. 主类

package Class.Stack.DecimalTrans;

public class Main {
    public static void main(String[] args) {
        TransformDecimal tool = new TransformDecimal(10);
        System.out.println("把131转化为8进制为:" + tool.transform(131, 8));
        System.out.println("把34转化为2进制为:" + tool.transform(34, 2));
        System.out.println("把10转化为6进制为:" + tool.transform(10, 6));
        System.out.println("把241转化为16进制为:" + tool.transform(241, 16));
        System.out.println("把167转化为16进制为:" + tool.transform(160, 16));
    }
}

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值