当前位置:   article > 正文

Java 计算器的实现(两种不同思路)_java 计算器的model怎么写

java 计算器的model怎么写

这几天java课上老师要我们实现一个计算器。由于刚开始学习java,其中界面显示部分的代码老师已经准备好了,并且整个程序是采用MVCModel–view–controller点击打开链接 的设计模式,我们要实现的只是其中的Model,即核心的算法模型。先看看用户界面(View部分)吧。


一、最初仅提供了基本用户界面的代码

为了让大家方便试验计算器程序,现把计算器的实现代码发上来。下面的是老师发布题目的代码。

     其中只有Calculator.java是MVC中的Model部分,这次也只需要修改这部分代码。其它部分提供了界面的布局、按钮等,不需要改变。

(1)最初的 Calculator类 定义

// Calculator.java
  1. // The core of the great calculator
  2. // Check the "TODO"s!!
  3. class Calculator {
  4. String expression = "0";
  5. // TODO: modify the method to return a proper expression
  6. // which will be shown in the screen of the calculator
  7. String getExpression() {
  8. return expression;
  9. }
  10. // TODO: modify the method to handle the key press event
  11. void keyPressed(char key) {
  12. expression += key;
  13. }
  14. // TODO: you can modify this method to print any debug
  15. // information (It will be called by CalculatorCmd)
  16. void debugPrintStatus() {
  17. System.out.println("Expression = " + expression);
  18. }
  19. }

(2)计算器图形版 程序入口

// CalculatorApp.java
  1. // Define entry point of the calculator application
  2. class CalculatorApp {
  3. public static void main(String[] args) {
  4. CalculatorWindow mainwnd = new CalculatorWindow();
  5. CalculatorController control = new CalculatorController();
  6. mainwnd.setController(control);
  7. mainwnd.setVisible(true);
  8. }
  9. }

(3)计算器命令行版 程序入口

// CalculatorCmd.java
  1. // A calculator with command line interface
  2. import java.io.*;
  3. public class CalculatorCmd {
  4. public static void main(String[] args) throws IOException {
  5. System.out.println("Welcome to use commoand line calculator.");
  6. Calculator calculator = new Calculator();
  7. while (true) {
  8. calculator.debugPrintStatus();
  9. System.out.println("\nEXP = " + calculator.getExpression());
  10. System.out.print("input: ");
  11. char c = (char)System.in.read();
  12. if (c == 'x' || c == 'X') break;
  13. calculator.keyPressed(c);
  14. }
  15. System.out.println("\nBye.");
  16. }
  17. }
(4)控制器(Control)部分,将按下的 按键key 移交给keypressed(char key)函数,这也是计算器需要编写的重点。
// CalculatorController.java
  1. // The controller of the calculator application
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. class CalculatorController implements ActionListener {
  5. Calculator calc = new Calculator();
  6. JLabel window;
  7. public void actionPerformed(ActionEvent e) {
  8. char key = e.getActionCommand().charAt(0);
  9. calc.keyPressed(key);
  10. if (window!=null) {
  11. window.setText(calc.getExpression());
  12. }
  13. }
  14. void setDisplayWindow(JLabel w) {
  15. window = w;
  16. }
  17. }
(5)计算器的窗口布局,有点繁琐,暂时可以不用细究。
  1. // The window of the calculator application
  2. // Layout the buttons, and register the button action events
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import javax.swing.border.*;
  6. import java.awt.event.*;
  7. class CalculatorWindow extends JFrame {
  8. JLabel disp;
  9. JButton cancelButton, equalButton, dotButton;
  10. JButton signButton, addButton, subButton, mulButton, divButton;
  11. JButton[] numButton;
  12. CalculatorWindow() {
  13. this.setSize(400, 440);
  14. this.setResizable(false);
  15. this.setTitle("Java Calculator");
  16. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17. Container wnd = getContentPane();
  18. wnd.setLayout(null);
  19. JPanel dispPanel = new JPanel();
  20. JPanel controlPanel = new JPanel();
  21. wnd.add(dispPanel);
  22. wnd.add(controlPanel);
  23. dispPanel.setBounds(0,0,400,60);
  24. controlPanel.setBounds(0,60,400,360);
  25. dispPanel.setBorder(new LineBorder(Color.GRAY));
  26. disp = new JLabel("0");
  27. // disp.setBorder(new LineBorder(Color.RED));
  28. disp.setSize(new Dimension(380, 60));
  29. Font dispFont = new Font("Arial", Font.PLAIN, 24);
  30. disp.setFont(dispFont);
  31. disp.setHorizontalAlignment(SwingConstants.RIGHT);
  32. dispPanel.setLayout(null);
  33. dispPanel.add(disp);
  34. // dispPanel.setMinimumSize(new Dimension(400, 500));
  35. GridBagLayout gridbag = new GridBagLayout();
  36. // controlPanel.setLayout(new GridLayout(4,4,10,20));
  37. controlPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  38. controlPanel.setLayout(gridbag);
  39. controlPanel.setBorder(new EmptyBorder(20,10,20,10));
  40. cancelButton = new JButton("c");
  41. signButton = new JButton("+/-");
  42. addButton = new JButton("+");
  43. subButton = new JButton("-");
  44. mulButton = new JButton("*");
  45. divButton = new JButton("/");
  46. equalButton = new JButton("=");
  47. numButton = new JButton[10];
  48. for(int i=0; i<10; i++) {
  49. numButton[i] = new JButton(String.valueOf(i));
  50. }
  51. dotButton = new JButton(".");
  52. GridBagConstraints c = new GridBagConstraints();
  53. c.insets = new Insets(3,2,3,2);
  54. c.fill = GridBagConstraints.BOTH;
  55. c.weightx = 1.0;
  56. c.weighty = 1.0;
  57. gridbag.setConstraints(mulButton, c);
  58. controlPanel.add(mulButton);
  59. gridbag.setConstraints(divButton, c);
  60. controlPanel.add(divButton);
  61. gridbag.setConstraints(signButton, c);
  62. controlPanel.add(signButton);
  63. c.gridwidth = GridBagConstraints.REMAINDER;
  64. gridbag.setConstraints(cancelButton, c);
  65. controlPanel.add(cancelButton);
  66. c.gridwidth = 1;
  67. gridbag.setConstraints(subButton, c);
  68. controlPanel.add(subButton);
  69. gridbag.setConstraints(numButton[9], c);
  70. controlPanel.add(numButton[9]);
  71. gridbag.setConstraints(numButton[8], c);
  72. controlPanel.add(numButton[8]);
  73. c.gridwidth = GridBagConstraints.REMAINDER;
  74. gridbag.setConstraints(numButton[7], c);
  75. controlPanel.add(numButton[7]);
  76. c.gridwidth = 1;
  77. gridbag.setConstraints(addButton, c);
  78. controlPanel.add(addButton);
  79. gridbag.setConstraints(numButton[6], c);
  80. controlPanel.add(numButton[6]);
  81. gridbag.setConstraints(numButton[5], c);
  82. controlPanel.add(numButton[5]);
  83. c.gridwidth = GridBagConstraints.REMAINDER;
  84. gridbag.setConstraints(numButton[4], c);
  85. controlPanel.add(numButton[4]);
  86. c.gridwidth = 1;
  87. c.gridheight = 2;
  88. gridbag.setConstraints(equalButton, c);
  89. controlPanel.add(equalButton);
  90. c.gridheight = 1;
  91. gridbag.setConstraints(numButton[3], c);
  92. controlPanel.add(numButton[3]);
  93. gridbag.setConstraints(numButton[2], c);
  94. controlPanel.add(numButton[2]);
  95. c.gridwidth = GridBagConstraints.REMAINDER;
  96. gridbag.setConstraints(numButton[1], c);
  97. controlPanel.add(numButton[1]);
  98. c.gridwidth = 1;
  99. gridbag.setConstraints(dotButton, c);
  100. controlPanel.add(dotButton);
  101. c.gridwidth = GridBagConstraints.REMAINDER;
  102. gridbag.setConstraints(numButton[0], c);
  103. controlPanel.add(numButton[0]);
  104. this.addWindowListener(new WindowAdapter() {
  105. public void windowOpened(WindowEvent e) {
  106. equalButton.requestFocus();
  107. }
  108. });
  109. }
  110. class CalculatorHotKey extends KeyAdapter {
  111. public void keyPressed(KeyEvent e) {
  112. char key = e.getKeyChar();
  113. if (key >= '0' && key <= '9') {
  114. numButton[key - '0'].doClick();
  115. } else switch(e.getKeyChar()) {
  116. case 'c':
  117. cancelButton.doClick();
  118. break;
  119. case '~':
  120. signButton.doClick();
  121. break;
  122. case '+':
  123. addButton.doClick();
  124. break;
  125. case '-':
  126. subButton.doClick();
  127. break;
  128. case '*':
  129. mulButton.doClick();
  130. break;
  131. case '/':
  132. divButton.doClick();
  133. break;
  134. case '=':
  135. equalButton.doClick();
  136. break;
  137. case '.':
  138. dotButton.doClick();
  139. break;
  140. case '\n':
  141. equalButton.doClick();
  142. break;
  143. }
  144. }
  145. }
  146. void setController(CalculatorController control) {
  147. control.setDisplayWindow(disp);
  148. CalculatorHotKey keyMap = new CalculatorHotKey();
  149. cancelButton.addKeyListener(keyMap);
  150. cancelButton.addActionListener(control);
  151. cancelButton.setActionCommand("c");
  152. signButton.addKeyListener(keyMap);
  153. signButton.addActionListener(control);
  154. signButton.setActionCommand("~");
  155. addButton.addKeyListener(keyMap);
  156. addButton.addActionListener(control);
  157. addButton.setActionCommand("+");
  158. subButton.addKeyListener(keyMap);
  159. subButton.addActionListener(control);
  160. subButton.setActionCommand("-");
  161. mulButton.addKeyListener(keyMap);
  162. mulButton.addActionListener(control);
  163. mulButton.setActionCommand("*");
  164. divButton.addKeyListener(keyMap);
  165. divButton.addActionListener(control);
  166. divButton.setActionCommand("/");
  167. equalButton.addKeyListener(keyMap);
  168. equalButton.addActionListener(control);
  169. equalButton.setActionCommand("=");
  170. dotButton.addKeyListener(keyMap);
  171. dotButton.addActionListener(control);
  172. dotButton.setActionCommand(".");
  173. for(int i=0; i<10; i++) {
  174. numButton[i].addKeyListener(keyMap);
  175. numButton[i].addActionListener(control);
  176. numButton[i].setActionCommand(String.valueOf(i));
  177. }
  178. }
  179. }


==========================================

二、采用状态机(State Machine)思路编写的计算器核心模型(Calculator.java)

       基于从老师课上演示得到的灵感,感觉整个计算器的行为表现得就像一个状态机(StateMachine)。上学期学EDA时,利用VHDL语言在FPGA硬件平台上完成许多任务时也是建立状态机模型来实现,我想那不妨就用状态机模型来指导整个程序的编写吧。我整理了一下计算器的各种状态,画了一张状态转移图。当然,或许还可以再设计出更简单的状态图。

       就像图例所示,每个状态有各自的状态编号st(i),i=1,2,..,5;初始状态为复位(st0)。

每个状态根据键盘不同的输入跳转到不同的下一个状态,在每个状态中执行相应的操作。如下图:

      [1] 操作数置换:因为输入完第二操作数后,不是按等号,而是继续按下运算符,于是先把结果记作第1操作数,清空第二操作数,并继续进入输入运算符的状态。


在程序中,状态的编号是用枚举变量实现的,这个状态机的结构是在keyPressed(char key)函数中利用一个switch(c_state)实现的,其中c_state意味着当前状态(current state)。在每一个状态下会执行那个状态的服务函数fun_sti(),i=1,2,..,5;在这些函数内实现当前状态的任务以及状态跳转的控制

       我自己觉得用这种思路去写程序,容易方便程序的不断“升级壮大”,因为每次我只需要仔细地完成某个状态的代码便可开始试运行,就算是某个状态的代码出错了,还不会影响到其它状态的执行,容易找出错误位置。还容易依此添加新的控制能力,就比如说要控制往屏幕上输出消息,可以拿当前状态为依据输出不同的消息:

完整的实现代码如下:

// Calculator.java
  1. // The core of the great calculator
  2. // Check the "TODO"s!!
  3. class Calculator {
  4. private double result=0.0;
  5. private char[] symbols={'+','-','*','/'}; // 定义将会使用到的符号
  6. private char symbol_use=' '; // 最近一次运算被选中的符号,默认为空
  7. private enum STATE {st0, st1, st2, st3, st4, st5}; // 状态图中使用的状态
  8. private STATE c_state= STATE.st1; // 初始状态直接从 st1 开始好了
  9. private OperaNum op_left = new OperaNum(), // 定义左、右操作数
  10. op_right = new OperaNum();
  11. // 在屏幕上显示的内容,依据当前的不同状态来定。
  12. String getExpression() {
  13. switch(c_state){
  14. case st0:
  15. case st1:
  16. return op_left.STR_value();
  17. case st2:
  18. return op_left.STR_value() + symbol_use;
  19. case st3:
  20. return op_left.STR_value() + symbol_use + op_right.STR_value();
  21. case st4:
  22. return "= " + result;
  23. default:
  24. return "0";
  25. }
  26. }
  27. // 下面是处于各状态需要做的事情
  28. private void fun_st0(){
  29. op_left.setClear();
  30. op_right.setClear();
  31. result = 0.0;
  32. c_state = STATE.st1;
  33. }
  34. private void fun_st1_basic(char key){
  35. if( (key>='0' && key<='9') || key=='.' || key=='~' ){
  36. op_left.pushDigital(key);
  37. c_state = STATE.st1;
  38. }
  39. else if( key=='=' ){
  40. // result =
  41. symbol_use = '=';
  42. fun_result();
  43. c_state = STATE.st4;
  44. }
  45. else if( key=='c' ){ // 按下C键
  46. fun_st0();
  47. c_state = STATE.st0;
  48. }
  49. else { // 输入运算符号
  50. fun_st2(key);
  51. c_state = STATE.st2;
  52. }
  53. }
  54. private void fun_st2(char key){
  55. if( key=='+' || key=='-' || key=='*' || key=='/'){
  56. symbol_use = key;
  57. c_state = STATE.st2;
  58. }
  59. else if( (key>='0' && key<='9') || key=='.' || key=='~' ){
  60. fun_st3(key);
  61. c_state = STATE.st3;
  62. }
  63. }
  64. private void fun_st3(char key){
  65. if( (key>='0' && key<='9') || key=='.' || key=='~' ){
  66. op_right.pushDigital(key);
  67. c_state = STATE.st3;
  68. }
  69. else if( key=='=' ){
  70. // result =
  71. fun_result();
  72. c_state = STATE.st4;
  73. }
  74. else if( key=='c' ){ // 按下C键
  75. fun_st0();
  76. c_state = STATE.st0;
  77. }
  78. else{
  79. c_state = STATE.st5; // 按下运算符号
  80. fun_st5(key);
  81. }
  82. }
  83. void fun_st4(char key){ // 此时,已经显示出了结果
  84. if( (key>='0' && key<='9') || key=='.' || key=='~' ){
  85. fun_st0();
  86. op_left.pushDigital(key);
  87. c_state = STATE.st1;
  88. }
  89. else if( key=='+' || key=='-' || key=='*' || key=='/' ){ // 输入运算符号
  90. op_left.setValue(result);
  91. op_right.setClear();
  92. fun_st2(key);
  93. c_state = STATE.st2;
  94. }
  95. }
  96. void fun_st5(char key){
  97. fun_result();
  98. op_left.setValue(result);
  99. op_right.setClear();
  100. fun_st2(key);
  101. c_state = STATE.st2;
  102. }
  103. private void fun_result(){
  104. switch(symbol_use){
  105. case '+':
  106. result = op_left.value() + op_right.value();
  107. break;
  108. case '-':
  109. result = op_left.value() - op_right.value();
  110. break;
  111. case '*':
  112. result = op_left.value() * op_right.value();
  113. break;
  114. case '/':
  115. if( op_right.value()==0.0 )
  116. result = Double.POSITIVE_INFINITY;
  117. else
  118. result = op_left.value() / op_right.value();
  119. break;
  120. default: // 等号,或来自 st1 的直接按下等号
  121. result = op_left.value();
  122. break;
  123. }
  124. }
  125. // 在此函数中设定状态机模型
  126. void keyPressed(char key) {
  127. //expression += key;
  128. if(key=='c')
  129. c_state = STATE.st0; // 复位一下
  130. switch(c_state){
  131. case st0: fun_st0(); // 初始状态,并且等待输入
  132. case st1: fun_st1_basic(key); break; // 输入左操作数
  133. // ===============
  134. case st2: fun_st2(key); break;
  135. case st3: fun_st3(key); break;
  136. case st4: fun_st4(key); break;
  137. case st5: fun_st5(key); break;
  138. default:
  139. fun_st0();
  140. c_state = STATE.st0;
  141. break;
  142. }
  143. }
  144. // TODO: you can modify this method to print any debug
  145. // information (It will be called by CalculatorCmd)
  146. void debugPrintStatus() {
  147. // System.out.println("Expression = " + expression);
  148. System.out.println( "c_state" + c_state );
  149. }
  150. }


上面还需要补充一点,为了方便进行左、右操作数的运算, 操作数单独提出来作为一种OperaNum类来实现,另写为OperaNum.java如下:

  1. /* 操作数用类来实现,统一各种运算
  2. --> 与数字有关的所有符号:【0-9】,【.】,【~】
  3. */
  4. public class OperaNum{
  5. String expression="0"; // 操作数的字符串表达式
  6. private double v=0.0; // 操作数的值
  7. private boolean SIGN=true; // true= pos, false= minus.
  8. OperaNum(double v){
  9. setValue(v);
  10. }
  11. OperaNum(){}
  12. void pushDigital(char key){ // 用于实现状态图中的“输入操作数”
  13. // (1) 输入普通数字
  14. if( key>='0' && key<='9')
  15. if( expression.equals("0") ){ // 若字符串是初始状态0,再按0无反应
  16. if( key=='0')
  17. expression="0";
  18. else
  19. expression = String.valueOf(key);
  20. }
  21. else
  22. expression +=key;
  23. if(key=='.') // (2) 处理小数点
  24. pushDot(key);
  25. if(key=='~') // (3) 处理符号
  26. SIGN = !SIGN;
  27. v= value();
  28. }
  29. void pushDot(char key){ // 帮助pushDigital()函数来输入小数点,这里要检查是否合乎规范
  30. if( expression.indexOf('.')<=0 ) // 若不成立,则表达式已经有小数点了
  31. expression += '.';
  32. }
  33. void setValue(double init_num){ // 强制设定操作数,一般不用
  34. v = init_num;
  35. expression = String.valueOf(v);
  36. }
  37. void setClear(){
  38. v = 0;
  39. expression = "0";
  40. SIGN = true;
  41. }
  42. double value(){ // 返回当前操作数的值,还要根据是否加入了负号来决定取值
  43. v= Double.parseDouble(expression);
  44. return SIGN? v : (-1)*v;
  45. }
  46. String STR_value(){ // 返回当前操作数的字符串表达式,
  47. return SIGN? expression : "(-"+expression+")";
  48. }
  49. }


三、用简单的if语句控制的计算器核心模型(Calculator.java)

换一种思路,你说要简单来看嘛,可以把整个运行就分作3种状态:输入第1个操作数(INPUT_1)、输入运算符(INPUT_OP)、输入第二个操作数(INPUT_2)、已经输出了结果(SHOW)。这可以在Calculator类的开头定义出几个状态来用:

  1. private final int INPUT_1 = 1;
  2. private final int INPUT_OP = 2;
  3. private final int INPUT_2 = 3;
  4. private final int SHOW = 4;
  5. private int INPUT_STATE = 1; // 存放程序当前所处状态

当程序处在INPUT_1时,若按下了运算符,则进入INPUT_OP状态;

        在INPUT_OP状态时若按下了数字则进入了INPUT_2状态;之后若按下了等号则进入SHOW状态,并再自动进入INPUT_1状态。

在INPUT_2状态时,若按下了运算符,则先把当前结果记作第1操作数,清空第2操作数,并进入INPUT_OP状态。

实现代码如下:

// Calculator.java
  1. // The core of the great calculator
  2. // Check the "TODO"s!!
  3. class MyNum{
  4. String expression="0"; // 操作数的字符串表达式
  5. private double value=0.0; // 操作数的值
  6. boolean SIGN=true; // 表示数字是正数还是负数
  7. MyNum(){}
  8. void pushDigital(char key){
  9. if( key>='0' && key<='9') // 输入数字
  10. if( expression.equals("0") ){ // 若字符串是初始状态0,再按0无反应
  11. if( key=='0')
  12. expression="0";
  13. else
  14. expression = String.valueOf(key);
  15. }
  16. else
  17. expression += key;
  18. else if(key=='.') // 小数点
  19. pushDot(key);
  20. else if(key=='~') // 正负数的转变
  21. SIGN = !SIGN;
  22. }
  23. void pushDot(char key){ // 如果已经输入过小数点了,就不再输入
  24. if( expression.indexOf('.')<0 )
  25. expression += '.';
  26. }
  27. void set(double num){ // 强制设定操作数,一般不用
  28. value = num;
  29. expression = String.valueOf(value);
  30. }
  31. void clear(){
  32. value = 0;
  33. expression = "0";
  34. SIGN = true;
  35. }
  36. double read_value(){ // 返回当前操作数的值,还要根据是否加入了负号来决定取值
  37. value = Double.parseDouble(expression);
  38. return SIGN? value : (-1)*value;
  39. }
  40. String get_exp(){ // 返回当前操作数的字符串表达式,
  41. return SIGN? expression : " -"+expression;
  42. }
  43. }
  44. class Calculator {
  45. String expression = "0";
  46. char operator=' ';
  47. double result=0.0;
  48. MyNum left_op = new MyNum(),
  49. right_op = new MyNum();
  50. private final int INPUT_1 = 1;
  51. private final int INPUT_OP = 2;
  52. private final int INPUT_2 = 3;
  53. private final int SHOW = 4;
  54. private int INPUT_STATE = 1; // 存放程序当前所处状态
  55. // TODO: modify the method to return a proper expression
  56. // which will be shown in the screen of the calculator
  57. String getExpression() {
  58. if(INPUT_STATE==INPUT_1){
  59. expression = left_op.get_exp();
  60. }
  61. else if(INPUT_STATE==INPUT_OP){
  62. expression = "" + left_op.get_exp() + operator;
  63. }
  64. else if(INPUT_STATE==INPUT_2){
  65. expression = "" + left_op.get_exp() + operator + right_op.get_exp();
  66. }
  67. else if(INPUT_STATE==SHOW)
  68. expression = "= " + result;
  69. return expression;
  70. }
  71. // TODO: modify the method to handle the key press event
  72. void keyPressed(char key) {
  73. // expression += key;
  74. if(key=='c'){
  75. left_op.clear();
  76. right_op.clear();
  77. expression = "0";
  78. operator=' ';
  79. INPUT_STATE = INPUT_1;
  80. }
  81. if( key>='0' && key<='9' || key=='.' || key=='~'){
  82. if (INPUT_STATE == SHOW) {
  83. left_op.clear();
  84. right_op.clear();
  85. expression = "0";
  86. operator=' ';
  87. INPUT_STATE = INPUT_1;
  88. left_op.pushDigital(key);
  89. }
  90. else if(INPUT_STATE==INPUT_1){
  91. left_op.pushDigital(key);
  92. }
  93. else if (INPUT_STATE==INPUT_OP){
  94. INPUT_STATE = INPUT_2;
  95. right_op.pushDigital(key);
  96. }
  97. else if (INPUT_STATE==INPUT_2) {
  98. right_op.pushDigital(key);
  99. }
  100. }
  101. if( key=='+' || key=='-' || key=='*' || key=='/' ){
  102. if(INPUT_STATE==INPUT_1)
  103. INPUT_STATE=INPUT_OP;
  104. else if(INPUT_STATE == SHOW){
  105. left_op.set(result);
  106. right_op.clear();
  107. INPUT_STATE=INPUT_OP;
  108. }
  109. // else if(INPUT_STATE)
  110. operator = key;
  111. }
  112. if(key=='='){
  113. result = get_result();
  114. INPUT_STATE = SHOW;
  115. }
  116. }
  117. double get_result(){
  118. switch(operator){
  119. case '+': return left_op.read_value() + right_op.read_value();
  120. case '-': return left_op.read_value() - right_op.read_value();
  121. case '*': return left_op.read_value() * right_op.read_value();
  122. case '/':
  123. if(right_op.read_value()==0) return Double.POSITIVE_INFINITY;
  124. else return left_op.read_value() / right_op.read_value();
  125. default: return left_op.read_value();
  126. }
  127. }
  128. // TODO: you can modify this method to print any debug
  129. // information (It will be called by CalculatorCmd)
  130. void debugPrintStatus() {
  131. System.out.println("Expression = " + expression);
  132. }
  133. }






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

闽ICP备14008679号