【面试题】语言解释器

41 篇文章 5 订阅
15 篇文章 0 订阅

问题:
在这里插入图片描述
在这里插入图片描述

/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.com.picc.flowable.draw.rest;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


import org.flowable.idm.api.IdmIdentityService;
import org.flowable.ui.common.model.GroupRepresentation;
import org.flowable.ui.common.model.RemoteGroup;
import org.flowable.ui.common.model.ResultListDataRepresentation;
import org.flowable.ui.common.service.idm.RemoteIdmService;
import org.flowable.ui.idm.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Rest resource for managing groups, used in the editor app.
 */
public class EditorGroupsResource {
    public static void main(String[] args) {
        List<String> scList = new ArrayList<>();
        Scanner sc = new Scanner(System.in);

        boolean scFlag = true;
        System.out.println("开始输入(输入end结束)");
        // 1、循环把输入的值存到集合中
        while(scFlag) {
            String str = sc.nextLine();
            scList.add(str);
            // 2、结束循环的条件
            if ("end".equals(str) || scList.size() >= 100){
                scFlag = false;
            }
        }
        // 输出结果
        System.out.println(sum(scList));
    }

    /**
     * 计算结果
     * @param scList
     * @return
     */
    private static HashMap<String, Integer> sum(List<String> scList){
        HashMap<String, Integer> hashMap = new HashMap<>();
        // 循环控制标志
        boolean flag = true;
        // 集合的长度
        int size = scList.size();
        // 记录集合剩余长度
        int count = size;

        while (flag) {
            String str = scList.get(size - count);
            String[] indexStr = str.split(" ");
            switch (indexStr[0]) {
                case "mov": {
                    // 说明是声明变量
                    if (isInteger(indexStr[2])) {
                        hashMap.put(indexStr[1], Integer.valueOf(indexStr[2]));
                    }else {
                        // 当下标为2的不是数值类型,就应该是 mov bx ax类型
                        String[] indexOne = scList.get(0).split(" ");
                        hashMap.put(indexStr[1], hashMap.get(indexOne[1]));
                    }
                    count--;
                    break;
                }
                case "dec": {
                    // 做减法
                    Integer num = hashMap.get(indexStr[1]);
                    hashMap.put(indexStr[1], num - 1);
                    count--;
                    break;
                }
                case "inc": {
                    // 做加法
                    Integer num = hashMap.get(indexStr[1]);
                    hashMap.put(indexStr[1], num + 1);
                    count--;
                    break;
                }
                case "jnz": {
                    // 跳转指令
                    Integer num = hashMap.get(indexStr[1]);
                    if (num == 0){
                        count--;
                    }else {
                        count = count + Math.abs(Integer.valueOf(indexStr[2]));
                    }
                    break;
                }
                default:
                    break;
            }
            if ("end".equals(indexStr[0])) {
                flag = false;
            }
        }
        return hashMap;
    }

    /**
     * 判断字符串中是否为数字
     *
     * @param str
     * @return
     */
    private static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^-?[0-9]+");
        return pattern.matcher(str).matches();
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值