我正在使用中缀来进行后缀计算。我想出了main.java的GUI,用于将输入中缀转换为postfix和calculate的Evaluation.java,以及除以0的DivisionException.java。但是,程序本身工作正常,但计算,检查括号对和DivisionException是不正常。我甚至无法判断我是否正确编写了ActionListener方法。请帮我一把。
// Main.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Main extends JFrame{
private GridLayout glm = new GridLayout(3, 1, 5, 20);
private JPanel jp1 = new JPanel();
private JPanel jp2 = new JPanel();
private JPanel jp3 = new JPanel();
private GridLayout gl1 = new GridLayout (1, 2);
private JLabel jl1 = new JLabel("Enter Infix Expression", JLabel.CENTER);
private JTextField jtf1 = new JTextField();
private GridLayout gl2 = new GridLayout(1, 3, 5, 9);
private JButton jbEvaluate = new JButton("Evaluate");
private GridLayout gl3 = new GridLayout(1, 2);
private JLabel jl2 = new JLabel("Result", JLabel.CENTER);
private JTextField jtf2 = new JTextField();
private String postfixExpression;
private int result;
public String userInput(){
return (String)jtf1.getText();
}
public Main(){
setTitle("Infix Expression Evaluator");
setSize(500, 200);
setLayout(glm);
jp1.setLayout(gl1);
jp1.add(jl1);
jp1.add(jtf1);
add(jp1);
jp2.setLayout(gl2);
jp2.add(new JLabel(""));
jp2.add(jbEvaluate);
jbEvaluate.addActionListener(new EnterActionListener());
jp2.add(new JLabel(""));
add(jp2);
jp3.setLayout(gl3);
jp3.add(jl2);
jp3.add(jtf2);
jtf2.setEditable(false);
jtf2.setBackground(Color.lightGray);
add(jp3);
setLocationRelativeTo(null);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class EnterActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
try{
if(userInput() == null){
JOptionPane.showMessageDialog(null, "Paranthesis does not match", "Paranthesis Error", JOptionPane.WARNING_MESSAGE);
}
else if(Evaluation.checkParenthesis(userInput()) == true){
postfixExpression = Evaluation.InfixToPostfix(userInput());
result = Evaluation.Calculation(postfixExpression);
jtf2.setText(Integer.toString(result));
}
else if(Evaluation.checkParenthesis(userInput()) == false){
JOptionPane.showMessageDialog(null, "Paranthesis does not match", "Paranthesis Error", JOptionPane.WARNING_MESSAGE);
}
else if(userInput() == null){
JOptionPane.showMessageDialog(null, "Paranthesis does not match", "Paranthesis Error", JOptionPane.WARNING_MESSAGE);
}
}catch(DivisionException de){
new DivisionException();
}
}
}
public static void main (String[] args){
Main main = new Main();
}
}
// Evaluation.java
import java.util.*;
import javax.swing.JOptionPane;
public class Evaluation {
public static String InfixToPostfix(String infixExpression){
boolean numberEnd = false;
String postfixExpression = "";
Stack stack = new Stack();
for(int i = 0; i< infixExpression.length(); i++){
char infixExpChar = infixExpression.charAt(i);
switch(infixExpChar){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
postfixExpression = postfixExpression.concat(infixExpChar+"");
numberEnd = true;
break;
case ' ':
JOptionPane.showMessageDialog(null, "Space in between expression is unacceptable", "Spacing Error", JOptionPane.WARNING_MESSAGE);
break;
case '(':
if(numberEnd == true){
postfixExpression = postfixExpression.concat(" ");
numberEnd = false;
}else{
stack.push(new Character('('));
break;
}
case ')':
if(numberEnd == true){
postfixExpression = postfixExpression.concat(" ");
numberEnd = false;
}else{
while(((Character)stack.peek()).charValue() != '('){
postfixExpression = postfixExpression.concat(((Character)stack.pop()).toString());
}
}Object openParenthesis = stack.pop();
break;
case '+':
case '-':
case '*':
case '/':
if(numberEnd == true){
postfixExpression = postfixExpression.concat(" ");
numberEnd = false;
}
while(!stack.isEmpty() && (((Character) stack.peek()).charValue()) != '(' && (Precedence(infixExpChar)) <= Precedence(((Character)stack.peek()).charValue())){
postfixExpression = postfixExpression.concat(((Character)stack.pop()).toString());
}
stack.push(new Character(infixExpChar));
break;
}
}if(numberEnd == true){
postfixExpression = postfixExpression.concat(" ");
numberEnd = false;
}while(!stack.isEmpty()){
postfixExpression = postfixExpression.concat(((Character)stack.pop()).toString());
}
return postfixExpression;
}
public static int Calculation(String postfixExpression) throws DivisionException{
int value1, value2;
String temporary = "";
Stack stack = new Stack();
for(int i = 0; i < postfixExpression.length(); i++){
char postfixExpChar = postfixExpression.charAt(i);
switch(postfixExpChar){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temporary = temporary.concat(postfixExpChar+"");
break;
case ' ':
stack.push(new Integer(temporary));
temporary = new String();
break;
case '+':
value1 = new Integer(((Integer)stack.pop()).intValue() + ((Integer)stack.pop()).intValue());
stack.push(value1);
break;
case '-':
value2 = new Integer(((Integer)stack.pop()).intValue());
value1 = new Integer(((Integer)stack.pop()).intValue() - value2);
stack.push(value1);
break;
case '*':
value1 = new Integer(((Integer)stack.pop()).intValue() * ((Integer)stack.pop()).intValue());
stack.push(value1);
break;
case '/':
value2 = new Integer(((Integer)stack.pop()).intValue());
value1 = new Integer(((Integer)stack.pop()).intValue() / value2);
if(value2 == 0){
throw new DivisionException();
}
stack.push(value1);
break;
}
}return (int) stack.peek();
}
public static int Precedence(char operator){
int precedence = 0;
switch(operator){
case '+':
case '-':
precedence = 1;
break;
case '*':
case '/':
precedence = 2;
break;
}return precedence;
}
public static boolean checkParenthesis(String exp){
Stack stack = new Stack();
for (int i = 0; i < exp.length(); i++){
char character = exp.charAt(i);
if(character == '('){
stack.push(new Character(character));
}else if(character == ')'){
if(stack.isEmpty()){
JOptionPane.showMessageDialog(null, "Parenthesis does not match", "Parenthesis Error", JOptionPane.WARNING_MESSAGE);
return false;
}else
return true;
}
}return stack.isEmpty();
}
}
// DivisionException
import javax.swing.JOptionPane;
public class DivisionException extends Exception{
public DivisionException(){
JOptionPane.showMessageDialog(null, "Cannot divide a number by 0", "Division Error", JOptionPane.WARNING_MESSAGE);
}
}