• 20155226 mini DC 课堂测试补交


    由于电脑突然出了点问题,我没有完成mini DC这个测试,现将测试内容及结果补交

    题目如下

    提交测试截图和码云练习项目链接,实现Linux下dc的功能,计算后缀表达式的值

    • 代码如下
      • MyDC.class
    import com.sun.xml.internal.fastinfoset.util.CharArray;
    import java.util.StringTokenizer;
    import java.util.Stack;
    public class MyDC {
        /**
         * constant for addition symbol
         */
        private final char ADD = '+';
        /**
         * constant for subtraction symbol
         */
        private final char SUBTRACT = '-';
        /**
         * constant for multiplication symbol
         */
        private final char MULTIPLY = '*';
        /**
         * constant for division symbol
         */
        private final char DIVIDE = '/';
        /**
         * the stack
         */
        private Stack<Integer> stack;
    
        public MyDC() {
            stack = new Stack<Integer>();
        }
    
        public int evaluate(String expr) {
            int op1, op2, result = 0;
            String token;
            StringTokenizer tokenizer = new StringTokenizer(expr);
    
            while (tokenizer.hasMoreTokens()) {
                token = tokenizer.nextToken();
                //如果是运算符,调用isOperator
                if (isOperator(token)==true) {
                    op2=stack.pop();
                    op1=stack.pop();
                    //从栈中弹出操作数2
                    //从栈中弹出操作数1
                    result=evalSingleOp(token.charAt(0),op1,op2);
                    //根据运算符和两个操作数调用evalSingleOp计算result;
                    stack.push(result);
                    //计算result入栈;
                }
                else//如果是操作数
                {
                    stack.push(Integer.parseInt(token));
                }
                //操作数入栈;
            }
    
            return result;
        }
    
        private boolean isOperator(String token) {
            return (token.equals("+") || token.equals("-") ||
                    token.equals("*") || token.equals("/"));
        }
    
        private int evalSingleOp(char operation, int op1, int op2) {
            int result = 0;
    
            switch (operation) {
                case ADD:
                    result = op1 + op2;
                    break;
                case SUBTRACT:
                    result = op1 - op2;
                    break;
                case MULTIPLY:
                    result = op1 * op2;
                    break;
                case DIVIDE:
                    result = op1 / op2;
            }
            return result;
        }
    }
    
    * **MyDCTester.class**
    
    import java.util.Scanner;
             public class MyDCTester  {
    
                public static void main (String[] args) {
            String expression, again;
    
            int result;
    
                 try
                {
                       Scanner in = new Scanner(System.in);
    
                       do
                           {
                           MyDC evaluator = new MyDC();
                             System.out.println ("Enter a valid postfix expression: ");
                           expression = in.nextLine();
    
                          result = evaluator.evaluate (expression);
                            System.out.println ("That expression equals " + result);
                             System.out.print ("Evaluate another expression [Y/N]? ");
                           again = in.nextLine();
                          System.out.println();
                          }
                     while (again.equalsIgnoreCase("y"));
                  }![](http://images2015.cnblogs.com/blog/1071653/201705/1071653-20170503220452023-968944949.jpg)
    
                catch (Exception IOException)
             {
                     System.out.println("Input exception reported");
                }
            }
     }
    
    • 测试截图

  • 相关阅读:
    doT.js——前端javascript模板引擎问题备忘录
    (转)regex类(个人理解)
    ajax提交表单、ajax实现文件上传
    SQL添加表字段
    Elasticsearch使用总结
    有一张表里面有上百万的数据,在做查询的时候,如何优化?从数据库端,java端和查询语句上回答
    sql语句的字段转成Date
    Mybatis 示例之 foreach
    Eclipse不编译解决方案
    Java使用RSA加密解密及签名校验
  • 原文地址:https://www.cnblogs.com/20155226thy/p/6804232.html
Copyright © 2020-2023  润新知