LeetCode刷题18-仿LISP运算

  1 package com.example.demo.leetcode.case202208;
  2 
  3 import java.util.Scanner;
  4 
  5 /**
  6  * 功能描述
  7  *
  8  * @author ASUS
  9  * @version 1.0
 10  * @Date 2022/8/7
 11  */
 12 public class Main2022080704 {
 13     /*
 14     仿lisp运算
 15     题目描述:
 16     LISP 语言唯一的语法就是括号要配对。 形如(OP P1 P2 …),括号内元素由单个空格分割。 其中第一个
 17     元素 OP 为操作符,后续元素均为其参数,参数个数取决于操作符类型 注意:参数 P1, P2 也有可能是另外
 18     一个嵌套的(OP P1 P2 …) 当前 OP 类型为 add / sub / mul / div(全小写),分别代表整数的加减乘除法
 19     简单起见,所有 OP 参数个数均为 2
 20     举例:
 21     输入:(mul 3 -7) 输出: -21
 22     输入:(add 1 2) 输出:3
 23     输入:(sub (mul 2 4) (div 9 3)) 输出:5
 24     输入:(div 1 0) 输出:error 题目涉及数字均为整数,可能为负;
 25     不考虑 32 位溢出翻转,计算过程中也不会发生 32 位溢出翻转 除零错误时,
 26     输出 “error”,除法遇除不尽,向下取整,即 3 / 2 = 1
 27     输入描述:
 28     输入为长度不超过 512 的字符串,用例保证了无语法错误
 29     输出描述:
 30     输出计算结果或者“error”
 31     示例 1
 32     输入:(div 12 (sub 45 45))
 33     输出:
 34     error
 35     */
 36 
 37     public static void main(String[] args) {
 38         // 输入信息
 39         Scanner scanner = new Scanner(System.in);
 40         String s = scanner.nextLine();
 41         System.out.println(calc(s));
 42     }
 43 
 44     // 获取一个表达式,返回其运算结果
 45     public static String calc(String s) {
 46         // 如果有括号那么首先拆分括号
 47         if (s.charAt(0) == '(') {
 48             while (s.charAt(0) == '(') {
 49                 // 取出括号内的具体内容
 50                 s = s.substring(1, s.length() - 1);
 51             }
 52         }
 53         // 是纯数字还是表达式还是error?
 54         if (s.equals("error"))
 55             return "error";
 56         // 是纯数字吗?正负数
 57         if ((s.charAt(0) >= '0' && s.charAt(0) <= '9') || (s.charAt(0) == '-'))
 58             return s;
 59         // 是表达式
 60         String op = s.substring(0, 3);
 61         String p1, p2;
 62         // p1有括号吗,有括号可能有空格,没括号必定没空格
 63         // 如果没括号
 64         int index = 4;
 65         if (s.charAt(index) != '(') {
 66             while (index < s.length() && s.charAt(index) != ' ') {
 67                 index++;
 68             }
 69         } else {
 70             // 左括号剩余个数
 71             int left_bracket_count = 0;
 72             while (true) {
 73                 if (s.charAt(index) == '(')
 74                     left_bracket_count++;
 75                 if (s.charAt(index) == ')')
 76                     left_bracket_count--;
 77                 index++;
 78                 if (left_bracket_count == 0)
 79                     break;
 80             }
 81         }
 82         p1 = s.substring(4, index);
 83         p2 = s.substring(index + 1);
 84         // 纯数字
 85         String p1_result = calc(p1);
 86         String p2_result = calc(p2);
 87         if (p1.equals("error") || p2.equals("error") || p1_result.equals("error") || p2_result.equals("error"))
 88             return "error";
 89         switch (op) {
 90             case "add":
 91                 return String.valueOf(Integer.parseInt(p1_result) + Integer.parseInt(p2_result));
 92             case "sub":
 93                 return String.valueOf(Integer.parseInt(p1_result) - Integer.parseInt(p2_result));
 94             case "mul":
 95                 return String.valueOf(Integer.parseInt(p1_result) * Integer.parseInt(p2_result));
 96             case "div":
 97                 if (Integer.parseInt(p2_result) == 0)
 98                     return "error";
 99                 return String.valueOf(Integer.parseInt(p1_result) / Integer.parseInt(p2_result));
100         }
101         return "error";
102     }
103 
104 }

 

posted @ 2022-08-07 14:01  chch213  阅读(176)  评论(0编辑  收藏  举报