java从文本框获得字符串类型的数学表达式,如何计算出结果。如下图 5
文本框中的字符串如何转化为数学表达式?我利用正则表达式提去出了数字和运算符号,我有如何判断它们本来的位置,插回到原位置呢??求这一部分的关键代码。。。...
文本框中的字符串如何转化为数学表达式?我利用正则表达式提去出了数字和运算符号,我有如何判断它们本来的位置,插回到原位置呢??
求这一部分的关键代码。。。 展开
求这一部分的关键代码。。。 展开
展开全部
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalculatorTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
CalculatorTest test = new CalculatorTest();
CalculatorFrame frame = test.new CalculatorFrame();
frame.setVisible(true);
}
});
}
public class CalculatorFrame extends JFrame {
private JPanel jPanel;
private JButton display;
private CalculatorListener calculatorListener;
private NumberListener numberListener;
private boolean start = true;
private String last = "=";
private double result;
public CalculatorFrame() {
this.setSize(400, 400);
this.setLayout(new BorderLayout());
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationByPlatform(false);
jPanel = new JPanel(new GridLayout(4, 4));
display = new JButton("0");
display.setEnabled(false);
calculatorListener = new CalculatorListener();
numberListener = new NumberListener();
this.add(display, BorderLayout.NORTH);
this.add(jPanel, BorderLayout.CENTER);
addButton("7", numberListener);
addButton("8", numberListener);
addButton("9", numberListener);
addButton("/", calculatorListener);
addButton("4", numberListener);
addButton("5", numberListener);
addButton("6", numberListener);
addButton("*", calculatorListener);
addButton("1", numberListener);
addButton("2", numberListener);
addButton("3", numberListener);
addButton("-", calculatorListener);
addButton("0", numberListener);
addButton(".", numberListener);
addButton("=", calculatorListener);
addButton("+", calculatorListener);
}
private void addButton(String text, ActionListener listener){
JButton jButton = new JButton(text);
jPanel.add(jButton);
jButton.addActionListener(listener);
}
private class CalculatorListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (!start) {
String option = e.getActionCommand();
double num = Double.parseDouble(display.getText());
if (last.equals("+")) result += num;
else if (last.equals("-")) result -= num;
else if (last.equals("*")) result *= num;
else if (last.equals("/")) result /= num;
else if (last.equals("=")) result = num;
display.setText(result + "");
last = option;
start = true;
}
}
}
private class NumberListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = e.getActionCommand();
if (start) {
display.setText("");
start = false;
}
display.setText(display.getText() + num);
}
}
}
}
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalculatorTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
CalculatorTest test = new CalculatorTest();
CalculatorFrame frame = test.new CalculatorFrame();
frame.setVisible(true);
}
});
}
public class CalculatorFrame extends JFrame {
private JPanel jPanel;
private JButton display;
private CalculatorListener calculatorListener;
private NumberListener numberListener;
private boolean start = true;
private String last = "=";
private double result;
public CalculatorFrame() {
this.setSize(400, 400);
this.setLayout(new BorderLayout());
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationByPlatform(false);
jPanel = new JPanel(new GridLayout(4, 4));
display = new JButton("0");
display.setEnabled(false);
calculatorListener = new CalculatorListener();
numberListener = new NumberListener();
this.add(display, BorderLayout.NORTH);
this.add(jPanel, BorderLayout.CENTER);
addButton("7", numberListener);
addButton("8", numberListener);
addButton("9", numberListener);
addButton("/", calculatorListener);
addButton("4", numberListener);
addButton("5", numberListener);
addButton("6", numberListener);
addButton("*", calculatorListener);
addButton("1", numberListener);
addButton("2", numberListener);
addButton("3", numberListener);
addButton("-", calculatorListener);
addButton("0", numberListener);
addButton(".", numberListener);
addButton("=", calculatorListener);
addButton("+", calculatorListener);
}
private void addButton(String text, ActionListener listener){
JButton jButton = new JButton(text);
jPanel.add(jButton);
jButton.addActionListener(listener);
}
private class CalculatorListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (!start) {
String option = e.getActionCommand();
double num = Double.parseDouble(display.getText());
if (last.equals("+")) result += num;
else if (last.equals("-")) result -= num;
else if (last.equals("*")) result *= num;
else if (last.equals("/")) result /= num;
else if (last.equals("=")) result = num;
display.setText(result + "");
last = option;
start = true;
}
}
}
private class NumberListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = e.getActionCommand();
if (start) {
display.setText("");
start = false;
}
display.setText(display.getText() + num);
}
}
}
}
追问
有没有如图我的图片那样的,先输入表达式,并且表达式不会消失于屏幕,就想真正计算器那样的。。。我想知道的问题是
比如“3*5+8-9”这样一个字符串(有可能比这个更复杂),我将它转换成真正的数学表达式,并计算出来程序该如何来写???
求解答》》》
追答
比如1 + 2 - 3 * 4 / 5 + 6 * ( 7 + 8 ),
设置变量num1,num2,num3,operator1,operator2,
然后num1=1,num2=2,num3=3,operator1='+',operator2='-',
这里判断operator2不是乘或除,所以先算operator1,
然后num1=1+2=3,num2=3,num3=4,operator1='-',operator2='*',
这里判断operator2是乘或除,operator1不是乘或除,所以先算operator2,
然后以此类推,
碰到括号就把括号中的东西取出来递归上述过程
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询