前言
这篇文章是全部做完之后写的,所以有一些是第一天学的东西,思路比较连贯。
一、设计思路。
1、创建两个文本框( text1,text2 ),点击按钮时把按钮所代表的 char 写到 text1(用 old 把 text1 里的 String 读出来,加上按钮新添加的 char )。
2、text1 里的内容 :例如 :“ 1+(1+2)* 2 - (1 / 2)= ”。
3、构造后缀表达式:
循环体{
----从左向右读取运算符号,
----如果运算符号前面有数,就把前面的数放进队列中,
(处理完数字处理运算符号,想要把运算符号堆栈)
----如果栈为空,则直接把运算符号压栈,
否则,----与栈顶的运算符号比较优先级,
----如果比栈顶的优先级低,则把栈顶的运算符号放进队列中,再跳转到查询栈是否为空,往下执行。
否则,将该运算符号入栈,
----当 text1 中的元素取完之后,则栈中元素出栈—>入队}
例子的结果:1 1 2 + 2 * + 1 2 / - =
3、出队入栈计算,
循环体{
----如果是数则直接进栈,
----如果是运算符号,则出栈两个数来做计算,
----计算的结果进栈。}
例子:{
1
1 1
1 1 2
1 1 2 — (+)—> 1 1+ 2 —>1 3
1 3 2
1 3 2 — (*)—> 1 3* 2 —>1 6
1 6 — (+)—> 1+6 —> 7
7 1
7 1 2
7 1 2 — (/)—> 7 1/2 —>7 0.5
7 0.5 — (-)—> 7-0.5 —>6.5
6.5 — (=)—>输出
}
二、具体操作
1.做事
1、新建一个JFormDesigner Form
2、选好参数
3、这里拖进来7个JPanel,前上面两个JPanel放两个JTextField,下面的JPanel中都放进4个JButton,并给JButton修改名字。
4、右击button,添加监听鼠标点击事件。
5、会自动生成代码。
CalculatorUI.properties
#
# Created by JFormDesigner on Tue Dec 01 23:10:18 CST 2020
#
CalculatorUI.backSpace.text=<---
CalculatorUI.oneBtn.text=1
CalculatorUI.threeBtn.text=3
CalculatorUI.sevenBtn.text=7
CalculatorUI.subBtn.text=-
CalculatorUI.sixBtn.text=6
CalculatorUI.twoBrn.text=2
CalculatorUI.divBtn.text=/
CalculatorUI.mulBtn.text=*
CalculatorUI.eightBtn.text=8
CalculatorUI.eqBtn.text=\=
CalculatorUI.leftBr.text=(
CalculatorUI.plusBtn.text=+
CalculatorUI.point.text=.
CalculatorUI.zeroBtn.text=0
CalculatorUI.clean.text=C
CalculatorUI.fiveBtn.text=5
CalculatorUI.fourBtn.text=4
CalculatorUI.nineBtn.text=9
CalculatorUI.rightBr.text=)
CalculatorUI.java (我把main函数写在了UI里,手生,不是标准操作)
package Calculator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ResourceBundle;
/*
* Created by JFormDesigner on Tue Dec 01 19:28:55 CST 2020
*/
/**
* @author unknown
*/
public class CalculatorUI extends JFrame {
public static void main(String[] args) {
CalculatorUI wi = new CalculatorUI();
wi.setVisible(true);
}
int biao = 0;
public int getB() {
return biao;
}
public CalculatorUI() {
initComponents();
}
//返回process内的String
public String getProcess() {
return this.process.getText();
}
//在result框中输出结果
public void setResult(String result1) {
this.result.setText(result1);
}
private void leftBrMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old + "(");
}
private void rightBrMouseClicked(MouseEvent e) {
String old = process.getText();
if (old.contains("(")){
process.setText(old + ")");
}
else {
result.setText("请输入正确的格式!");
}
}
private void cleanMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText("");
this.biao = 0;
}
private void backSpaceMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old.substring(0,old.length() - 1));
}
private void sevenBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"7");
}
private void eightBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"8");
}
private void nineBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"9");
}
private void mulBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"*");
}
private void fourBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"4");
}
private void fiveBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"5");
}
private void sixBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"6");
}
private void subBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"-");
}
private void oneBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"1");
}
private void twoBrnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"2");
}
private void threeBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"3");
}
private void plusBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"+");
}
private void pointMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+".");
}
private void zeroBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"0");
}
private void divBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"/");
}
private void eqBtnMouseClicked(MouseEvent e) {
String old = process.getText();
process.setText(old+"=");
this.biao = 1;
this.setResult(this.getProcess());
this.process.setText(Deal.deal(this.getProcess()));//点击=号时,处理函数。
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
ResourceBundle bundle = ResourceBundle.getBundle("UI");
panel2 = new JPanel();
process = new JTextField();
panel3 = new JPanel();
result = new JTextField();
panel4 = new JPanel();
leftBr = new JButton();
rightBr = new JButton();
clean = new JButton();
backSpace = new JButton();
panel5 = new JPanel();
sevenBtn = new JButton();
eightBtn = new JButton();
nineBtn = new JButton();
mulBtn = new JButton();
panel6 = new JPanel();
fourBtn = new JButton();
fiveBtn = new JButton();
sixBtn = new JButton();
subBtn = new JButton();
panel7 = new JPanel();
oneBtn = new JButton();
twoBrn = new JButton();
threeBtn = new JButton();
plusBtn = new JButton();
panel8 = new JPanel();
point = new JButton();
zeroBtn = new JButton();
divBtn = new JButton();
eqBtn = new JButton();
//======== this ========
var contentPane = getContentPane();
contentPane.setLayout(new GridLayout(7, 4));
//======== panel2 ========
{
panel2.setLayout(new GridLayout());
panel2.add(process);
}
contentPane.add(panel2);
//======== panel3 ========
{
panel3.setLayout(new GridLayout());
panel3.add(result);
}
contentPane.add(panel3);
//======== panel4 ========
{
panel4.setLayout(new GridLayout());
//---- leftBr ----
leftBr.setText(bundle.getString("CalculatorUI.leftBr.text"));
leftBr.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
leftBrMouseClicked(e);
}
});
panel4.add(leftBr);
//---- rightBr ----
rightBr.setText(bundle.getString("CalculatorUI.rightBr.text"));
rightBr.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
rightBrMouseClicked(e);
}
});
panel4.add(rightBr);
//---- clean ----
clean.setText(bundle.getString("CalculatorUI.clean.text"));
clean.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
cleanMouseClicked(e);
}
});
panel4.add(clean);
//---- backSpace ----
backSpace.setText(bundle.getString("CalculatorUI.backSpace.text"));
backSpace.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
backSpaceMouseClicked(e);
}
});
panel4.add(backSpace);
}
contentPane.add(panel4);
//======== panel5 ========
{
panel5.setLayout(new GridLayout());
//---- sevenBtn ----
sevenBtn.setText(bundle.getString("CalculatorUI.sevenBtn.text"));
sevenBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
sevenBtnMouseClicked(e);
}
});
panel5.add(sevenBtn);
//---- eightBtn ----
eightBtn.setText(bundle.getString("CalculatorUI.eightBtn.text"));
eightBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
eightBtnMouseClicked(e);
}
});
panel5.add(eightBtn);
//---- nineBtn ----
nineBtn.setText(bundle.getString("CalculatorUI.nineBtn.text"));
nineBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
nineBtnMouseClicked(e);
}
});
panel5.add(nineBtn);
//---- mulBtn ----
mulBtn.setText(bundle.getString("CalculatorUI.mulBtn.text"));
mulBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
mulBtnMouseClicked(e);
}
});
panel5.add(mulBtn);
}
contentPane.add(panel5);
//======== panel6 ========
{
panel6.setLayout(new GridLayout());
//---- fourBtn ----
fourBtn.setText(bundle.getString("CalculatorUI.fourBtn.text"));
fourBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
fourBtnMouseClicked(e);
}
});
panel6.add(fourBtn);
//---- fiveBtn ----
fiveBtn.setText(bundle.getString("CalculatorUI.fiveBtn.text"));
fiveBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
fiveBtnMouseClicked(e);
}
});
panel6.add(fiveBtn);
//---- sixBtn ----
sixBtn.setText(bundle.getString("CalculatorUI.sixBtn.text"));
sixBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
sixBtnMouseClicked(e);
}
});
panel6.add(sixBtn);
//---- subBtn ----
subBtn.setText(bundle.getString("CalculatorUI.subBtn.text"));
subBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
subBtnMouseClicked(e);
}
});
panel6.add(subBtn);
}
contentPane.add(panel6);
//======== panel7 ========
{
panel7.setLayout(new GridLayout());
//---- oneBtn ----
oneBtn.setText(bundle.getString("CalculatorUI.oneBtn.text"));
oneBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
oneBtnMouseClicked(e);
}
});
panel7.add(oneBtn);
//---- twoBrn ----
twoBrn.setText(bundle.getString("CalculatorUI.twoBrn.text"));
twoBrn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
twoBrnMouseClicked(e);
}
});
panel7.add(twoBrn);
//---- threeBtn ----
threeBtn.setText(bundle.getString("CalculatorUI.threeBtn.text"));
threeBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
threeBtnMouseClicked(e);
}
});
panel7.add(threeBtn);
//---- plusBtn ----
plusBtn.setText(bundle.getString("CalculatorUI.plusBtn.text"));
plusBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
plusBtnMouseClicked(e);
}
});
panel7.add(plusBtn);
}
contentPane.add(panel7);
//======== panel8 ========
{
panel8.setLayout(new GridLayout());
//---- point ----
point.setText(bundle.getString("CalculatorUI.point.text"));
point.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
pointMouseClicked(e);
}
});
panel8.add(point);
//---- zeroBtn ----
zeroBtn.setText(bundle.getString("CalculatorUI.zeroBtn.text"));
zeroBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
zeroBtnMouseClicked(e);
}
});
panel8.add(zeroBtn);
//---- divBtn ----
divBtn.setText(bundle.getString("CalculatorUI.divBtn.text"));
divBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
divBtnMouseClicked(e);
}
});
panel8.add(divBtn);
//---- eqBtn ----
eqBtn.setText(bundle.getString("CalculatorUI.eqBtn.text"));
eqBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
eqBtnMouseClicked(e);
}
});
panel8.add(eqBtn);
}
contentPane.add(panel8);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JPanel panel2;
private JTextField process;
private JPanel panel3;
private JTextField result;
private JPanel panel4;
private JButton leftBr;
private JButton rightBr;
private JButton clean;
private JButton backSpace;
private JPanel panel5;
private JButton sevenBtn;
private JButton eightBtn;
private JButton nineBtn;
private JButton mulBtn;
private JPanel panel6;
private JButton fourBtn;
private JButton fiveBtn;
private JButton sixBtn;
private JButton subBtn;
private JPanel panel7;
private JButton oneBtn;
private JButton twoBrn;
private JButton threeBtn;
private JButton plusBtn;
private JPanel panel8;
private JButton point;
private JButton zeroBtn;
private JButton divBtn;
private JButton eqBtn;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
Comp.java (比较优先级的类)
package Calculator;
public class Comp {
static String fu = "+-*/()=";
static int [][] com = new int[6][7];
//建立优先级数组表setCom(before,after)
public Comp() {
for (int i = 0; i<=4; i++) {
for (int j = 0; j<=6; j++) {
com[i][j] = 0;
if (i==4||j==4) {
com[i][j] = 1;
}
}
}
for (int k = 0; k<=6; k++) {
com[5][k] = 2;
}
com[0][2] = 1;
com[0][3] = 1;
com[1][2] = 1;
com[1][3] = 1;
com[4][1] = 3;//括号内的负数问题
com[4][5] = 1;//右括号遇见了左括号
}
//判断优先级,if返回0,则优先级低,进行
public static int getCom(char bef, char aft) {
int b = fu.indexOf(bef);
int a = fu.indexOf(aft);
return com[b][a];
}
}
Deal.java (处理类,我认为这个应该处理成主函数)
package Calculator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class Deal {
//创建窗口
public static String deal(String pt) {
//后缀表达式队列
Queue<String> afterZ = new LinkedList<String>();
//运算符号的栈
Stack<String> fu = new Stack();
if (pt.charAt(0)=='-') {
afterZ.offer("0");
}
int a = 1;
while (pt.length()>=1) {
int index = 0;
//找出第一个符号的下标index
while (true) {
if (pt.charAt(index) == '+'
|| pt.charAt(index) == '-'
|| pt.charAt(index) == '*'
|| pt.charAt(index) == '/'
|| pt.charAt(index) == '('
|| pt.charAt(index) == ')'
|| pt.charAt(index) == '=') {
break;
}
index++;
}
//判断第一个符号是不是在第一位
if (index != 0) {//如果不是在第一位,就把前面的数进队。
String zj = pt.substring(0, index);
afterZ.offer(zj);
}
//static的缺点
new Comp();
//把index位的运算符与栈中的运算符比较,若index位不大于则出栈
while (!fu.empty()) {
char zuo = fu.pop().charAt(0);
char you = pt.charAt(index);
// int a = Comp.fu.indexOf(zuo);
// int b = Comp.fu.indexOf(you);
if (Comp.getCom(zuo,you) == 0) {
afterZ.offer(String.valueOf(zuo));
continue;
}
if (Comp.getCom(zuo,you) == 2) { //前一个进栈的符号是右括号
fu.pop();
System.out.println("-----------");
continue;
}
if (Comp.getCom(zuo,you) == 3&&index==0) {
afterZ.offer("0");
}
//恢复出栈
fu.push(String.valueOf(zuo));
break;
}
//下面两个if解决负数问题。
// if (pt.charAt(0)=='-'&&fu.empty()) {
// afterZ.offer("0");
// }
// if (pt.charAt(0)=='('&&pt.charAt(1)=='-'){
// afterZ.offer("0");
// }
//此时栈为空,或者优先级最高
String kuo = String.valueOf(pt.charAt(index));
// if (kuo!=")") {
fu.push(kuo);
System.out.println(kuo);
// }
int len1 = pt.length();
pt = pt.substring(index + 1, len1);
System.out.println(pt);
int len2 = pt.length();
if (len1==len2) {
a++;
}
// System.out.println(len);
if (a==3) {
System.out.println(pt);
pt = "";
}
//判断长度是不是1
if (pt.length() == 0) {
while (!fu.empty()) {
afterZ.offer(fu.pop());
System.out.println("---------");
}
String eq = afterZ.peek();
System.out.println("---------");
if (eq!="=") {
afterZ.offer("=");
}
}
}
//队列入栈
Stack<Double> result = new Stack<Double>();
double zuo,you;
String z;
while (!(afterZ.peek()==null)) {
String s = afterZ.poll();
System.out.println(s);
if (s.charAt(0) == '+') {
you = result.pop();
zuo = result.pop();
result.push(zuo+you);
}
else if (s.charAt(0) == '-') {
you = result.pop();
zuo = result.pop();
result.push(zuo-you);
}
else if (s.charAt(0) == '*') {
you = result.pop();
zuo = result.pop();
result.push(zuo*you);
}
else if (s.charAt(0) == '/') {
you = result.pop();
zuo = result.pop();
result.push(zuo/you);
}
else if (s.charAt(0) == '=') {
return String.valueOf(result.pop());
}
else result.push(Double.parseDouble(s));
}
return null;
}
}
2.细节
负数处理:正常的负数输入思维:在最开始直接输入-55、或者把负数放在括号中。
在遇到 减号 - 时,判断是不是第一位,或者前面就是个左括号,是的话把就把0入队。(相当于0-55)。
括号处理:右括号遇到左括号就把右括号入栈,当等号遇到右括号,就把左括号和右括号都扔了,因为经过处理后,右括号前面就是左括号。
总结
总结就是先把完整的思想、流程、细节打好草稿,不然会累的半死。