📄 listener.java
字号:
package calc_ls;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.event.*;
public class Listener extends WindowAdapter implements ActionListener,WindowListener,ItemListener
{
static StringBuffer buffer = new StringBuffer("0");
float a = 0,b = 0;
boolean aIsEmpty = true;
boolean bIsEmpty = true;
char theta = '+';
public boolean isOP(char character) //判断字符是否为运算符
{
if (character == '+')
return true;
else if (character == '-')
return true;
else if (character == '*')
return true;
else if (character == '/')
return true;
else if (character == '(')
return true;
else if (character == ')')
return true;
else if (character == '#')
return true;
return false;
}//isOP
public StringBuffer getTempBuffer(StringBuffer exp) //从exp中获得一个运算数或运算符
{
int bufferCount = -1;
StringBuffer buffer = new StringBuffer();
while (exp.charAt(++bufferCount) != '|');
buffer.append(exp.subSequence(0, bufferCount)); //取得第一个运算数
exp.delete(0, bufferCount + 1); //删除第一个运算数
return buffer;
}//getTempBuffer
public int converOP(char op) //将运算符转换为int
{
if (op == '+')
return 0;
else if (op == '-')
return 1;
else if (op == '*')
return 2;
else if (op == '/')
return 3;
else if (op == '(')
return 4;
else if (op == ')')
return 5;
else if (op == '#')
return 6;
return 0;
}//converOP
public int precede(char op1, char op2) //确定运算符的优先权
{
int operatePRI[][] = {{1,1,-1,-1,-1,1,1},
{1,1,-1,-1,-1,1,1},
{1,1,1,1,-1,1,1},
{1,1,1,1,-1,1,1},
{-1,-1,-1,-1,-1,0,0},
{1,1,1,1,0,1,1},
{-1,-1,-1,-1,-1,0,0}};
int operation1 = converOP(op1);
int operation2 = converOP(op2);
return operatePRI[operation1][operation2];
}//precede
public float operate(float a, char theta, float b) //计算a theta b,其中theta为运算符, a,b为运算数
{
if (theta == '+')
return a + b;
else if (theta == '-')
return a - b;
else if (theta == '*')
return a * b;
else if (theta == '/')
return a / b;
else return 1;
}//operate
public float evaluateExpression(StringBuffer exp) //计算表达式方法
{
StringBuffer opStack = new StringBuffer(); //运算符栈
float numStack[] = new float[50]; //数字栈
int numCount = -1;
StringBuffer tempBuffer = new StringBuffer(); //定义存放取得的运算符或运算数的缓冲区
tempBuffer = getTempBuffer(exp);
opStack.append('#'); //向运算符栈压入标志符'#'
//取得得字符与运算符栈顶字符不为# 循环
while ( tempBuffer.charAt(0) != '#' || opStack.charAt(opStack.length()-1) != '#')
{
System.out.println("exp "+exp);
System.out.println(tempBuffer.length()+" length "+tempBuffer);
System.out.println(opStack.charAt(opStack.length()-1));
if ( !isOP(tempBuffer.charAt(tempBuffer.length()-1)) )
{
numStack[++numCount] = Float.parseFloat(tempBuffer.toString()); //将运算数入栈,运算数栈计数器加1
tempBuffer.delete(0, tempBuffer.length()); //清空缓冲区
tempBuffer = getTempBuffer(exp);
}
else
switch (precede(opStack.charAt(opStack.length()-1),tempBuffer.charAt(0)))
{
case -1: //栈顶元素优先权低
opStack.append(tempBuffer.charAt(0));
tempBuffer = getTempBuffer(exp);
break;
case 0: //脱括号并接受下一个运算数或运算符
opStack.deleteCharAt(opStack.length()-1);
tempBuffer = getTempBuffer(exp);
break;
case 1: //退栈并将运算结果入栈
char theta = opStack.charAt(opStack.length()-1);
opStack.deleteCharAt(opStack.length()-1);
float b = numStack[numCount--];
float a = numStack[numCount--];
numStack[++numCount] = operate(a,theta,b);
break;
}//switch
System.out.println("numStack "+numStack[numCount]);
}//while
return numStack[0];
}//evaluateExpression
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
Button btn = (Button)e.getSource();
String tempS = btn.getLabel();
// buffer.append(btn.getLabel());
// CalcWindow.lab.setText(btn.getLabel());
switch (CalcWindow.choice)
{
case 1:
{
if (!tempS.equals("=")){
if (tempS.equals("+/-"))
{
if (buffer.charAt(0) == '-')
buffer.deleteCharAt(0);
else
buffer.insert(0, '-');
}
else if (tempS.equals("+") || tempS.equals("-") || tempS.equals("*") || tempS.equals("/"))
{
if (buffer.charAt(buffer.length()-2) == '+' || buffer.charAt(buffer.length()-2) == '-'
|| buffer.charAt(buffer.length()-2) == '*' || buffer.charAt(buffer.length()-2) == '/')
{
buffer.replace(buffer.length()-1, buffer.length()-1, tempS);
}
else if (buffer.charAt(buffer.length()-2) == '(' || buffer.charAt(buffer.length()-2) == ')')
{
buffer.append(tempS + "|");
}
else
buffer.append("|" + tempS + "|");
// CalcWindow.textBuffer.append(buffer);
// buffer = new StringBuffer();
}
else if (tempS.equals("."))
{
if (buffer.charAt(buffer.length()-1) == '.');
else
buffer.append('.');
}
else if (tempS.equals("<--"))
{
if ( buffer.length() == 1 )
buffer.setCharAt(0, '0');
else
buffer.deleteCharAt(buffer.length()-1);
}
else if (tempS.equals("C"))
{
buffer = new StringBuffer("0");
CalcWindow.lab.setText("0.");
}
else if (tempS.equals("CE"))
{
buffer = new StringBuffer("0");
CalcWindow.textBuffer = new StringBuffer();
CalcWindow.lab.setText("0.");
}
else if (tempS.equals("("))
{
if (buffer.charAt(buffer.length()-1) == '|')
buffer.append("(|");
else
buffer.append("|(|");
// CalcWindow.textBuffer.append(buffer);
// buffer = new StringBuffer();
}
else if (tempS.equals(")"))
{
if (buffer.charAt(buffer.length()-1) == '|')
buffer.append(")|");
else
buffer.append("|)|");
// CalcWindow.textBuffer.append(buffer);
// buffer = new StringBuffer();
}
else
buffer.append(tempS);
// CalcWindow.textBuffer.append(buffer);
// buffer = new StringBuffer();
StringBuffer bufferDisplay = new StringBuffer(buffer.toString());
CalcWindow.lab.setText(bufferDisplay.toString().replace('|', ' '));}
else
{
CalcWindow.textBuffer.append(buffer);
if (CalcWindow.textBuffer.charAt(CalcWindow.textBuffer.length()-1) != '|')
CalcWindow.textBuffer.append("|#|");
else
CalcWindow.textBuffer.append("#|");
float result = evaluateExpression(CalcWindow.textBuffer);
CalcWindow.lab.setText(String.valueOf(result));
buffer = new StringBuffer("0");
CalcWindow.textBuffer = new StringBuffer();
// System.out.println(CalcWindow.textBuffer);
}
// StringBuffer bufferDisplay = buffer;
break;
}//case 1
case 2:
{
if (!tempS.equals("="))
{
if (tempS.equals("+/-"))
{
if (buffer.charAt(0) == '-')
buffer.deleteCharAt(0);
else
buffer.insert(0, '-');
CalcWindow.lab.setText(buffer.toString());
}
else if (tempS.equals("C"))
{
buffer = new StringBuffer();
CalcWindow.lab.setText("0");
}
else if (tempS.equals("CE"))
{
buffer = new StringBuffer();
a = 0;
b = 0;
aIsEmpty = true;
bIsEmpty = true;
CalcWindow.lab.setText("0");
}
else if (tempS.equals("<--"))
{
if ( buffer.length() == 1 )
buffer.setCharAt(0, '0');
else
buffer.deleteCharAt(buffer.length()-1);
CalcWindow.lab.setText(buffer.toString());
}
else if (tempS.equals("+") || tempS.equals("-") || tempS.equals("*") || tempS.equals("/"))
{
if (aIsEmpty == false && bIsEmpty == false)
{
theta = tempS.charAt(0);
a = Float.parseFloat(CalcWindow.lab.getText().toString());
// bIsEmpty = true;
}
else if (aIsEmpty == true)
{
theta = tempS.charAt(0);
if (buffer.toString().length() > 0)
a = Float.parseFloat(buffer.toString());
aIsEmpty = false;
buffer = new StringBuffer();
}
else
{
theta = tempS.charAt(0);
if (buffer.toString().length() > 0)
b = Float.parseFloat(buffer.toString());
a = operate(a, theta, b);
CalcWindow.lab.setText(a + "");
buffer = new StringBuffer();
bIsEmpty = false;
}
}
else
{
buffer.append(tempS);
CalcWindow.lab.setText(buffer.toString());
bIsEmpty = true;
}
}
else //是等号
{
if (buffer.toString().length() > 0)
b = Float.parseFloat(buffer.toString());
a = operate(a, theta, b);
CalcWindow.lab.setText(a + "");
buffer = new StringBuffer();
// a = 0;
aIsEmpty = true;
}
//...即时计算代码
}
}//switch
}
public void itemStateChanged(ItemEvent l)
{
Checkbox chb = (Checkbox)l.getSource();
if (chb.getLabel().equals("表达式"))
{
CalcWindow.choice = 1;
CalcWindow.button_leftparenthesis.setEnabled(true);
CalcWindow.button_rightparenthesis.setEnabled(true);
buffer = new StringBuffer("0");
CalcWindow.textBuffer = new StringBuffer();
CalcWindow.lab.setText("0");
}
else
{
CalcWindow.choice = 2;
CalcWindow.button_leftparenthesis.setEnabled(false);
CalcWindow.button_rightparenthesis.setEnabled(false);
buffer = new StringBuffer("0");
CalcWindow.textBuffer = new StringBuffer();
CalcWindow.lab.setText("0");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -