当前位置:   article > 正文

Java GUI——实现简单的四则混合运算的计算器_下面的java程序实现了一个简单gui界面的四则运算器,程序运行后界面如下图所示。请

下面的java程序实现了一个简单gui界面的四则运算器,程序运行后界面如下图所示。请

 代码运行如图

 

  1. package 其他;
  2. //计算器,可加减乘除混合运算
  3. //2020.11.25
  4. //18计科2 张阵涛
  5. import java.awt.BorderLayout;
  6. import java.awt.Container;
  7. import java.awt.Dimension;
  8. import java.awt.Font;
  9. import java.awt.GridLayout;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.util.Stack;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JPanel;
  16. import javax.swing.JTextField;
  17. import javax.swing.border.Border;
  18. public class MyCalc {
  19. public static void main(String[] args) {
  20. new myFrame("Calculator");
  21. }
  22. }
  23. class myFrame extends JFrame {
  24. JTextField text = new JTextField();
  25. JButton[] bt1 = new JButton[16];
  26. JButton[] bt2 = new JButton[3];
  27. String key2[] = new String[]{ "BackSpace","Clear", "C"};
  28. String key1[] = new String[] { "1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*",
  29. "0", ".", "=", "/" };
  30. JPanel key1Panel = new JPanel();
  31. JPanel key2Panel = new JPanel();
  32. boolean finish = false;
  33. double ans=0;
  34. public myFrame(String title) {
  35. super(title);
  36. init();
  37. }
  38. public void init() {
  39. text.setHorizontalAlignment(JTextField.RIGHT);
  40. text.setPreferredSize(new Dimension(0, 40));
  41. text.setFont(new Font("微软雅黑", Font.BOLD, 20));
  42. for (int i = 0; i < bt1.length; i++) {
  43. bt1[i] = new JButton(key1[i]);
  44. }
  45. for (int i = 0; i < bt2.length; i++) {
  46. bt2[i] = new JButton(key2[i]);
  47. }
  48. Container pane = this.getContentPane();
  49. this.add(text, BorderLayout.NORTH);
  50. key1Panel.setLayout(new GridLayout(5, 4, 3, 3));
  51. this.add(key2Panel, BorderLayout.CENTER);
  52. this.add(key1Panel, BorderLayout.SOUTH);
  53. for (int i = 0; i < bt1.length; i++) {
  54. key1Panel.add(bt1[i]);
  55. bt1[i].addActionListener(new ActionListener() {
  56. public void actionPerformed(ActionEvent e) {
  57. if (finish == true) {
  58. text.setText("");
  59. finish = false;
  60. }
  61. String command = e.getActionCommand();
  62. String s = text.getText();
  63. if ("0123456789.+-*/".indexOf(command) >= 0)
  64. text.setText(text.getText() + command);
  65. else if (command.equals("Clear"))
  66. text.setText(s.substring(0, s.length() - 1));
  67. else if (command.equals("C") || command.equals("BackSpace"))
  68. text.setText("");
  69. else if (command.equals("=")) {
  70. ans=new calc(s).result;
  71. text.setText(text.getText() + "="+ans);
  72. finish = true;
  73. }
  74. }
  75. });
  76. }
  77. for (int i = 0; i < bt2.length; i++) {
  78. key2Panel.add(bt2[i]);
  79. bt2[i].addActionListener(new ActionListener() {
  80. public void actionPerformed(ActionEvent e) {
  81. if (finish == true) {
  82. text.setText("");
  83. finish = false;
  84. }
  85. String command = e.getActionCommand();
  86. String s = text.getText();
  87. if ("0123456789.+-*/".indexOf(command) >= 0)
  88. text.setText(text.getText() + command);
  89. else if (command.equals("Clear"))
  90. text.setText(s.substring(0, s.length() - 1));
  91. else if (command.equals("C") || command.equals("BackSpace"))
  92. text.setText("");
  93. else if (command.equals("=")) {
  94. ans=new calc(s).result;
  95. text.setText(text.getText() + "="+ans);
  96. finish = true;
  97. }
  98. }
  99. });
  100. }
  101. this.setVisible(true);
  102. this.setLocation(500, 200);
  103. this.setSize(500, 400);
  104. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  105. }
  106. }
  107. class calc {
  108. Stack<String> ops = new Stack<String>();
  109. Stack<Double> num = new Stack<Double>();
  110. String exp;
  111. double result;
  112. public calc(String s) {
  113. exp = s;
  114. calctor();
  115. }
  116. public void calctor() {
  117. String s[] = tranform(exp).split(" ");
  118. for (String string : s) {
  119. if (string.matches("\\d+") || string.matches("\\d+.\\d+")) {
  120. double n = Double.parseDouble(string);
  121. num.push(n);
  122. } else if (string.equals("+") || string.equals("-") || string.equals("/") || string.equals("*")) {
  123. double op1 = num.pop(), op2 = num.pop();
  124. switch (string) {
  125. case "+":
  126. num.push(op1 + op2);
  127. break;
  128. case "-":
  129. num.push(op2 - op1);
  130. break;
  131. case "*":
  132. num.push(op2 * op1);
  133. break;
  134. case "/":
  135. num.push(op2 / op1);
  136. break;
  137. default:
  138. break;
  139. }
  140. } else {
  141. }
  142. }
  143. result = num.peek();
  144. // System.out.println("ans= " + result);
  145. }
  146. public String tranform(String s) {
  147. s = s.replace("+", " + ");
  148. s = s.replace("-", " - ");
  149. s = s.replace("*", " * ");
  150. s = s.replace("/", " / ");
  151. String str[] = s.split(" ");
  152. String s1 = "";
  153. for (String string : str) {
  154. if (string.matches("\\d+") || string.matches("\\d+.\\d+")) {
  155. s1 = s1 + " " + string;
  156. } else {
  157. if (ops.isEmpty()) {
  158. ops.push(string);
  159. continue;// 跳过下面
  160. }
  161. String temp = ops.peek();
  162. while (priority(temp) >= priority(string)) {// 栈顶元素优先级高于或等于进栈元素,不断弹出栈顶元素
  163. s1 = s1 + " " + temp;
  164. ops.pop();
  165. if (ops.isEmpty())
  166. break;
  167. temp = ops.peek();
  168. }
  169. ops.push(string);
  170. }
  171. }
  172. while (!ops.isEmpty())
  173. s1 = s1 + " " + ops.pop();
  174. // System.out.println(s1);
  175. return s1;
  176. }
  177. public int priority(String s) {
  178. switch (s) {
  179. case "+":
  180. case "-":
  181. return 1;
  182. case "*":
  183. case "/":
  184. return 2;
  185. default:
  186. break;
  187. }
  188. return 0;
  189. }
  190. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/275668
推荐阅读
相关标签
  

闽ICP备14008679号