?? simpletron.java
字號:
/* * 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; //作為數(shù)據(jù)的寄存器 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; //讀取到指定的內(nèi)存單元 private static final int WRITE = 11; //寫到屏幕 private static final int READSTRING = 12; private static final int WRITESTRING = 13; private static final int LOAD = 20; //將內(nèi)存單元的內(nèi)容裝入累加器 private static final int STORE = 21; //將累加器中內(nèi)容放回指定內(nèi)存單元 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 + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -