java實(shí)現(xiàn)科學(xué)計(jì)算器的全過程與代碼
介紹
本次設(shè)計(jì)的是一個(gè)有33個(gè)按鈕的科學(xué)計(jì)算器??梢赃M(jìn)行加,減,乘,除,開根號(hào),階乘,次方,百分號(hào),對(duì)數(shù),三角函數(shù)的計(jì)算。
實(shí)現(xiàn)思路
通過點(diǎn)擊按鈕可以得到一個(gè)算術(shù)表達(dá)式,并且它是一個(gè)字符串類型,然后需要做的就是把這個(gè)字符串轉(zhuǎn)化為計(jì)算機(jī)可計(jì)算的形式。這里就需要用到中綴表達(dá)式轉(zhuǎn)化為后綴表達(dá)式。轉(zhuǎn)化完之后通過棧來一步步的進(jìn)行輸出和計(jì)算,最后輸出結(jié)果。
實(shí)現(xiàn)過程和代碼
1.界面設(shè)計(jì)
設(shè)計(jì)效果如圖所示:
由圖片可知,總共設(shè)計(jì)了兩個(gè)文本框和33個(gè)按鈕(這里的布局是我直接用setBounds()方法設(shè)計(jì)的。因?yàn)槿绻貌季止芾砥鞯脑捯_(dá)到自己想要的效果不太方便)。
在設(shè)計(jì)時(shí)要注意的是:
1.不能把按鈕和文本框直接加入JFrame中,因?yàn)槿绻鸍Frame.setLayout(null)的話,面板上就會(huì)出問題,什么都看不到或者其他情況。因此建議創(chuàng)建一個(gè)JPanel,把JPanel的setLayout()設(shè)置為空。把按鈕和文本框加入到JPanel中,再把JPanel加入到JFrame中。就不會(huì)出問題了。JFrame的布局管理器默認(rèn)為流式布局,位置是中心區(qū)域。
2.設(shè)計(jì)完長度后,寬度一般要再加15個(gè)像素點(diǎn),高度要再加35個(gè)像素點(diǎn).要不然會(huì)有一部分按鈕看不到。如我本意要設(shè)計(jì)的面板寬為600,高為400,則實(shí)際上是
jFrame.setSize(614, 437);
下面就是我設(shè)計(jì)的界面的代碼:
private JFrame jFrame = new JFrame("計(jì)算器"); //設(shè)置字體,是否加粗,大小 private FontUIResource f = new FontUIResource("隸書", 1, 20); private FontUIResource f1 = new FontUIResource("楷書", 0, 35); private JPanel jPanel = new JPanel(); private int buttonx = 100; private int buttony = 50; private int yy = 50; //兩個(gè)文本框 private JTextField jTextField1 = new JTextField("0", 30); private JTextField jTextField2 = new JTextField("輸入要求在main()中", 30); private String str = ""; private String temp = ""; private int indexYN = 0; private String[] strings = { "(", ")", "1/x", "x^2", "x^3", "x^y", "x!", "√", "e", "ln", "log", "%", "sin", "cos", "tan", "π", "+", "=", "7", "8", "9", "0", "-", "4", "5", "6", ".", "*", "1", "2", "3", "c", "÷" }; //33個(gè)按鈕 private JButton[] buttons = new JButton[33]; public void loAsi(){ int tep = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 6; j++) { buttons[tep] = new JButton(strings[tep]); buttons[tep].setFont(f); buttons[tep++].setFocusable(false); } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { buttons[tep] = new JButton(strings[tep]); buttons[tep].setFont(f); buttons[tep++].setFocusable(false); } } int lo = 0; for (int i = 0; i < 2; i++) { for (int j = 0; j < 6; j++) { buttons[lo++].setBounds(buttonx * j, yy * (2 + i), buttonx, buttony); } } for (int i = 0; i < 5; i++) { buttons[lo++].setBounds(buttonx * i, yy * 4, buttonx, buttony); } buttons[lo++].setBounds(buttonx * 5, yy * 4, buttonx, buttony * 4); for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { buttons[lo++].setBounds(buttonx * j, yy * (5 + i), buttonx, buttony); } } jTextField1.setBounds(0, yy, 600, yy);//設(shè)置位置 jTextField1.setFont(f1);設(shè)置字體 jTextField1.setHorizontalAlignment(JLabel.RIGHT);//文本靠右 jTextField1.setEditable(false);//不可修改 jTextField1.setBackground(Color.WHITE);//顏色為白色 jTextField2.setBounds(0, 0, 600, yy); jTextField2.setFont(f1); jTextField2.setHorizontalAlignment(JLabel.RIGHT); jTextField2.setEditable(false); jTextField2.setBackground(Color.WHITE); } public void initButton(){//把按鈕添加到j(luò)Panel中 this.loAsi(); jPanel.add(jTextField1); jPanel.add(jTextField2); for (int i = 0; i < buttons.length; i++) { jPanel.add(buttons[i]); } jPanel.setLayout(null);//布局管理器為空,自定義設(shè)計(jì) } public void init(){ this.initButton(); this.buttonLister(); jFrame.add(jPanel);//把jPanel添加到j(luò)Frame中 jFrame.setSize(614, 437);//jFrame的長寬 jFrame.setResizable(false);//大小不可修改 jFrame.setLocationRelativeTo(null);//位置為顯示器的正中間 //窗口監(jiān)聽器,點(diǎn)右上角的關(guān)閉就關(guān)閉窗口 //可以不寫,應(yīng)為JFrame默認(rèn)的就可以關(guān)閉,不需要寫事件監(jiān)聽器。如果是Frame的話需要寫。 jFrame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); jFrame.setVisible(true);//設(shè)置面板可見 }
2.棧,中綴轉(zhuǎn)后綴
我總共設(shè)計(jì)了12種運(yùn)算符:(+, -, * , /, ^, !, g, l, o, s, c, t)分別是加,減,乘,除,次方,階乘,開根號(hào),,對(duì)數(shù)ln, 對(duì)數(shù)log2, sin, cos, tan
e是Math.E, p是Math.PI, %是直接乘以0.001
把中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式。例如:
中綴表達(dá)式:1+2*3+(l2+3)/2
后綴表達(dá)式為:123*+23+2/+
后綴表達(dá)式的運(yùn)算步驟是:
1.16+23+2/+
2.723+2/+
3.752/+
4.7 2.5+
5.9.5
代碼如下:
private List<String> zhongZhui(String str) {//把輸入的字符串轉(zhuǎn)換成中綴表達(dá)式。存入list中 int index = 0; List<String> list = new ArrayList<>(); do{ char ch = str.charAt(index); if("+-*/^!logsct()".indexOf(str.charAt(index)) >= 0){ //是操作符,直接添加至list中 index ++; list.add(ch+""); }else if (str.charAt(index) == 'e' || str.charAt(index) == 'p'){ index ++; list.add(ch+""); } else if("0123456789".indexOf(str.charAt(index)) >= 0){ //是數(shù)字,判斷多位數(shù)的情況 String str1 = ""; while (index < str.length() && "0123456789.".indexOf(str.charAt(index)) >= 0){ str1 += str.charAt(index); index ++; } list.add(str1); } }while (index < str.length()); return list; } public List<String> houZhui(List<String> list){//中綴表達(dá)式轉(zhuǎn)換稱后綴表達(dá)式 Stack<String> fuZhan = new Stack<>(); List<String> list2 = new ArrayList<>(); if (!list.isEmpty()) { for (int i = 0; i < list.size(); i++) { if (isNumber(list.get(i))){ list2.add(list.get(i)); } else if (list.get(i).charAt(0) == '('){ fuZhan.push(list.get(i)); } else if (isOperator(list.get(i)) && list.get(i).charAt(0) != '('){ if (fuZhan.isEmpty()){ fuZhan.push(list.get(i)); } else {//符棧不為空 if (list.get(i).charAt(0) != ')'){ if (adv(fuZhan.peek()) <= adv(list.get(i))){ //入棧 fuZhan.push(list.get(i)); } else {//出棧 while (!fuZhan.isEmpty() && !"(".equals(fuZhan.peek())){ if(adv(list.get(i)) <= adv(fuZhan.peek())){ list2.add(fuZhan.pop()); } } if (fuZhan.isEmpty() || fuZhan.peek().charAt(0) == '('){ fuZhan.push(list.get(i)); } } } else if (list.get(i).charAt(0) == ')'){ while (fuZhan.peek().charAt(0) != '('){ list2.add(fuZhan.pop()); } fuZhan.pop(); } } } } while (!fuZhan.isEmpty()){ list2.add(fuZhan.pop()); } } else { jTextField1.setText(""); } return list2; } public static boolean isOperator(String op){//判斷是否為操作符 if ("0123456789.ep".indexOf(op.charAt(0)) == -1) { return true; } else { return false; } } public static boolean isNumber(String num){//判斷是否為操作數(shù) if ("0123456789ep".indexOf(num.charAt(0)) >= 0) { return true; } else { return false; } } public static int adv(String f){//判斷操作符的優(yōu)先級(jí) int result = 0; switch(f) { case "+": result = 1; break; case "-": result = 1; break; case "*": result = 2; break; case "/": result = 2; break; case "^": result = 3; break; case "!": result = 4; break; case "g": result = 4; break; case "l": result = 4; break; case "o": result = 4; break; case "s": result = 4; break; case "c": result = 4; break; case "t": result = 4; break; } return result; }
3.判斷錯(cuò)誤
下面是一些我能想到的一些輸入錯(cuò)誤和運(yùn)算錯(cuò)誤:
//當(dāng)只有一位字符時(shí),只能是“0123456789ep”中的一個(gè)
//1.第一個(gè)字符只能為"losctg(0123456789ep"中的一個(gè)
//2.“±/”后面只能是"0123456789losctg(ep"中的一個(gè)
//3.".“后面只能是“0123456789”中的一個(gè)
//4.”!"后面只能是“±/^)”中的一個(gè)
//5."losctg"后面只能是“0123456789(ep”中的一個(gè)
//6."0"的判斷操作
//1.當(dāng)0的上一個(gè)字符不為"0123456789."時(shí),后一位只能是“±/.!^)”中的一個(gè)
//2.如果0的上一位為“.”,則后一位只能是“0123456789±/.^)”中的一個(gè)
//3.如果0到上一個(gè)運(yùn)算符之間沒有“.”的話,整數(shù)位第一個(gè)只能是“123456789”,
// 且后一位只能是“0123456789±/.!^)”中的一個(gè)。
//4.如果0到上一個(gè)運(yùn)算符之間有一個(gè)“.”的話,則后一位只能是“0123456789±/.^)”中的一個(gè)
//7."123456789"后面只能是“0123456789±/.!^)”中的一個(gè)
//8."(“后面只能是“0123456789locstg()ep”中的一個(gè)
//9.”)"后面只能是“±/!^)”中的一個(gè)
//10.最后一位字符只能是“0123456789!)ep”中的一個(gè)
//12.不能有多個(gè)“.”
//13."ep"后面只能是“±*/^)”中的一個(gè)
除數(shù)不能為0
階乘必須為自然數(shù)
ln的x必須大于0
log的x必須大于0
tan的x不能為±(π/2 + kπ)
括號(hào)是否匹配
代碼如下:
public void estimate(){//判斷輸入是否錯(cuò)誤 int i = 0; if (str.length() == 0){ } if (str.length() == 1){ //當(dāng)只有一位字符時(shí),只能是“0123456789ep”中的一個(gè) if ("0123456789ep".indexOf(str.charAt(0)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } } if (str.length() > 1){ for (i = 0; i < str.length() - 1; i++) { //1.第一個(gè)字符只能為"losctg(0123456789ep"中的一個(gè) if ("losctg(0123456789ep".indexOf(str.charAt(0)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //2.“+-*/”后面只能是"0123456789losctg(ep"中的一個(gè) if ("+-*/".indexOf(str.charAt(i)) >= 0 && "0123456789losctg(ep".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //3."."后面只能是“0123456789”中的一個(gè) if (str.charAt(i) == '.' && "0123456789".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //4."!"后面只能是“+-*/^)”中的一個(gè) if (str.charAt(i) == '!' && "+-*/^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //5."losctg"后面只能是“0123456789(ep”中的一個(gè) if ("losctg".indexOf(str.charAt(i)) >= 0 && "0123456789(ep".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //6."0"的判斷操作 if (str.charAt(0) == '0' && str.charAt(1) == '0'){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } if (i >= 1 && str.charAt(i) == '0'){ //&& str.charAt(0) == '0' && str.charAt(1) == '0' int m = i; int n = i; int is = 0; //1.當(dāng)0的上一個(gè)字符不為"0123456789."時(shí),后一位只能是“+-*/.!^)”中的一個(gè) if ("0123456789.".indexOf(str.charAt(m - 1)) == -1 && "+-*/.!^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //2.如果0的上一位為“.”,則后一位只能是“0123456789+-*/.^)”中的一個(gè) if (str.charAt(m - 1) == '.' && "0123456789+-*/.^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } n -= 1; while (n > 0){ if ("(+-*/^glosct".indexOf(str.charAt(n)) >= 0){ break; } if (str.charAt(n) == '.'){ is++; } n--; } //3.如果0到上一個(gè)運(yùn)算符之間沒有“.”的話,整數(shù)位第一個(gè)只能是“123456789”, // 且后一位只能是“0123456789+-*/.!^)”中的一個(gè)。 if ((is == 0 && str.charAt(n) == '0') || "0123456789+-*/.!^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //4.如果0到上一個(gè)運(yùn)算符之間有一個(gè)“.”的話,則后一位只能是“0123456789+-*/.^)”中的一個(gè) if (is == 1 && "0123456789+-*/.^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } if (is > 1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } } //7."123456789"后面只能是“0123456789+-*/.!^)”中的一個(gè) if ("123456789".indexOf(str.charAt(i)) >= 0 && "0123456789+-*/.!^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //8."("后面只能是“0123456789locstg()ep”中的一個(gè) if (str.charAt(i) == '(' && "0123456789locstg()ep".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //9.")"后面只能是“+-*/!^)”中的一個(gè) if (str.charAt(i) == ')' && "+-*/!^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //10.最后一位字符只能是“0123456789!)ep”中的一個(gè) if ("0123456789!)ep".indexOf(str.charAt(str.length() - 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //12.不能有多個(gè)“.” if (i > 2 && str.charAt(i) == '.'){ int n = i - 1; int is = 0; while (n > 0){ if ("(+-*/^glosct".indexOf(str.charAt(n)) >= 0){ break; } if (str.charAt(n) == '.'){ is++; } n--; } if (is > 0){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } } //13."ep"后面只能是“+-*/^)”中的一個(gè) if ("ep".indexOf(str.charAt(i)) >= 0 && "+-*/^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } } } }
4.計(jì)算后綴表達(dá)式
代碼如下:
public double math(List<String> list2) {//通過后綴表達(dá)式進(jìn)行計(jì)算 Stack<String> stack = new Stack<String>(); for (int i = 0; i < list2.size(); i++) { if (isNumber(list2.get(i))) { if (list2.get(i).charAt(0) == 'e'){ stack.push(String.valueOf(Math.E)); } else if (list2.get(i).charAt(0) == 'p'){ stack.push(String.valueOf(Math.PI)); } else { stack.push(list2.get(i)); } } else if (isOperator(list2.get(i))){ double res = 0; if (list2.get(i).equals("+")) { double num2 = Double.parseDouble(stack.pop()); double num1 = Double.parseDouble(stack.pop()); res = num1 + num2; } else if (list2.get(i).equals("-")) { double num2 = Double.parseDouble(stack.pop()); double num1 = Double.parseDouble(stack.pop()); res = num1 - num2; } else if (list2.get(i).equals("*")) { double num2 = Double.parseDouble(stack.pop()); double num1 = Double.parseDouble(stack.pop()); res = num1 * num2; } else if (list2.get(i).equals("/")) { double num2 = Double.parseDouble(stack.pop()); double num1 = Double.parseDouble(stack.pop()); if (num2 != 0){ res = num1 / num2; } else { jTextField1.setText("除數(shù)不能為0"); indexYN = 1; } } else if (list2.get(i).equals("^")) { double num2 = Double.parseDouble(stack.pop()); double num1 = Double.parseDouble(stack.pop()); res = Math.pow(num1, num2); } else if (list2.get(i).equals("!")) { double num1 = Double.parseDouble(stack.pop()); if (num1 == 0 || num1 == 1){ res = 1; } else if (num1 == (int)num1 && num1 > 1){ int d = 1; for (int j = (int)num1; j >0; j--) { d *= j; } res = d; } else { jTextField1.setText("階乘必須為自然數(shù)"); indexYN = 1; } } else if (list2.get(i).equals("g")) { double num1 = Double.parseDouble(stack.pop()); res = Math.sqrt(num1); } else if (list2.get(i).equals("l")) { double num1 = Double.parseDouble(stack.pop()); if (num1 > 0){ res = Math.log(num1); } else { jTextField1.setText("ln的x必須大于0"); indexYN = 1; } } else if (list2.get(i).equals("o")) { double num1 = Double.parseDouble(stack.pop()); if (num1 > 0){ res = Math.log(num1) / Math.log(2); } else { jTextField1.setText("log的x必須大于0"); indexYN = 1; } } else if (list2.get(i).equals("s")) { double num1 = Double.parseDouble(stack.pop()); res = Math.sin(num1); } else if (list2.get(i).equals("c")) { double num1 = Double.parseDouble(stack.pop()); res = Math.cos(num1); } else if (list2.get(i).equals("t")) { double num1 = Double.parseDouble(stack.pop()); if (Math.cos(num1) != 0){ res = Math.tan(num1); } else { jTextField1.setText("tan的x不能為+-(π/2 + kπ)"); indexYN = 1; } } stack.push("" + res); } } if (indexYN == 0){ if (!stack.isEmpty()){ return Double.parseDouble(stack.pop()); } else { return 0; } } else { return -999999; } } public void zhanDui(){//進(jìn)行計(jì)算,并且判斷括號(hào)是否匹配 String khao = ""; int leftkh = 0; int rightkh = 0; int m = 0; //判斷括號(hào)是否成對(duì) for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '('){ khao += '('; leftkh++; } if (str.charAt(i) == ')'){ khao += ')'; rightkh++; } } if (leftkh != rightkh){ jTextField1.setText("輸入錯(cuò)誤!括號(hào)不匹配"); indexYN = 1; } if ((leftkh == 0 && rightkh == 0) || ((leftkh == rightkh && leftkh > 0) && khao.charAt(0) == '(' && khao.charAt(khao.length() - 1) == ')')){ if (indexYN == 0){ List<String> list1 = zhongZhui(str); //System.out.println(list1); List<String> list2 = houZhui(list1); //System.out.println(list2); if (indexYN == 0){ if (math(list2) == -999999){ jTextField1.setText(jTextField1.getText()); } else { jTextField1.setText(String.valueOf(math(list2))); jTextField2.setText(String.valueOf(math(list2))); jTextField2.setText(String.valueOf(math(list2))); } } } } else { jTextField1.setText("輸入錯(cuò)誤!括號(hào)不匹配"); indexYN = 1; } }
5.事件監(jiān)聽器
因?yàn)樵O(shè)置了33個(gè)按鈕,所以每個(gè)按鈕都要設(shè)計(jì)事件監(jiān)聽器,我設(shè)置的目的是把點(diǎn)中的按鈕的值寫入到文本框中輸出,并且把它代表的數(shù)值或者操作符存放到字符串中,以便于進(jìn)行中綴轉(zhuǎn)后綴和運(yùn)算。
代碼如下:
public void buttonLister(){//事件監(jiān)聽器 int tep = 0; buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "("); str += "("; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + ")"); str += ")"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "1/"); str += "1/"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "^2"); str += "^2"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "^3"); str += "^3"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "^"); str += "^"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "!"); str += "!"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "√"); str += "g"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "e"); str += "e"; //str += "2.7182818284590452354"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "ln"); str += "l"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "log"); str += "o"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "%"); str += "*0.01"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "sin"); str += "s"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "cos"); str += "c"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "tan"); str += "t"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "π"); str += "p"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "+"); str += "+"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (str != ""){ estimate(); zhanDui(); } else { str = ""; } } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "7"); str += "7"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "8"); str += "8"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "9"); str += "9"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "0"); str += "0"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "-"); str += "-"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "4"); str += "4"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "5"); str += "5"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "6"); str += "6"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "."); str += "."; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "*"); str += "*"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "1"); str += "1"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "2"); str += "2"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "3"); str += "3"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(""); str = ""; indexYN = 0; } }); buttons[tep].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "÷"); str += "/"; } }); }
完整代碼
直接拷貝到Java中就可以用:
一個(gè)類走完程序。
main函數(shù)中還可以設(shè)置面板風(fēng)格。有
- Niumbus風(fēng)格
- Windows風(fēng)格
- Windows經(jīng)典風(fēng)格
三種風(fēng)格可以自定義選擇,我個(gè)人覺得Niumbus風(fēng)格比較好看。選擇哪一個(gè)就釋放哪一個(gè),把另外兩個(gè)注釋掉就可以了。
package com.lmdxyt; import javax.swing.*; import javax.swing.plaf.FontUIResource; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; import java.util.List; import java.util.Stack; public class JSQ { private JFrame jFrame = new JFrame("計(jì)算器"); private FontUIResource f = new FontUIResource("隸書", 1, 20); private FontUIResource f1 = new FontUIResource("楷書", 0, 35); private JPanel jPanel = new JPanel(); private int buttonx = 100; private int buttony = 50; private int yy = 50; private JTextField jTextField1 = new JTextField("0", 30); private JTextField jTextField2 = new JTextField("輸入要求在main()中", 30); private String str = ""; private String temp = ""; private int indexYN = 0; private String[] strings = { "(", ")", "1/x", "x^2", "x^3", "x^y", "x!", "√", "e", "ln", "log", "%", "sin", "cos", "tan", "π", "+", "=", "7", "8", "9", "0", "-", "4", "5", "6", ".", "*", "1", "2", "3", "c", "÷" }; private JButton[] buttons = new JButton[33]; public void loAsi(){ int tep = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 6; j++) { buttons[tep] = new JButton(strings[tep]); buttons[tep].setFont(f); buttons[tep++].setFocusable(false); } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { buttons[tep] = new JButton(strings[tep]); buttons[tep].setFont(f); buttons[tep++].setFocusable(false); } } int lo = 0; for (int i = 0; i < 2; i++) { for (int j = 0; j < 6; j++) { buttons[lo++].setBounds(buttonx * j, yy * (2 + i), buttonx, buttony); } } for (int i = 0; i < 5; i++) { buttons[lo++].setBounds(buttonx * i, yy * 4, buttonx, buttony); } buttons[lo++].setBounds(buttonx * 5, yy * 4, buttonx, buttony * 4); for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { buttons[lo++].setBounds(buttonx * j, yy * (5 + i), buttonx, buttony); } } jTextField1.setBounds(0, yy, 600, yy); jTextField1.setFont(f1); jTextField1.setHorizontalAlignment(JLabel.RIGHT); jTextField1.setEditable(false); jTextField1.setBackground(Color.WHITE); jTextField2.setBounds(0, 0, 600, yy); jTextField2.setFont(f1); jTextField2.setHorizontalAlignment(JLabel.RIGHT); jTextField2.setEditable(false); jTextField2.setBackground(Color.WHITE); } public void initButton(){//把按鈕添加到j(luò)Panel中 this.loAsi(); jPanel.add(jTextField1); jPanel.add(jTextField2); for (int i = 0; i < buttons.length; i++) { jPanel.add(buttons[i]); } jPanel.setLayout(null); } private List<String> zhongZhui(String str) {//把輸入的字符串轉(zhuǎn)換成中綴表達(dá)式。存入list中 int index = 0; List<String> list = new ArrayList<>(); do{ char ch = str.charAt(index); if("+-*/^!logsct()".indexOf(str.charAt(index)) >= 0){ //是操作符,直接添加至list中 index ++; list.add(ch+""); }else if (str.charAt(index) == 'e' || str.charAt(index) == 'p'){ index ++; list.add(ch+""); } else if("0123456789".indexOf(str.charAt(index)) >= 0){ //是數(shù)字,判斷多位數(shù)的情況 String str1 = ""; while (index < str.length() && "0123456789.".indexOf(str.charAt(index)) >= 0){ str1 += str.charAt(index); index ++; } list.add(str1); } }while (index < str.length()); return list; } public List<String> houZhui(List<String> list){//中綴表達(dá)式轉(zhuǎn)換稱后綴表達(dá)式 Stack<String> fuZhan = new Stack<>(); List<String> list2 = new ArrayList<>(); if (!list.isEmpty()) { for (int i = 0; i < list.size(); i++) { if (isNumber(list.get(i))){ list2.add(list.get(i)); } else if (list.get(i).charAt(0) == '('){ fuZhan.push(list.get(i)); } else if (isOperator(list.get(i)) && list.get(i).charAt(0) != '('){ if (fuZhan.isEmpty()){ fuZhan.push(list.get(i)); } else {//符棧不為空 if (list.get(i).charAt(0) != ')'){ if (adv(fuZhan.peek()) <= adv(list.get(i))){ //入棧 fuZhan.push(list.get(i)); } else {//出棧 while (!fuZhan.isEmpty() && !"(".equals(fuZhan.peek())){ if(adv(list.get(i)) <= adv(fuZhan.peek())){ list2.add(fuZhan.pop()); } } if (fuZhan.isEmpty() || fuZhan.peek().charAt(0) == '('){ fuZhan.push(list.get(i)); } } } else if (list.get(i).charAt(0) == ')'){ while (fuZhan.peek().charAt(0) != '('){ list2.add(fuZhan.pop()); } fuZhan.pop(); } } } } while (!fuZhan.isEmpty()){ list2.add(fuZhan.pop()); } } else { jTextField1.setText(""); } return list2; } public static boolean isOperator(String op){//判斷是否為操作符 if ("0123456789.ep".indexOf(op.charAt(0)) == -1) { return true; } else { return false; } } public static boolean isNumber(String num){//判斷是否為操作數(shù) if ("0123456789ep".indexOf(num.charAt(0)) >= 0) { return true; } else { return false; } } public static int adv(String f){//判斷操作符的優(yōu)先級(jí) int result = 0; switch(f) { case "+": result = 1; break; case "-": result = 1; break; case "*": result = 2; break; case "/": result = 2; break; case "^": result = 3; break; case "!": result = 4; break; case "g": result = 4; break; case "l": result = 4; break; case "o": result = 4; break; case "s": result = 4; break; case "c": result = 4; break; case "t": result = 4; break; } return result; } public void buttonLister(){//事件監(jiān)聽器 int tep = 0; buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "("); str += "("; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + ")"); str += ")"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "1/"); str += "1/"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "^2"); str += "^2"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "^3"); str += "^3"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "^"); str += "^"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "!"); str += "!"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "√"); str += "g"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "e"); str += "e"; //str += "2.7182818284590452354"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "ln"); str += "l"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "log"); str += "o"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "%"); str += "*0.01"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "sin"); str += "s"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "cos"); str += "c"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "tan"); str += "t"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "π"); str += "p"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "+"); str += "+"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (str != ""){ estimate(); zhanDui(); } else { str = ""; } } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "7"); str += "7"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "8"); str += "8"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "9"); str += "9"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "0"); str += "0"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "-"); str += "-"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "4"); str += "4"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "5"); str += "5"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "6"); str += "6"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "."); str += "."; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "*"); str += "*"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "1"); str += "1"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "2"); str += "2"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "3"); str += "3"; } }); buttons[tep++].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(""); str = ""; indexYN = 0; } }); buttons[tep].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextField1.setText(jTextField1.getText() + "÷"); str += "/"; } }); } public void init(){ this.initButton(); this.buttonLister(); jFrame.add(jPanel); jFrame.setSize(614, 437); jFrame.setResizable(false); jFrame.setLocationRelativeTo(null); jFrame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); jFrame.setVisible(true); } public void estimate(){//判斷輸入是否錯(cuò)誤 int i = 0; if (str.length() == 0){ } if (str.length() == 1){ //當(dāng)只有一位字符時(shí),只能是“0123456789ep”中的一個(gè) if ("0123456789ep".indexOf(str.charAt(0)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } } if (str.length() > 1){ for (i = 0; i < str.length() - 1; i++) { //1.第一個(gè)字符只能為"losctg(0123456789ep"中的一個(gè) if ("losctg(0123456789ep".indexOf(str.charAt(0)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //2.“+-*/”后面只能是"0123456789losctg(ep"中的一個(gè) if ("+-*/".indexOf(str.charAt(i)) >= 0 && "0123456789losctg(ep".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //3."."后面只能是“0123456789”中的一個(gè) if (str.charAt(i) == '.' && "0123456789".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //4."!"后面只能是“+-*/^)”中的一個(gè) if (str.charAt(i) == '!' && "+-*/^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //5."losctg"后面只能是“0123456789(ep”中的一個(gè) if ("losctg".indexOf(str.charAt(i)) >= 0 && "0123456789(ep".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //6."0"的判斷操作 if (str.charAt(0) == '0' && str.charAt(1) == '0'){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } if (i >= 1 && str.charAt(i) == '0'){ //&& str.charAt(0) == '0' && str.charAt(1) == '0' int m = i; int n = i; int is = 0; //1.當(dāng)0的上一個(gè)字符不為"0123456789."時(shí),后一位只能是“+-*/.!^)”中的一個(gè) if ("0123456789.".indexOf(str.charAt(m - 1)) == -1 && "+-*/.!^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //2.如果0的上一位為“.”,則后一位只能是“0123456789+-*/.^)”中的一個(gè) if (str.charAt(m - 1) == '.' && "0123456789+-*/.^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } n -= 1; while (n > 0){ if ("(+-*/^glosct".indexOf(str.charAt(n)) >= 0){ break; } if (str.charAt(n) == '.'){ is++; } n--; } //3.如果0到上一個(gè)運(yùn)算符之間沒有“.”的話,整數(shù)位第一個(gè)只能是“123456789”, // 且后一位只能是“0123456789+-*/.!^)”中的一個(gè)。 if ((is == 0 && str.charAt(n) == '0') || "0123456789+-*/.!^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //4.如果0到上一個(gè)運(yùn)算符之間有一個(gè)“.”的話,則后一位只能是“0123456789+-*/.^)”中的一個(gè) if (is == 1 && "0123456789+-*/.^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } if (is > 1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } } //7."123456789"后面只能是“0123456789+-*/.!^)”中的一個(gè) if ("123456789".indexOf(str.charAt(i)) >= 0 && "0123456789+-*/.!^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //8."("后面只能是“0123456789locstg()ep”中的一個(gè) if (str.charAt(i) == '(' && "0123456789locstg()ep".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //9.")"后面只能是“+-*/!^)”中的一個(gè) if (str.charAt(i) == ')' && "+-*/!^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //10.最后一位字符只能是“0123456789!)ep”中的一個(gè) if ("0123456789!)ep".indexOf(str.charAt(str.length() - 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } //12.不能有多個(gè)“.” if (i > 2 && str.charAt(i) == '.'){ int n = i - 1; int is = 0; while (n > 0){ if ("(+-*/^glosct".indexOf(str.charAt(n)) >= 0){ break; } if (str.charAt(n) == '.'){ is++; } n--; } if (is > 0){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } } //13."ep"后面只能是“+-*/^)”中的一個(gè) if ("ep".indexOf(str.charAt(i)) >= 0 && "+-*/^)".indexOf(str.charAt(i + 1)) == -1){ jTextField1.setText("輸入錯(cuò)誤!"); indexYN = 1; } } } } public double math(List<String> list2) {//通過后綴表達(dá)式進(jìn)行計(jì)算 Stack<String> stack = new Stack<String>(); for (int i = 0; i < list2.size(); i++) { if (isNumber(list2.get(i))) { if (list2.get(i).charAt(0) == 'e'){ stack.push(String.valueOf(Math.E)); } else if (list2.get(i).charAt(0) == 'p'){ stack.push(String.valueOf(Math.PI)); } else { stack.push(list2.get(i)); } } else if (isOperator(list2.get(i))){ double res = 0; if (list2.get(i).equals("+")) { double num2 = Double.parseDouble(stack.pop()); double num1 = Double.parseDouble(stack.pop()); res = num1 + num2; } else if (list2.get(i).equals("-")) { double num2 = Double.parseDouble(stack.pop()); double num1 = Double.parseDouble(stack.pop()); res = num1 - num2; } else if (list2.get(i).equals("*")) { double num2 = Double.parseDouble(stack.pop()); double num1 = Double.parseDouble(stack.pop()); res = num1 * num2; } else if (list2.get(i).equals("/")) { double num2 = Double.parseDouble(stack.pop()); double num1 = Double.parseDouble(stack.pop()); if (num2 != 0){ res = num1 / num2; } else { jTextField1.setText("除數(shù)不能為0"); indexYN = 1; } } else if (list2.get(i).equals("^")) { double num2 = Double.parseDouble(stack.pop()); double num1 = Double.parseDouble(stack.pop()); res = Math.pow(num1, num2); } else if (list2.get(i).equals("!")) { double num1 = Double.parseDouble(stack.pop()); if (num1 == 0 || num1 == 1){ res = 1; } else if (num1 == (int)num1 && num1 > 1){ int d = 1; for (int j = (int)num1; j >0; j--) { d *= j; } res = d; } else { jTextField1.setText("階乘必須為自然數(shù)"); indexYN = 1; } } else if (list2.get(i).equals("g")) { double num1 = Double.parseDouble(stack.pop()); res = Math.sqrt(num1); } else if (list2.get(i).equals("l")) { double num1 = Double.parseDouble(stack.pop()); if (num1 > 0){ res = Math.log(num1); } else { jTextField1.setText("ln的x必須大于0"); indexYN = 1; } } else if (list2.get(i).equals("o")) { double num1 = Double.parseDouble(stack.pop()); if (num1 > 0){ res = Math.log(num1) / Math.log(2); } else { jTextField1.setText("log的x必須大于0"); indexYN = 1; } } else if (list2.get(i).equals("s")) { double num1 = Double.parseDouble(stack.pop()); res = Math.sin(num1); } else if (list2.get(i).equals("c")) { double num1 = Double.parseDouble(stack.pop()); res = Math.cos(num1); } else if (list2.get(i).equals("t")) { double num1 = Double.parseDouble(stack.pop()); if (Math.cos(num1) != 0){ res = Math.tan(num1); } else { jTextField1.setText("tan的x不能為+-(π/2 + kπ)"); indexYN = 1; } } stack.push("" + res); } } if (indexYN == 0){ if (!stack.isEmpty()){ return Double.parseDouble(stack.pop()); } else { return 0; } } else { return -999999; } } public void zhanDui(){//進(jìn)行計(jì)算,并且判斷括號(hào)是否匹配 String khao = ""; int leftkh = 0; int rightkh = 0; int m = 0; //判斷括號(hào)是否成對(duì) for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '('){ khao += '('; leftkh++; } if (str.charAt(i) == ')'){ khao += ')'; rightkh++; } } if (leftkh != rightkh){ jTextField1.setText("輸入錯(cuò)誤!括號(hào)不匹配"); indexYN = 1; } if ((leftkh == 0 && rightkh == 0) || ((leftkh == rightkh && leftkh > 0) && khao.charAt(0) == '(' && khao.charAt(khao.length() - 1) == ')')){ if (indexYN == 0){ List<String> list1 = zhongZhui(str); //System.out.println(list1); List<String> list2 = houZhui(list1); //System.out.println(list2); if (indexYN == 0){ if (math(list2) == -999999){ jTextField1.setText(jTextField1.getText()); } else { jTextField1.setText(String.valueOf(math(list2))); jTextField2.setText(String.valueOf(math(list2))); jTextField2.setText(String.valueOf(math(list2))); } } } } else { jTextField1.setText("輸入錯(cuò)誤!括號(hào)不匹配"); indexYN = 1; } } public static void main(String[] args) { /* * 1.在輸入過程中每兩個(gè)數(shù)值之間必須有運(yùn)算符,否則報(bào)錯(cuò). * 例如:錯(cuò)誤:2ln4(5+8), 2sin2 等 * 正確:2*ln4*(5+8), 2*sin2 * 2.不支持負(fù)數(shù),要想得到負(fù)數(shù),只能0-x * 例如:錯(cuò)誤:-2+3*(-2*6) * 正確:0-2+3*((0-2)+6) * */ new JSQ().init(); //Niumbus風(fēng)格 try { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (InstantiationException ex) { ex.printStackTrace(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } /*//Windows風(fēng)格 try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (InstantiationException ex) { ex.printStackTrace(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (UnsupportedLookAndFeelException ex) { ex.printStackTrace(); }*/ /* //Windows經(jīng)典風(fēng)格 try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (InstantiationException ex) { ex.printStackTrace(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (UnsupportedLookAndFeelException ex) { ex.printStackTrace(); }*/ } }
總結(jié)
到此這篇關(guān)于java實(shí)現(xiàn)科學(xué)計(jì)算器的文章就介紹到這了,更多相關(guān)java科學(xué)計(jì)算器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java多線程文件分片下載實(shí)現(xiàn)的示例代碼
這篇文章主要介紹了Java多線程文件分片下載實(shí)現(xiàn)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Java中的自動(dòng)裝箱與自動(dòng)拆箱的實(shí)現(xiàn)
自動(dòng)裝箱和自動(dòng)拆箱使得我們?cè)谑褂没緮?shù)據(jù)類型時(shí)更加方便,同時(shí)也提高了代碼的可讀性和健壯性,本文將詳細(xì)介紹Java中的自動(dòng)裝箱和自動(dòng)拆箱機(jī)制,感興趣的可以了解一下2023-08-08Java import static及import原理區(qū)別解析
這篇文章主要介紹了Java import static及import原理區(qū)別解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10java多線程處理執(zhí)行solr創(chuàng)建索引示例
這篇文章主要介紹了java多線程處理執(zhí)行solr創(chuàng)建索引示例,需要的朋友可以參考下2014-02-02Idea創(chuàng)建多模塊maven聚合項(xiàng)目的實(shí)現(xiàn)
這篇文章主要介紹了Idea創(chuàng)建多模塊maven聚合項(xiàng)目的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12使用feign服務(wù)調(diào)用添加Header參數(shù)
這篇文章主要介紹了使用feign服務(wù)調(diào)用添加Header參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Spring基于常用AspectJ切點(diǎn)表達(dá)式使用介紹
AspectJ是一個(gè)基于Java語言的AOP框架,使用AspectJ需要導(dǎo)入Spring?AOP和AspectJ相關(guān)jar包,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12