zoukankan      html  css  js  c++  java
  • 2017-2018-2 实验五 《网络编程与安全》实验报告

    一、实验报告封面

    课程:Java程序设计  班级:1652班

    指导教师:娄嘉鹏  实验日期:2018年5月28日

    实验时间:15:35 - 17:15  实验序号:实验五

    实验名称:网络编程与安全

    二、实验内容及步骤

    (一)网络编程与安全-任务1

    实验要求:

    1. 参考数据结构应用

    2. 结对实现中缀表达式转后缀表达式的功能 MyBC.java

    3. 结对实现从上面功能中获取的表达式中实现后缀表达式求值的功能,调用 MyDC.java

    知识点总结

    栈的一个应用是用来对四则运算表达式进行求值。

    表达式 Exp = S1 + OP + S2 (S1 ,S2是两个操作数,OP为运算符)有三种标识方法:

    • OP + S1 + S2 为前缀表示法

    • S1 + OP + S2 为中缀表示法

    • S1 + S2 + OP 为后缀表示法

    dc 运算符

    + 依次弹出 w1 与 w2,将 w2+w1 压栈。精度为结果值精度

    -  弹出 w1 与 w2,将 w2-w1 压栈

    * 依次弹出 w1 与 w2,将 w2w1 压栈。精度为结果值精度与 precision 中较大值

    / 依次弹出 w1 与 w2,将 w2/w1 压栈。精度为 precision

    % 依次弹出 w1 与 w2,将 w2-w2/w1*w1 压栈

    实验代码

    MyDC:

    import java.util.StringTokenizer;
    import java.util.Stack;
    
    public class MyDC {
        private final char ADD = '+';
        private final char SUBTRACT = '-';
        private final char MULTIPLY = '*';
        private final char DIVIDE = '/';
        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();
    
                if (isOperator(token)) {
                    op2 = (stack.pop()).intValue();
                    op1 = (stack.pop()).intValue();
                    result = evalSingleOp(token.charAt(0), op1, op2);
                    stack.push(new Integer(result));
                } else
                    stack.push(new Integer(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;
        }
    }

    MyDCTest:

    import java.util.Scanner;
    public class MyDCTest  {
        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();
                    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"));
                }
                catch (Exception IOException) { 
                    System.out.println("Input exception reported"); 
                } 
        }
    }

    MyBC:

    import java.io.IOException;
    import java.util.Scanner;
    public class MyBC {
        private Stack theStack;
        private String input;
        private String output = "";
        public MyBC(String in) {
            input = in;
            int stackSize = input.length();
            theStack = new Stack(stackSize);
        }
        public String doTrans() {
            for (int j = 0; j < input.length(); j++) {
                char ch = input.charAt(j);
                switch (ch) {
                    case '+':
                    case '-':
                        gotOper(ch, 1);
                        break;
                    case '*':
                    case '/':
                        gotOper(ch, 2);
                        break;
                    case '(':
                        theStack.push(ch);
                        break;
                    case ')':
                        gotParen(ch);
                        break;
                    default:
                        output = output + ch;
                        break;
                }
            }
            while (!theStack.isEmpty()) {
                output = output + theStack.pop();
            }
            System.out.println(output);
            return output;
        }
        public void gotOper(char opThis, int prec1) {
            while (!theStack.isEmpty()) {
                char opTop = theStack.pop();
                if (opTop == '(') {
                    theStack.push(opTop);
                    break;
                }
                else {
                    int prec2;
                    if (opTop == '+' || opTop == '-')
                        prec2 = 1;
                    else
                        prec2 = 2;
                    if (prec2 < prec1) {
                        theStack.push(opTop);
                        break;
                    }
                    else
                        output = output + opTop;
                }
            }
            theStack.push(opThis);
        }
        public void gotParen(char ch){
            while (!theStack.isEmpty()) {
                char chx = theStack.pop();
                if (chx == '(')
                    break;
                else
                    output = output + chx;
            }
        }
        public static void main(String[] args) throws IOException {
            Scanner in = new Scanner(System.in);
            String input = in.nextLine();
            System.out.println(input);
            String output;
            MyBC theTrans = new MyBC(input);
            output = theTrans.doTrans();
        }
        class Stack {
            private int maxSize;
            private char[] stackArray;
            private int top;
            public Stack(int max) {
                maxSize = max;
                stackArray = new char[maxSize];
                top = -1;
            }
            public void push(char j) {
                stackArray[++top] = j;
            }
            public char pop() {
                return stackArray[top--];
            }
            public char peek() {
                return stackArray[top];
            }
            public boolean isEmpty() {
                return (top == -1);
            }
        }
    }

    实验截图

  • 相关阅读:
    springMVC必须的jar包
    project---clean
    maven项目 启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    @ResponseBody 注解
    HttpServletRequest 需要导入xx包?
    Maven中GroupID和ArtifactID
    Java中包、类、方法、属性、常量的命名规则
    使用 @RequestMapping 注解,需要导入的包:spring-webmvc
    【杂题】【动态规划】【搜索】——洛谷P1441砝码称重
    搞清clientHeight、offsetHeight、scrollHeight、offsetTop、scrollTop
  • 原文地址:https://www.cnblogs.com/IconicV/p/9131190.html
Copyright © 2011-2022 走看看