在idea2020实现简单算术表示式到抽象语法树的转换。具体要求如下:(1)用户任意输入一个正确的算术表示式,程序将其转换成抽象语法树输出;(2)输入以#结束的中缀表达式(包括+-/()数字),例如:x=a+bc;(3)输出的抽象语法树需以GUI界面展示。的代码是什么
时间: 2023-12-26 14:06:57 浏览: 192
下面是一个示例代码实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
public class ArithmeticExpressionTree extends JFrame implements ActionListener {
private JTextField inputField;
private JButton convertButton;
private JTextArea outputArea;
public ArithmeticExpressionTree() {
setTitle("Arithmetic Expression Tree Converter");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inputField = new JTextField(20);
convertButton = new JButton("Convert");
convertButton.addActionListener(this);
outputArea = new JTextArea(10, 20);
outputArea.setEditable(false);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(inputField, BorderLayout.NORTH);
contentPane.add(convertButton, BorderLayout.CENTER);
contentPane.add(new JScrollPane(outputArea), BorderLayout.SOUTH);
setContentPane(contentPane);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String input = inputField.getText();
if (input.length() == 0 || input.charAt(input.length() - 1) != '#') {
JOptionPane.showMessageDialog(this, "Invalid input!");
return;
}
input = input.substring(0, input.length() - 1);
try {
ExpressionTree tree = buildExpressionTree(input);
outputArea.setText(tree.toString());
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Invalid expression!");
}
}
private static class ExpressionTree {
private char operator;
private int operand;
private ExpressionTree left, right;
public ExpressionTree(char operator, ExpressionTree left, ExpressionTree right) {
this.operator = operator;
this.left = left;
this.right = right;
}
public ExpressionTree(int operand) {
this.operand = operand;
}
public int evaluate() {
if (left == null && right == null) {
return operand;
} else {
int leftValue = left.evaluate();
int rightValue = right.evaluate();
switch (operator) {
case '+':
return leftValue + rightValue;
case '-':
return leftValue - rightValue;
case '*':
return leftValue * rightValue;
case '/':
return leftValue / rightValue;
default:
throw new IllegalStateException("Invalid operator: " + operator);
}
}
}
@Override
public String toString() {
if (left == null && right == null) {
return Integer.toString(operand);
} else {
return "(" + left.toString() + operator + right.toString() + ")";
}
}
}
private static boolean isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
private static int getPrecedence(char operator) {
switch (operator) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
throw new IllegalStateException("Invalid operator: " + operator);
}
}
private static ExpressionTree buildExpressionTree(String input) {
Stack<ExpressionTree> operandStack = new Stack<>();
Stack<Character> operatorStack = new Stack<>();
int i = 0;
while (i < input.length()) {
char c = input.charAt(i);
if (Character.isDigit(c)) {
int j = i;
while (j < input.length() && Character.isDigit(input.charAt(j))) {
j++;
}
int operand = Integer.parseInt(input.substring(i, j));
operandStack.push(new ExpressionTree(operand));
i = j;
} else if (isOperator(c)) {
while (!operatorStack.isEmpty() && getPrecedence(c) <= getPrecedence(operatorStack.peek())) {
char operator = operatorStack.pop();
ExpressionTree right = operandStack.pop();
ExpressionTree left = operandStack.pop();
operandStack.push(new ExpressionTree(operator, left, right));
}
operatorStack.push(c);
i++;
} else if (c == '(') {
operatorStack.push(c);
i++;
} else if (c == ')') {
while (operatorStack.peek() != '(') {
char operator = operatorStack.pop();
ExpressionTree right = operandStack.pop();
ExpressionTree left = operandStack.pop();
operandStack.push(new ExpressionTree(operator, left, right));
}
operatorStack.pop();
i++;
} else {
throw new IllegalStateException("Invalid character: " + c);
}
}
while (!operatorStack.isEmpty()) {
char operator = operatorStack.pop();
ExpressionTree right = operandStack.pop();
ExpressionTree left = operandStack.pop();
operandStack.push(new ExpressionTree(operator, left, right));
}
return operandStack.pop();
}
public static void main(String[] args) {
new ArithmeticExpressionTree();
}
}
```
这个代码实现了一个基于 Java Swing 的 GUI 界面,可以让用户输入一个中缀算术表达式(以#结尾),然后将其转换成抽象语法树并在界面上展示出来。具体实现中,我们使用两个栈来处理算术表达式的转换过程:一个操作数栈用于存储操作数,一个运算符栈用于存储运算符。我们遍历输入的表达式,当遇到一个数字时,我们将其转换成操作数节点并压入操作数栈中;当遇到一个运算符时,我们将其与运算符栈的栈顶元素进行比较,如果优先级不高于栈顶元素则将栈顶元素弹出并生成一个新的运算符节点,然后将该节点压入操作数栈中;当遇到一个左括号时,我们将其直接压入运算符栈中;当遇到一个右括号时,我们将运算符栈中的元素弹出直到遇到左括号,并依次生成新的运算符节点并压入操作数栈中。最终,我们将操作数栈中的唯一元素作为抽象语法树的根节点返回。
阅读全文