【机试题】语言解释器


前两天遇到的一道机试题,觉得挺有意思的,分享出来,大家可以在评论区中对代码予以改进,欢迎大家踊跃讨论😄

描述:

实现一个简单的语言解释器,支持以下指令:

指令格式描述
mov a v把数v赋值给a,其中a是变量名称,由不超过10个小写字母组成,v是变量名或者常数(常数为-10000~10000的整数)
inc a变量a加1
dec a变量a减1
jnz a v如果变量a的值不是0,则相对跳转v条指令。比如-2

输入保证最多有100个变量,100条语句;执行inc, dec和jnz之前,相应变量一定已经用mov赋值过。

输出执行完毕后各个变量的值。

样例输入1

mov bx 2
mov ax 5
inc bx
dec ax
jnz ax -2
dec ax

样例输出1

ax -1
bx 7

Coding:

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

/**
 * @author:eamon
 * @create:2022/3/7,18:47
 * @version:1.0
 */
public class Interpreter {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<String> objs = new ArrayList<>();

        boolean flag = true;
        System.out.println("开始输入(输入 end 结束):");

        while (flag) {
            String s = sc.nextLine();
            objs.add(s);

            if ("end".equals(s) || objs.size() >= 100) {
                flag = false;
            }
        }
        System.out.println(test(objs));
    }

    public static HashMap<String, Integer> test(List<String> objs) {
        boolean flag = true;

        int size = objs.size();
        int count = size;

        HashMap<String, Integer> map = new HashMap<>();
        while (flag) {

            String s = objs.get(size - count);
            String[] s1 = s.split(" ");

            if (s1[0].equals("mov")) {
                if (isInteger(s1[2])) {
                    map.put(s1[1], Integer.valueOf(s1[2]));
                } else {
                    String[] s2 = objs.get(0).split(" ");
                    map.put(s1[1], map.get(s2[1]));
                }
                count--;
            } else if (s1[0].equals("dec")) {
                Integer res = map.get(s1[1]);
                map.put(s1[1], res - 1);
                count--;
            } else if (s1[0].equals("inc")) {
                Integer res = map.get(s1[1]);
                map.put(s1[1], res + 1);
                count--;
            } else if (s1[0].equals("jnz")) {
                Integer res = map.get(s1[1]);
                if (res != 0) {
                    count = objs.size() - (size - count - Math.abs(Integer.valueOf(s1[2])));
                } else {
                    count--;
                }
            }
            if ("end".equals(s1[0])) {
                flag = false;
            }
        }
        return map;
    }

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

测试&结果:

开始输入(输入 end 结束):
mov bx 2
mov ax 5
inc bx
dec ax
jnz ax -2
dec ax
end
{bx=7, ax=-1}
  • 2
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进阶的小名

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值