simpletron.java

来自「一个用JAVA做的」· Java 代码 · 共 229 行

JAVA
229
字号
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package srtpv02;import java.io.*;import java.util.*;import java.util.regex.Pattern;/** * * @author Administrator */public class Simpletron {    private static Simpletron instance;        private Simpletron() {        System.out.println("Welcome to Simpletron");    }        public static Simpletron GetInstance() {        if( instance == null ) {            instance = new Simpletron();        }                return instance;    }    public void run() throws Exception {        readCode(new Scanner(System.in));        execute();    }        public void run (String fileName) throws FileNotFoundException ,Exception {        readCode(new Scanner(new File(fileName + ".code")));        readMemory(new Scanner(new File(fileName + ".mem")));        readSMemory(new Scanner(new File(fileName + ".smem")));        execute();    }        private void readCode(Scanner sc) throws Exception {        for(int count=0;count<100;++count) {            codeMemory[count] = sc.nextInt();        }    }    private void readMemory(Scanner sc) throws Exception {        for(int count=0;count<100;++count) {            dataMemory[count] = sc.nextInt();        }    }    private void readSMemory(Scanner sc) throws Exception {        sc.useDelimiter(Pattern.compile("\\n"));        for(int count=0;count<50;++count) {            stringMemory[count] = sc.next();        }    }        private void execute() throws Exception {        int instructionRegister;        int codeCount = 0;        int stringCount = 0;        int operationCode = 0;        int operand = 0;        int accumulator = 0;             //作为数据的寄存器        String sAccumulator = null;      //作为字符串的寄存器        boolean flag = true;        Stack<Integer> nextIP = new Stack<Integer>();        BufferedReader stdin = new BufferedReader(                new InputStreamReader(System.in));        while(flag) {            instructionRegister = codeMemory[codeCount];            operationCode = instructionRegister / 100;            operand = instructionRegister % 100;                        switch (operationCode) {                case READ:                    String s = stdin.readLine();                    dataMemory[operand] = Integer.parseInt(s);                    break;                case READSTRING:                    s = stdin.readLine();                    stringMemory[operand] = s;                    break;                case STORE:                    dataMemory[operand] = accumulator;                    break;                case STORESTRING:                    stringMemory[operand] = sAccumulator;                    break;                case WRITE:                    System.out.println(dataMemory[operand]);                    break;                case WRITESTRING:                    System.out.println(stringMemory[operand]);                    break;                case LOAD:                    accumulator = dataMemory[operand];                    break;                case LOADSTRING:                    sAccumulator = stringMemory[operand];                    break;                case ADD:                    accumulator += dataMemory[operand];                    break;                case SUBTRACT:                    accumulator -= dataMemory[operand];                    break;                case DIVIDE:                    accumulator /= dataMemory[operand];                    break;                case MULTIPLY:                    accumulator *= dataMemory[operand];                    break;                case MOD:                    accumulator %= dataMemory[operand];                    break;                case POWER:                    accumulator = (int)Math.pow(accumulator, dataMemory[operand]);                    break;                case BRANCH:                    codeCount = operand;                    continue;                case BRANCHNEG:                    if( accumulator < 0 ) {                        codeCount = operand;                        continue;                    } else                         break;                case BRANCHNEGANDEQU:                    if( accumulator <= 0 ) {                        codeCount = operand;                        continue;                    } else                        break;                case BRANCHPOSANDEQU:                    if( accumulator >= 0 ) {                        codeCount = operand;                        continue;                    } else                        break;                case BRANCHPOS:                    if( accumulator > 0 ) {                        codeCount = operand;                        continue;                    } else                        break;                case BRANCHZERO:                    if( accumulator == 0 ) {                        codeCount = operand;                        continue;                    } else                         break;                case BRANCHNOTEQU:                    if( accumulator != 0 ) {                        codeCount = operand;                        continue;                    } else                        break;                case HALT:                    flag = false;                    break;                case GOSUB:                    nextIP.push(codeCount + 1);                    codeCount = operand;                    continue;                case RETURN:                    codeCount = nextIP.pop();                    continue;                default:                    throw new Exception("语法错误:" + instructionRegister);            }                        ++codeCount;                    }    }    private int[] dataMemory = new int[100];    private int[] codeMemory = new int[100];    private String[] stringMemory = new String[50];    private static final int READ = 10;             //读取到指定的内存单元    private static final int WRITE = 11;            //写到屏幕    private static final int READSTRING = 12;    private static final int WRITESTRING = 13;    private static final int LOAD = 20;             //将内存单元的内容装入累加器    private static final int STORE = 21;            //将累加器中内容放回指定内存单元    private static final int LOADSTRING = 22;    private static final int STORESTRING = 23;    private static final int ADD = 30;    private static final int SUBTRACT = 31;    private static final int DIVIDE = 32;    private static final int MULTIPLY = 33;    private static final int MOD = 34;    private static final int POWER = 35;    private static final int BRANCH = 40;    private static final int BRANCHNEG = 41;    private static final int BRANCHZERO = 42;    private static final int BRANCHPOSANDEQU = 44;    private static final int BRANCHNEGANDEQU = 45;    private static final int BRANCHPOS = 43;    private static final int BRANCHNOTEQU = 46;    private static final int HALT = 80;    private static final int GOSUB = 52;    private static final int RETURN = 53;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?