m0_57407186 2021-11-22 13:10 采纳率: 75%
浏览 50
已结题

用Java代码写出输入表达式就可以求出具体值的代码,用栈或者队列的形式

用Java代码写出输入表达式就可以求出具体值的代码,用栈或者队列的形式

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-11-22 13:18
    关注
    public class Stack<T> {
        //节点类
        public class Node{
            public T data;
            public Node next;
            public Node(){}
            public Node(T data,Node next){
                this.data = data;
                this.next = next;
            }
        }//Node
        
        public Node top = new Node();
        public int size;
        public Node oldNode;
        
        //入栈
        public void push(T element){
            top = new Node(element,top);
            size++;
        }
        
        //出栈
        public T pop(){
            oldNode = top;
            top = top.next;
            //oldNode = null;
            size--;
            return oldNode.data;
        }
        
        //返回栈顶对象的数据域,但不出栈
        public T peek(){
            return top.data;
        }
        
        //栈长
        public int length(){
            return size;
        }
        
        //判断栈是否为空
        public boolean isEmpty(){
            return size == 0;
        }
    
    }
    
    import java.util.Scanner;
    public class Test {
        public static final char[][] relation = {{'>','>','<','<','<','>','>'},
                {'>','>','<','<','<','>','>'},{'>','>','>','>','<','>','>'},
                {'>','>','>','>','<','>','>'},{'<','<','<','<','<','=','!'},
                {'>','>','>','>','!','>','>'},{'<','<','<','<','<','!','='}};
        
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            while(true){
            try{
            System.out.println("请输入要计算的表达式:");
            String exp = input.next();
            System.out.println(calc(exp + "#"));
            }catch(ArithmeticException e1){
                System.out.println("表达式中的分母不能为0");
                e1.printStackTrace();
            }
            }
        }
        /**
         * 
         * @param exp 要计算的表达式
         * @return 计算的结果
         */
        private static int calc(String exp) {
            //操作数栈
            Stack<Integer> num = new Stack<Integer>();
            //操作符栈
            Stack<Character> op = new Stack<Character>();
            
            op.push('#');
            int i = 0;
            char ch = exp.charAt(i);
            boolean flag = false;//判断连续的几个字符是否是数字,若是,就处理成一个数字。这样就能处理多位数的运算了。
            while(ch != '#' || op.peek() != '#') {//peek()查看栈顶对象但不移除。
                if(ch >= '0' && ch <= '9') {
                    if(flag) {
                        int tmp = num.pop();
                        num.push(tmp * 10 + Integer.parseInt(ch + ""));
                    } else {
                        num.push(Integer.parseInt(ch + ""));
                    }
                    flag = true;
                    i++;
                } else {
                    flag = false;
                    switch(precede(op.peek(), ch)) {
                    case '<':
                        op.push(ch);
                        i++;
                        break;
                    case '=':
                        op.pop();
                        i++;
                        break;
                    case '>':
                        int num2 = num.pop();
                        int num1 = num.pop();
                        int result = operate(num1, op.pop(), num2);
                        num.push(result);
                        break;
                    case '!':
                        System.out.println("输入的表达式错误!");
                        return -1;
                    }
                }
                ch = exp.charAt(i);
            }
            return num.peek();
        }
        private static char precede(char peek, char ch) {
            return relation[getIndex(peek)][getIndex(ch)];
        }
        
        /**
         * 
         * @param ch 操作符
         * @return 操作符的索引,按照+、-、*、/、(、)的顺序
         */
        private static int getIndex(char ch) {
            int index = -1;
            switch(ch) {
            case '+':
                index = 0;
                break;
            case '-':
                index = 1;
                break;
            case '*':
                index = 2;
                break;
            case '/':
                index = 3;
                break;
            case '(':
                index = 4;
                break;
            case ')':
                index = 5;
                break;
            case '#':
                index = 6;
                break;
            }
            return index;
        }
        
        /**
         * 
         * @param num1  第一个运算数
         * @param ch 运算符
         * @param num2 第二个运算数
         * @return 运算结果
         */
        private static int operate(int num1, char ch, int num2) {
            int result = 0;
            switch(ch) {
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num1 - num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num1 / num2;
                break;
            }
            return result;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月30日
  • 已采纳回答 11月22日
  • 创建了问题 11月22日

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)