📄 expframe.java
字号:
confirmButton.setEnabled(true);
}
public void kill() {
stop(3);
}
public void Start() {
start(1);
start(2);
}
// 启动线程runner
public void start(int flag) {
if (flag == 1) {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
if (flag == 2) {
if (moving == null) {
moving = new Thread(this);
moving.start();
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newButton) {
stop(3);
if (mode) {
Base();
}
}
else if (e.getSource() == runButton) {
stop(2);
//启动线程runner
start(1);
runFlag = true;
}
else if (e.getSource() == stepButton) {
runFlag = false;
stop(1);
//启动线程runner
start(2);
movFlag = true;
}
else if (e.getSource() == stayButton) {
stop(3);
runFlag = false;
}
else if (e.getSource() == confirmButton) {
str = expJTextField.getText().trim();
if (str.length() == 0) {
//满足str.length()为0表示没有输入表达式
JOptionPane.showConfirmDialog(this, " 表达式不能为空,请从新输入", "出错信息",
JOptionPane.DEFAULT_OPTION);
return;
}
if (stringTokenizer(str) == 1) {
confirmButton.setEnabled(false);
expJTextField.setEditable(false);
expJTextField.setBackground(Color.white);
initAnim();
runButton.setEnabled(true);
stepButton.setEnabled(true);
mode = true;
}
}
else if (e.getSource() == helpButton) {
if (Root.helpFrame != null) {
Root.helpFrame.dispose();
}
Root.helpFrame = new HelpFrame("/help/exp.html", super.root);
Root.helpFrame.setLocation(200, 100);
Root.helpFrame.showNewHtmlPage("/help/exp.html");
Root.helpIsOn = true;
Root.helpFrame.setVisible(true);
}
else if (e.getSource() == closeButton) {
Root.clip.stop();
this.setVisible(false);
}
else if (e.getSource() == musicButton) {
if (flag1) {
Root.clip.loop();
musicButton.setText("关闭音乐");
flag1 = !flag1;
}
else {
Root.clip.stop();
musicButton.setText("播放音乐");
flag1 = !flag1;
}
}
}
//函数precede(char c1,char c2),参数c1,c2都是运算符
//函数用来比较c1和c2的运算优先级,c1运算优先级高于c2返回1,低于返回-1,等于返回0
//返回2表示表达式出现语法错误
public int precede(String c1, String c2) {
if (c2.equals("+") || c2.equals("-")) {
if (c1.equals("+") || c1.equals("-") || c1.equals("*") || c1.equals("/") ||
c1.equals(")"))
return 1;
if (c1.equals("(") || c1.equals("#"))
return -1;
}
else if (c2.equals("*") || c2.equals("/")) {
if (c1.equals("*") || c1.equals("/") || c1.equals(")"))
return 1;
if (c1.equals("+") || c1.equals("-") || c1.equals("(") || c1.equals("#"))
return -1;
}
else if (c2.equals("(")) {
if (c1.equals(")"))
return 2;
else
return -1;
}
else if (c2.equals(")")) {
if (c1.equals("("))
return 0;
else if (c1.equals("#"))
return 2;
else
return 1;
}
else if (c2.equals("#")) {
if (c1.equals("#"))
return 0;
else if (c1.equals("("))
return 2;
else
return 1;
}
return 2;
}
//函数stringTokenizer(String str),参数str是表达式的字符串表示
//函数用于把表达式的运算符、界限符和运算数分别加到数组operator[]和figure[]中
//并检查表达是是否合法
public int stringTokenizer(String str) {
oldFlag = newFlag = 0;
record = 0;
oper = 0;
flag = 0;
//s用于获取字符串的字符
String s;
//s1用于保存无空格的表达式字符串
String s1 = "#";
for (int j = 0; j < str.length(); j++) {
//循环用于消除表达式中间的空格
s = str.substring(j, j + 1).trim();
if (s.length() != 0)
s1 = s1 + s;
}
s = s1.substring(s1.length() - 1, s1.length());
if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
JOptionPane.showConfirmDialog(this,
" 输入的表达式不合法\n 请从新输入正确的表达式\n",
"出错信息", JOptionPane.DEFAULT_OPTION);
return 0;
}
s = s1.substring(1, 2);
if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
JOptionPane.showConfirmDialog(this,
" 输入的表达式不合法\n 请从新输入正确的表达式\n",
"出错信息", JOptionPane.DEFAULT_OPTION);
return 0;
}
s1 = s1 + "#";
String c;
for (int i = 0; i < s1.length(); i++) {
c = s1.substring(i, i + 1);
if (c.equals("+") || c.equals("-") || c.equals("*") || c.equals("/") ||
c.equals("(") || c.equals(")") || c.equals("#")) {
operator[oper] = c;
oper++;
if (flag == 0) {
if (oldFlag != 0 && i != 0) {
newFlag = i;
flag = 2;
}
else {
oldFlag = i + 1;
exp[record] = c;
record++;
flag = 1;
}
}
else if (flag == 1) {
newFlag = i;
flag = 2;
}
if (i != 0 && c.equals("#")) {
operator[oper] = c;
oper++;
if (oldFlag != newFlag) {
exp[record] = s1.substring(oldFlag, newFlag);
record++;
}
exp[record] = c;
record++;
break;
}
if (flag == 2) {
if (oldFlag != newFlag) {
exp[record] = s1.substring(oldFlag, newFlag);
record++;
exp[record] = c;
record++;
flag = 0;
oldFlag = newFlag + 1;
}
else if (s1.substring(oldFlag - 1, oldFlag).equals("#")) {
if (i == 1) {
exp[record] = c;
record++;
flag = 0;
oldFlag = newFlag + 1;
}
else {
break;
}
}
else if (s1.substring(oldFlag - 1, oldFlag).equals(")")) {
exp[record] = c;
record++;
flag = 0;
oldFlag = newFlag + 1;
}
else if (s1.substring(oldFlag, oldFlag + 1).equals("(")) {
exp[record] = c;
record++;
flag = 0;
oldFlag = newFlag + 1;
}
else {
//当表达式的运算数不合法时弹出一个出错对话框
JOptionPane.showConfirmDialog(this,
" 输入的表达式不合法\n 请从新输入正确的表达式\n",
"出错信息", JOptionPane.DEFAULT_OPTION);
return 0;
}
}
}
}
StringTokenizer tokenizer = new StringTokenizer(s1, "+ - * ( / ) #");
while (tokenizer.hasMoreTokens()) {
String s2 = tokenizer.nextToken();
float p;
try {
p = Float.valueOf(s2).floatValue();
}
catch (NumberFormatException e) {
//当表达式的运算数不合法时弹出一个出错对话框
JOptionPane.showConfirmDialog(this,
" 输入的表达式不合法\n 请从新输入正确的表达式\n",
"出错信息", JOptionPane.DEFAULT_OPTION);
return 0;
}
}
return 1;
}
//函数Operate(char a,char theta,char b)参数theta是一个运算符
//函数用于返回参数a和参数b经过运算符theta运算的结果
public float Operate(float a, String theta, float b) {
float result1 = 1;
if (theta.equals("+"))
result1 = a + b;
else if (theta.equals("-"))
result1 = a - b;
else if (theta.equals("*"))
result1 = a * b;
else if (theta.equals("/"))
result1 = a / b;
return result1;
}
public void EvaluateExpression() {
jTextField4.setText(" " + temporary);
switch (codePart) {
case 0:
temporary = exp[tag];
tag++;
OPTR.push(temporary);
append("界限符 " + temporary + " 入栈");
optrCount++;
pushMoving(temporary, 1);
temporary = exp[tag];
tag++;
jTextField4.setText(" " + temporary);
codePanel.highlight(3);
codePart = 1;
break;
case 1:
codePanel.highlight(4);
if (!temporary.equals("#") || !OPTR.peek().equals("#")) {
codePart = 2;
}
else
codePart = 11;
break;
case 2:
codePanel.highlight(5);
if (In(temporary)) {
codePart = 4;
}
else
codePart = 5;
break;
case 3:
codePanel.highlight(18);
codePart = 1;
break;
case 4:
codePanel.highlight(6);
OPEN.push(temporary);
append("操作数 " + temporary + " 入栈");
openCount++;
pushMoving(temporary, 2);
temporary = exp[tag];
tag++;
codePart = 3;
break;
case 5:
codePanel.highlight(7);
codePart = 6;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -