2019届华为秋招笔试题第三题_仿LISP运算

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

1.题目描述

 LISP语言唯一的语法就是括号要配对。
 形如 (OP P1 P2 …),括号内元素由单个空格分割。
 其中第一个元素OP为操作符,后续元素均为其参数,参数个数取决于操作符类型
 注意:参数 P1, P2 也有可能是另外一个嵌套的 (OP P1 P2 …)
 当前OP类型为add/sub/mul/div(全小写),分别代表整数的加减乘除法。简单起见,所以OP参数个数为2
举例
  输入:(mul 3 -7)输出:-21
  输入:(add 1 2) 输出:3
  输入:(sub (mul 2 4) (div 9 3)) 输出 :5
  输入:(div 1 0) 输出:error
输入描述
  合法C字符串,字符串长度不超过512,用例保证了无语法错误
输出描述
  合法C字符串,字符包括’0’-‘9’及负号’-‘或者’error’

2.解题思路

按照题意模拟语法运算规则

3.代码

import java.util.Scanner;
import java.util.Stack;
public class Main_3 {
    public static void main(String[] args) {
        Scanner sr = new Scanner(System.in);
        String str = sr.nextLine();
        solution(str);
    }

    public static void solution(String str) {
        Stack<Integer> numStack = new Stack<>();
        Stack<String> operStack = new Stack<>();
        int mark = 0;
        int paramOne = 0;
        int paramTwo = 0;
        for(int i = 0;i<str.length();i++){
            char chas = str.charAt(i);
            if(chas == '('){
                //截取符号位
                operStack.push(str.substring(i+1,i+4));
                //这里为空格的索引位置
                i = i + 4;
                //符号位后第一个数字的索引坐标
                mark = i+1;
            }else if(chas == ')'){
                if(mark < i){
                    //所有数字的截取
                    numStack.push(Integer.valueOf(str.substring(mark,i)));
                    i++;
                    mark = i+1;
                }
                //得到一次()的对应,就进行一次计算
                paramOne = numStack.pop();
                paramTwo = numStack.pop();
                calc(numStack,operStack,paramOne,paramTwo);
            }else{
                //空格位将数字进行区分
                if(chas == ' '){
                    if(mark < i ){
                        numStack.push(Integer.valueOf(str.substring(mark,i)));
                        //下一个数字的索引为空格后面一位,故mark = i+1;
                        mark = i + 1;
                    }
                }
            }
        }
        //如果还有没计算完的,就进行再次计算
        while (!operStack.isEmpty()){
            paramTwo = numStack.pop();
            paramOne = numStack.pop();
            calc(numStack,operStack,paramOne,paramTwo);
        }
    }

    private static void calc(Stack<Integer> numStack, Stack<String> operStack, int paramOne, int paramTwo) {
        switch(operStack.pop()){
            case "add":
                numStack.push(paramOne + paramTwo);
                break;
            case "sub":
                numStack.push(paramOne - paramTwo);
                break;
            case "mul":
                numStack.push(paramOne * paramTwo);
                break;
            case "div":
                if(paramTwo == 0)
                    System.out.println("error");
                else
                    numStack.push(paramOne / paramTwo);
                break;
        }
    }
}

题目及代码整理自牛客技术资料
如有错误请指出!!

猜你喜欢

转载自blog.csdn.net/qq_17556191/article/details/95025782