加减乘除以及左括号优先级设置为:
具体的处理方法: 用到了两个栈,一个符号栈,一个数字栈
符号栈的处理方式是: 单调栈的方式,只有当栈外符号的优先级高于栈顶符号的优先级时才会将栈外符号压入栈中。
数字栈的处理方式是: 没有处理方式,不管是什么数值的数字直接压入栈中即可。
package 图形界面;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.*;
import javax.swing.JFrame;
public class 计算器 {
public static void main(String[] args) {
JFrame frame = new JFrame("计算器");
frame.setSize(700,700);
JPanel panel = new JPanel();
panel.setLayout(null);
JLabel label1 = new JLabel("输入计算表达式: ");
label1.setBounds(200,100,100,40);
panel.add(label1);
JTextField text1 = new JTextField(10);
text1.setBounds(300,100,200,40);
panel.add(text1);
JButton button1 = new JButton("运算");
panel.add(button1);
button1.setBounds(300,160,80,60);
panel.add(new JLabel(" "));
JLabel label2 = new JLabel("结果:");
label2.setBounds(200,240,100,40);
panel.add(label2);
JTextField text2 = new JTextField(10);
panel.add(text2);
text2.setBounds(300,240,200,40);
frame.add(panel);
frame.setVisible(true);
button1.addActionListener(new listen(button1,text1,text2));
}
static class listen implements ActionListener {
JButton button1;
JTextField text1;
JTextField text2;
public listen(JButton button1,JTextField text1,JTextField text2){
this.button1 = button1;
this.text1 = text1;
this.text2 = text2;
}
public void actionPerformed(ActionEvent e){
String s = text1.getText();
System.out.println(s);
Stack<Double> stk1 = new Stack<Double>();
Stack<Character> stk2 = new Stack<Character>();
for (int i = 0; i < s.length(); ++i){
char temp = s.charAt(i); // String 取其某一个元素
if ((temp >= '0' && temp <= '9') || temp == '.'){
double sum = temp - '0';
int j = i + 1;
boolean flag = true;
int index = 1;
while(j < s.length()){
temp = s.charAt(j);
if (temp == '.'){
flag = false;
index = 1;
++j;
continue;
}
else if (flag && (temp >= '0' && temp <= '9')){
sum = sum * 10 + temp - '0';
++j;
}else if (!flag && (temp >= '0' && temp <= '9')){
sum += Math.pow(0.1,index) * (temp - '0');
index ++;
++j;
}
else break;
}
System.out.println(sum);
stk1.push(new Double(sum));
i = j - 1;
}
else if (temp == '+' || temp == '-' || temp == '*' || temp == '/' || temp == '(' ){
if (stk2.empty() || get_out_priority(temp) >= get_in_priority(stk2.peek().charValue()))
stk2.push(new Character(temp));
else{
while(!stk2.empty() && (get_out_priority(temp) < get_in_priority(stk2.peek().charValue()))){
char c = stk2.peek().charValue();
stk2.pop();
double num2 = stk1.peek().doubleValue();
stk1.pop();
double num1 = stk1.peek().doubleValue();
stk1.pop();
if (c == '+') stk1.push(new Double(num1 + num2));
if (c == '-') stk1.push(new Double(num1 - num2));
if (c == '*') stk1.push(new Double(num1 * num2));
if (c == '/') stk1.push(new Double(num1 / num2));
}
stk2.push(new Character(temp));
}
}
else if (temp == ')'){ // ')'
while(stk2.peek().charValue() != '('){
char c = stk2.peek().charValue();
stk2.pop();
double num2 = stk1.peek().doubleValue();
stk1.pop();
double num1 = stk1.peek().doubleValue();
stk1.pop();
if (c == '+') stk1.push(new Double(num1 + num2));
if (c == '-') stk1.push(new Double(num1 - num2));
if (c == '*') stk1.push(new Double(num1 * num2));
if (c == '/') stk1.push(new Double(num1 / num2));
}
stk2.pop();
}
}
if (!stk2.empty()){
while(!stk2.empty()){
char c = stk2.peek().charValue();
stk2.pop();
double num2 = stk1.peek().doubleValue();
stk1.pop();
double num1 = stk1.peek().doubleValue();
stk1.pop();
if (c == '+') stk1.push(new Double(num1 + num2));
if (c == '-') stk1.push(new Double(num1 - num2));
if (c == '*') stk1.push(new Double(num1 * num2));
if (c == '/') stk1.push(new Double(num1 / num2));
}
}
text2.setText((stk1.peek().toString()));
System.out.println(stk1.peek().doubleValue());
}
int get_out_priority(char temp){
if (temp == '+') return 3;
if (temp == '-') return 3;
if (temp == '*') return 4;
if (temp == '/') return 4;
if (temp == '(') return 5;
return 0;
}
int get_in_priority(char temp){
if (temp == '+') return 3;
if (temp == '-') return 3;
if (temp == '*') return 4;
if (temp == '/') return 4;
if (temp == '(') return 0;
return 0;
}
}
}