JAVA栈实现计算器

本文介绍了如何使用JAVA的栈数据结构来实现一个简单的计算器。首先,通过链表实现了栈的基本操作,然后展示了如何运用栈来完成计算器的功能,包括对运算符的处理和计算表达式的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

栈实现计算器

如有错误欢迎指正!

一、链表实现栈

代码如下(示例):

class stack<T>{
    private singlelinked<T> singlelinked = new singlelinked<>();
    private int top = -1;

    public stack() {
    }
    //判断栈是否为空
    public boolean isNull(){
        return singlelinked.isNull();
    }
    //入栈push
    public void push(T value){
        top++;
        Node<T> node = new Node<>(top, value);
        singlelinked.add(node);
    }

    //出栈pop
    public T pop(){
        T value = singlelinked.del();
        return value;
    }

    //遍历栈
    public void list(){
        singlelinked.list();
    }

    /**
     * 判断当前字符是不是运算符
     * @param c1 字符
     * @return true:是 false:否
     */
    public boolean isoperator(char c1) {
        return c1 == '+' || c1 == '-' || c1 == '*' || c1 == '/';
    }

    /**
     * 获取栈顶的值
     * @return 栈顶值
     */
    public T peek() {
        T value = singlelinked.endvalue();
        return value;
    }

    public int operpriority(char ch) {
        if(ch == '*' || ch == '/'){
            return 1;
        }else if(ch == '+' || ch == '-'){
            return 0;
        }else{
            return -1;
        }
    }

    public int cal(int num1,int num2,char oper) {
        int res = 0;
        switch(oper){
            case '+':
                res = num1+num2;
                break;
            case '-':
                res = num2-num1;
                break;
            case '*':
                res = num1*num2;
                break;
            case '/':
                res = num2/num1;
                break;
            default:
                break;
        }
        return res;
    }
}

class singlelinked<T>{
    private Node<T> head = new Node<>(-1, (T)"");

    public singlelinked() {
    }

    public Node<T> getHead() {
        return head;
    }
    //判断链表是否为空
    public boolean isNull(){
        if (head.getNext() == null){
            return true;
        }else{
            return false;
        }
    }
    //向链表末尾插入数据
    public void add(Node<T> node) {
        if (head.getNext() == null){
            head.setNext(node);
            return;
        }
        Node<T> temp = head;
        while (true){
            if (temp.getNext() == null){
                break;
            }
            temp = temp.getNext();
        }
        temp.setNext(node);
        return;
    }

    //链表删除对应编号的数据
    public T del(){
        if (head.getNext() == null){
            throw new RuntimeException("没有数据");
        }
        Node<T> node = head;
        while (true){
            if (node.getNext().getNext() == null){
                break;
            }
            node = node.getNext();
        }
        T value = node.getNext().getNum();
        node.setNext(node.getNext().getNext());
        return value;
    }

    //遍历链表
    public void list(){
        if (head.getNext() == null){
            System.out.println("链表为空!");
            return;
        }
        Node<T> temp = head.getNext();
        while (true){
            System.out.println(temp);
            if (temp.getNext() == null){
                break;
            }
            temp = temp.getNext();
        }
    }

    //获取链表末尾的值
    public T endvalue(){
        if (head.getNext() == null){
            throw new RuntimeException("没有数据");
        }
        Node<T> node = head.getNext();
        while (true){
            if(node.getNext() == null){
                break;
            }
            node = node.getNext();
        }
        return node.getNum();
    }

}

class Node<T>{
    private int no;
    private T num;
    private Node<T> next;

    public void setNext(Node<T> next) {
        this.next = next;
    }

    public Node<T> getNext() {
        return next;
    }

    @Override
    public String toString() {
        return "[num="+num+"]";
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setNum(T num) {
        this.num = num;
    }

    public Node(int no, T num) {
        this.no = no;
        this.num = num;
    }

    public int getNo() {
        return no;
    }

  	public T getNum() {
        return num;
    }
}

二、实现计算器功能

代码如下(示例):

public static void main(String[] args){
 
        String express = "14/2-55+1-5+233-4";
        //数栈
        stack<Integer> numstack = new stack<>();
        //符号栈
        stack<Character> operstack = new stack<>();
        char c1 = ' ';
        int index = 0;//记录扫描的位置
        int num1 = 0;
        int num2 = 0;
        char oper = ' ';
        int res = 0;
        String keepnum = "";
        while (true){
            c1 = express.substring(index,index+1).charAt(0);
            if(operstack.isoperator(c1)){
                //判断当前操作符栈是否为空
                if(!operstack.isNull()){
                    if(operstack.operpriority(c1) <= operstack.operpriority(operstack.peek())){
                        num1 = numstack.pop();
                        num2 = numstack.pop();
                        oper = operstack.pop();
                        res = numstack.cal(num1,num2,oper);
                        numstack.push(res);
                        operstack.push(c1);
                    }else{
                        operstack.push(c1);
                    }
                }else {
                    operstack.push(c1);
                }
            }else{
                keepnum += c1;
                if(index == express.length()-1){
                    numstack.push(Integer.parseInt(keepnum));
                }else{
                    if(operstack.isoperator(express.substring(index+1,index+2).charAt(0))){
                        numstack.push(Integer.parseInt(keepnum));
                        keepnum = "";
                    }
                }
            }
            index++;
            if (index >= express.length()){
                break;
            }
        }

        while (true){
            if (operstack.isNull()){
                break;
            }
            num1 = numstack.pop();
            num2 = numstack.pop();
            oper = operstack.pop();
            res = numstack.cal(num1,num2,oper);
            numstack.push(res);
        }
        int res2 = numstack.pop();
        System.out.printf("表达式%s = %d",express,res2);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值