2017-2018-2 20165234 實驗五 《網絡編程與安全》實驗報告

1、實驗報告封面

課程:Java程序設計  班級:1652班  姓名:劉津甫  學號:20165234html

指導教師:婁嘉鵬  實驗日期:2018年5月28日java

實驗時間:15:35 - 17:15  實驗序號:實驗五express

實驗名稱:網絡編程與安全編程

2、實驗內容及步驟

(一)網絡編程與安全-任務1

實驗要求:

1. 參考數據結構應用安全

2. 結對實現中綴表達式轉後綴表達式的功能 MyBC.java網絡

3. 結對實現從上面功能中獲取的表達式中實現後綴表達式求值的功能,調用 MyDC.java數據結構

知識點總結

棧的一個應用是用來對四則運算表達式進行求值。post

表達式 Exp = S1 + OP + S2 (S1 ,S2是兩個操做數,OP爲運算符)有三種標識方法:lua

  • OP + S1 + S2 爲前綴表示法spa

  • 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);
        }
    }
}

實驗截圖

相關文章
相關標籤/搜索