?? executor.java
字號:
str = str.substring(2);
strT = (str.length()==1)? "" : str.substring(1);
}
//以'-'號開始
else {
//計(jì)算第一個加減運(yùn)算符號位置
int location = -1;
for (int i = 1; i < str.length(); i++) {
if ("+-".indexOf(str.charAt(i))!=-1) {
location = i;
break;
}
}
//獲得第一個操作數(shù)的值
String value1 = str.substring(0, location);
int number1 = getNumber(value1, startIndex, endIndex);
//計(jì)算第二個操作數(shù)的開始位置
int end = str.length();
for (int i = location+2; i < end; i++) {
if ("+-".indexOf(str.charAt(i))!=-1) {
end = i;
break;
}
}
//獲得第二個操作數(shù)的值
String value2 = str.substring(location+1, end);
int number2 = getNumber(value2, startIndex, endIndex);
//計(jì)算加減法
int number = (str.charAt(location) == '+') ? number1 + number2 : number1 - number2;
//生成進(jìn)行一次加減法后的串
str = (end==str.length()) ? number + "": number + str.substring(end);
strT = (str.length()==1)? "" : str.substring(1);
}
}
return getNumber(str, startIndex, endIndex);
}
/** 計(jì)算關(guān)系表達(dá)式的值
* 語法分析已保證格式正確
* String str 為關(guān)系表達(dá)式字符串*/
private boolean computeRelation (String str, int startIndex,
int endIndex) throws MyRuntimeException {
//將real替換為"#"
str = str.replace("real", "#");
/** 循環(huán)消去括號*/
while (str.indexOf('[')+str.indexOf('(') != -2) {
//查找第一個括號開的位置
for (int i = 0; i <str.length(); i++) {
if (str.charAt(i)=='[' || str.charAt(i)=='(') {
/** 第一個括號開為中括號*/
if (str.charAt(i)=='[') {
//存儲數(shù)組變量名開始位置
int start = -1;
for (int j = i; j >=0; j--) {
//查找變量開始位置
if ("+-*/<>=".indexOf(str.charAt(j))!=-1) {
start = j;
break;
}
}
//獲得的數(shù)組變量名
String var = str.substring(start+1, i);
//錯誤:變量未聲明
if (!isDeclared(var)) throw new MyRuntimeException(
"變量 “" + var + "” 使用前未聲明", startIndex, endIndex);
//錯誤:變量聲明為int型
if (!isArrDeclared(var)) throw new MyRuntimeException(
"對int型變量 “" + var + "” 使用索引", startIndex, endIndex);
//計(jì)算對應(yīng)括號閉位置
int endBracket = SyntaxAnalyze.analyzeBracket(str, '[', ']', i);
//遞歸計(jì)算索引值
int index = computeArithmetic(
str.substring(i+1, endBracket), startIndex, endIndex);
//錯誤:索引值越界
if (index < 0 || index >= getArrVar(var).length)
throw new MyRuntimeException("數(shù)組索引越界: " +
"數(shù)組變量 “" + var + "”的長度:" + getArrVar(var).length +
" 錯誤的索引:" + index, startIndex, endIndex);
//獲取變量的值
int value = getArrVar(var).getValue(index);
//生成進(jìn)行一次消括號后的串
String strT1 = (start==-1) ? "" : str.substring(0, start+1);
String strT2 = (endBracket==str.length()-1) ?
"" : str.substring(endBracket + 1, str.length());
str = strT1 + value + strT2;
}
/** 第一個括號開為小括號*/
else {
//計(jì)算對應(yīng)括號閉位置
int endBracket = SyntaxAnalyze.analyzeBracket(str, '(', ')', i);
String strT = str.substring(i+1, endBracket);
String value = "";
//小括號內(nèi)返回值為布爾類型
if (strT.indexOf('>')+strT.indexOf('<')+
strT.indexOf('=')+strT.indexOf('#') != -4 )
//獲取小括號內(nèi)布爾值,分別使用"#"、"!"表示返回值的true或false
value += (computeRelation(strT, startIndex, endIndex)) ? "#" : "!";
//小括號內(nèi)返回值為數(shù)字
else
//計(jì)算小括號內(nèi)的值
value += computeArithmetic(
str.substring(i+1, endBracket), startIndex, endIndex);
//生成進(jìn)行一次消括號后的串
String strT1 = (i==0) ? "" : str.substring(0, i);
String strT2 = (endBracket==str.length()-1) ?
"" : str.substring(endBracket+1, str.length());
str = strT1 + value + strT2;
}
break;
}
}
}
if (str.equals("!") || str.equals("!==#") || str.equals("#==!") ||
str.equals("!<>!") || str.equals("#<>#")) return false;
else if (str.equals("#") || str.equals("!<>#")||str.equals("#<>!") ||
str.equals("!==!") || str.equals("#==#")) return true;
/** 不含有"#"和"!"的情況*/
else if (str.contains("==") || str.contains("<>")) {
//計(jì)算關(guān)系運(yùn)算符所在位置
int locationT = str.indexOf('<');
int location = (locationT > 0) ? locationT : str.indexOf('=');
//獲取兩個用于比較的值
int number1 = computeArithmetic(str.substring(0, location), startIndex, endIndex);
int number2 = computeArithmetic(
str.substring(location+2, str.length()), startIndex, endIndex);
//進(jìn)行比較
switch (str.charAt(location)) {
case '<': return number1 != number2;
case '=': return number1 == number2;
}
}
else {
//計(jì)算關(guān)系運(yùn)算符所在位置
int locationT = str.indexOf('<');
int location = (locationT > 0) ? locationT : str.indexOf('>');
//獲取兩個用于比較的值
int number1 = computeArithmetic (str.substring(0, location), startIndex, endIndex);
int number2 = computeArithmetic (
str.substring(location+1, str.length()), startIndex, endIndex);
//進(jìn)行比較
switch (str.charAt(location)) {
case '>': return number1 > number2;
case '<': return number1 < number2;
}
}
return false;
}
/** 獲取數(shù)值
* @param str 變量名(可能含有負(fù)號)或數(shù)字的字符串
* @param startIndex 輸入中的開始位置
* @param endIndex 輸入中的結(jié)束位置
* @return 字符串表示的數(shù)字
* @throws MyRuntimeException
*/
private int getNumber (String str, int startIndex, int endIndex) throws MyRuntimeException {
int value = 0;
//檢查是否數(shù)字
try {
value = Integer.parseInt(str);
}
//待獲取為變量
catch (Exception e) {
//是否以'-'開頭
String strT = (str.charAt(0)=='-') ? str.substring(1) : str;
//檢查變量是否已聲明
if (!isDeclared(strT))
throw new MyRuntimeException("變量 “" + strT + "” 使用前未聲明", startIndex, endIndex);
//檢查變量是否聲明為int型
if (isIntDeclared(strT)) {
//檢查變量是否已初始化
if (isEvaluated (strT))
value = (str.charAt(0)=='-') ? -getIntVar(strT) : getIntVar(strT);
else throw new MyRuntimeException(
"變量 “" + strT + "” 使用前未初始化", startIndex, endIndex);
}
//變量聲明為數(shù)組型
else throw new MyRuntimeException(
"在運(yùn)算表達(dá)式中直接使用了數(shù)組變量 “" + strT + "”", startIndex, endIndex);
}
return value;
}
/** 使用具體值對數(shù)組進(jìn)行賦值
* String str 為數(shù)組賦值(或初始化)使用的值*/
private ArrayList<Integer> computeArrayData(String variable, String str, boolean evaluate,
int startIndex, int endIndex)
throws MyRuntimeException {
ArrayList<Integer> arrayData = new ArrayList<Integer>();
while (str.indexOf(',')!=-1) {
int end = str.indexOf(',');
if (end == 0) {
if (evaluate){
arrayData.add(getArrVar(variable).value[arrayData.size()]);
}
else arrayData.add(0);
}
else arrayData.add(computeArithmetic(str.substring(0, end), startIndex, endIndex));
str = str.substring(end+1);
}
arrayData.add(computeArithmetic(str, startIndex, endIndex));
return arrayData;
}
/** 檢查變量是否已聲明*/
//String variable 為變量名,以下所有方法中相同
private boolean isDeclared (String variable) {
if (isIntDeclared(variable) || isArrDeclared(variable)) return true;
return false;
}
/** 檢查int型變量是否已聲明*/
private boolean isIntDeclared (String variable) {
for (Variable value:intList)
if (value.name.equals(variable)) return true;
return false;
}
/** 檢查數(shù)組變量是否已聲明*/
private boolean isArrDeclared (String variable) {
for (ArrayVariable value:arrList)
if (value.name.equals(variable)) return true;
return false;
}
/** 獲取數(shù)組變量對象*/
private ArrayVariable getArrVar (String variable) {
for (ArrayVariable value:arrList)
if (value.name.equals(variable)) return value;
return new ArrayVariable(variable, -1);
}
/** 獲取int變量對象*/
private int getIntVar (String variable) {
for (Variable value:intList)
if (value.name.equals(variable)) return value.value;
return 0;
}
/** 檢查int型變量是否已初始化*/
private boolean isEvaluated (String variable) {
for (Variable value:intList)
if (value.name.equals(variable)) return value.evaluated;
return false;
}
/** 設(shè)置int型變量值
* int value 為新變量值*/
private void setIntVarValue (String variable, int value) {
for (Variable var:intList)
if (var.name.equals(variable)) {
var.setValue(value);
break;
}
}
/** 設(shè)置array型變量值
* 將以ArrayList<Integer>類型保存的數(shù)組變量值賦值與變量名為variable的數(shù)組變量*/
private void setArrVarValue (String variable, ArrayList<Integer> valueList) {
for (ArrayVariable var:arrList)
if (var.name.equals(variable)) {
var.setValue(valueList);
break;
}
}
/** 設(shè)置array型變量值
* 將ArrayVariable型對象arrayVar賦值與變量名為variable的數(shù)組變量*/
private void setArrVarValue (String variable, ArrayVariable arrayVar) {
for (ArrayVariable var:arrList)
if (var.name.equals(variable)) {
var.setValue(arrayVar);
break;
}
}
/** 設(shè)置array型變索引index處的值
* int indexValue索引index處新的值*/
private void setArrIndexValue (String variable, int index, int indexValue) {
for (ArrayVariable var:arrList)
if (var.name.equals(variable)) {
var.setIndexValue(index, indexValue);
break;
}
}
/** 信息窗口
* 繼承JDialog并將屬性設(shè)為模式的以保證打開此窗口時只有關(guān)閉后才能進(jìn)行其他操作
* 實(shí)現(xiàn)ActionListener接口以響應(yīng)事件*/
class DialogAdapter extends JDialog implements ActionListener {
//提供信息的文件名
private String fileName = "";
//構(gòu)造方法
public DialogAdapter(int index) {
this(null, index);
}
//構(gòu)造方法
public DialogAdapter(JFrame parent, int index) {
//調(diào)用JDialog構(gòu)造方法,設(shè)置此窗口為模式的
super(parent, true);
String frameName = (index==1) ? "更新及使用說明" : "關(guān)于執(zhí)行代碼";
fileName = (index==1) ? "更新及使用說明.txt": "關(guān)于執(zhí)行代碼.txt";
//窗口屬性
setTitle(frameName);
setSize(550, 600);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((d.width - getSize().width) / 2,
(d.height - getSize().height) / 2);
}
/** 響應(yīng)事件*/
public void actionPerformed(ActionEvent evt) {
//文本屬性
JTextArea jtaExplain = new JTextArea();
jtaExplain.setWrapStyleWord(true);
jtaExplain.setLineWrap(true);
jtaExplain.setEditable(false);
jtaExplain.setTabSize(2);
JScrollPane jspExplain = new JScrollPane(jtaExplain);
add(jspExplain);
//讀入信息文件內(nèi)容
try {
BufferedReader input = new BufferedReader(new FileReader(fileName));
String line;
while ((line = input.readLine()) != null)
jtaExplain.append(line + "\n");
input.close();
}
catch (IOException ex) {
jtaExplain.setText("文件不在指定位置!\n 請檢查文件的位置!");
}
//顯示信息窗口
setVisible(true);
}
}
/** 運(yùn)行時發(fā)現(xiàn)錯誤的異常類*/
class MyRuntimeException extends Exception{
//產(chǎn)生錯誤的類型
private String reason = "";
public int start = -1;
public int end = -1;
//構(gòu)造方法
public MyRuntimeException(String reason, int start, int end) {
this.reason = reason;
this.start = start;
this.end = end;
}
//錯誤輸出
public String toString() {
return reason;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -