1. 程式人生 > 實用技巧 >20192302 郝奕軒 2020-2021-1 《資料結構與面向物件程式設計》實驗八報告

20192302 郝奕軒 2020-2021-1 《資料結構與面向物件程式設計》實驗八報告

20192302 2020-2021-1 《資料結構與面向物件程式設計》實驗八 報告

課程:《程式設計與資料結構》
班級: 1923
姓名: 郝奕軒
學號:20192302
實驗教師:王志強
實驗日期:2020年12月3日
必修/選修: 必修

1.實驗內容

  • (1)參考教材PP16.1,完成鏈樹LinkedBinaryTree的實現(getRight,contains,toString,preorder,postorder)
    用JUnit或自己編寫驅動類對自己實現的LinkedBinaryTree進行測試,提交測試程式碼執行截圖,要全屏,包含自己的學號資訊
    課下把程式碼推送到程式碼託管平臺
  • (2)基於LinkedBinaryTree,實現基於(中序,先序)序列構造唯一一棵二㕚樹的功能,比如給出中序HDIBEMJNAFCKGL和先序ABDHIEJMNCFGKL,構造出附圖中的樹
    用JUnit或自己編寫驅動類對自己實現的功能進行測試,提交測試程式碼執行截圖,要全屏,包含自己的學號資訊
    課下把程式碼推送到程式碼託管平臺
  • (3)自己設計並實現一顆決策樹
    提交測試程式碼執行截圖,要全屏,包含自己的學號資訊
    課下把程式碼推送到程式碼託管平臺
  • (4)輸入中綴表示式,使用樹將中綴表示式轉換為字尾表示式,並輸出字尾表示式和計算結果(如果沒有用樹,正常評分。如果用到了樹,即使有小的問題,也酌情給滿分)
    提交測試程式碼執行截圖,要全屏,包含自己的學號資訊

2. 實驗過程及結果

  • (1)參考教材PP16.1,完成鏈樹LinkedBinaryTree的實現(getRight,contains,toString,preorder,postorder)
    用JUnit或自己編寫驅動類對自己實現的LinkedBinaryTree進行測試,提交測試程式碼執行截圖,要全屏,包含自己的學號資訊
    課下把程式碼推送到程式碼託管平臺
    `package test8;

import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class LinkedBinaryTree implements Iterable, BinaryTreeADT {
protected BinaryTreeNode root;
protected int modCount;
protected LinkedBinaryTree left,right;

public LinkedBinaryTree() {
    root = null;
}

public LinkedBinaryTree(T element) {
    root = new BinaryTreeNode<T>(element);
}

public LinkedBinaryTree(T element, LinkedBinaryTree<T> left,
                        LinkedBinaryTree<T> right) {
    root = new BinaryTreeNode<T>(element);
    root.setLeft(left.root);
    root.setRight(right.root);
    this.left = left;

    this.right=right;
}

public BinaryTreeNode<T> getRootNode() throws EmptyCollectionException {
    if (isEmpty()) {
        throw new EmptyCollectionException("BinaryTreeNode ");
    }
    return root;
}

public LinkedBinaryTree<T> getLeft() {
    return left;
}

public LinkedBinaryTree<T> getRight() {
    return right;
}


public int getHeight()
{
    return height(root);
}


private int height(BinaryTreeNode<T> node)
{
    if(node==null){
        return 0;
    }
    else {
        int leftTreeHeight = height(node.getLeft());
        int rightTreeHeight= height(node.getRight());
        return leftTreeHeight>rightTreeHeight ? (leftTreeHeight+1):(rightTreeHeight+1);
    }
}
@Override
public T getRootElement() throws EmptyCollectionException {
    if (root.getElement().equals(null)) {
        throw new EmptyCollectionException("BinaryTreeNode ");
    }
    return root.getElement();
}

@Override
public boolean isEmpty() {
    return (root == null);
}

@Override
public int size() {

    int size = 0;
    if(root.getLeft()!=null){
        size+=1;
    }
    if(root.getRight()!=null){
        size+=1;
    }

    return size;
}


public String removeRightSubtree() throws EmptyCollectionException {
    if (root == null)
        throw new EmptyCollectionException("tree is empty");
    BinaryTreeNode cur = root;
    while (cur.getLeft() != null){
        cur.setRight(null);
        cur = cur.left;
    }
    return super.toString();
}

public void removeAllElements() throws EmptyCollectionException {
    if (root == null)
        throw new EmptyCollectionException("tree is empty");
    root = null;
}

@Override
public boolean contains(T targetElement) {
    if(targetElement == find(targetElement))
        return true;
    else
        return false;
}

@Override

public T find(T targetElement) {

    BinaryTreeNode<T> current = findAgain(targetElement, root);

    if (current == null)
        try {
            throw new ElementNotFoundException("LinkedBinaryTree");
        } catch (ElementNotFoundException e) {
            e.printStackTrace();
        }

    return (current.getElement());
}

private BinaryTreeNode<T> findAgain(T targetElement, BinaryTreeNode<T> next) {
    if (next == null)
        return null;

    if (next.getElement().equals(targetElement))
        return next;

    BinaryTreeNode<T> temp = findAgain(targetElement, next.getLeft());

    if (temp == null)
        temp = findAgain(targetElement, next.getRight());

    return temp;
}



@Override
public Iterator<T> iteratorInOrder() {
    ArrayListUnordered<T> tempList = new ArrayListUnordered<T>();
    inOrder(root, tempList);
    return new TreeIterator(tempList.iterator());
}
public void toPreString(){
    preOrder(root);
}
private void preOrder(BinaryTreeNode root){
    if(null!= root){
        System.out.print(root.getElement() + "\t");
        preOrder(root.getLeft());
        preOrder(root.getRight());
    }
}

public void toPostString(){
    postOrder(root);
}
private void postOrder(BinaryTreeNode root) {
    if (null != root) {
        postOrder(root.getLeft());
        postOrder(root.getRight());
        System.out.print(root.getElement() + "\t");
    }
}


protected void inOrder(BinaryTreeNode<T> node, ArrayListUnordered<T> tempList) {
    if (node != null) {
        inOrder(node.getLeft(), tempList);
        tempList.addToRear(node.getElement());
        inOrder(node.getRight(), tempList);
    }
}


@Override
public Iterator<T> iteratorPreOrder() {
    ArrayListUnordered<T> tempList = new ArrayListUnordered<T>();
    preOrder(root, tempList);
    return new TreeIterator(tempList.iterator());
}
private void preOrder(BinaryTreeNode<T> node, ArrayListUnordered<T> tempList) {
    if (node != null) {
        tempList.addToRear(node.getElement());
        inOrder(node.getLeft(), tempList);
        inOrder(node.getRight(), tempList);
    }
}

@Override
public Iterator<T> iteratorPostOrder() {
    ArrayListUnordered<T> tempList = new ArrayListUnordered<T>();
    postOrder(root, tempList);
    return new TreeIterator(tempList.iterator());
}
private void postOrder(BinaryTreeNode<T> node, ArrayListUnordered<T> tempList) {
    if (node != null) {
        tempList.addToRear(node.getElement());
        inOrder(node.getLeft(), tempList);
        inOrder(node.getRight(), tempList);
    }
}

@Override
public Iterator<T> iteratorLevelOrder() {
    ArrayListUnordered<BinaryTreeNode<T>> nodes = new ArrayListUnordered<BinaryTreeNode<T>>();
    ArrayListUnordered<T> tempList = new ArrayListUnordered<T>();
    BinaryTreeNode<T> current;

    nodes.addToRear(root);

    while (!nodes.isEmpty()) {
        current = nodes.removeFirst();

        if (current != null) {
            tempList.addToRear(current.getElement());
            System.out.println(current.element);
            if (current.getLeft() != null)
                nodes.addToRear(current.getLeft());
            System.out.println(current.left);
            if (current.getRight() != null)
                nodes.addToRear(current.getRight());
            System.out.println(current.right);
        } else
            tempList.addToRear(null);
    }

    return new TreeIterator(tempList.iterator());
}

public void toLevelString1(){
    if(root == null)
        return;
    int height = getHeight();
    for(int i = 1; i <= height; i++){
        levelOrder(root,i);
    }
}
private void levelOrder(BinaryTreeNode root,int level){
    if(root == null || level < 1){
        return;
    }
    if(level == 1){
        System.out.print(root.getElement() + "\n");
        return;
    }
    levelOrder(root.getLeft(),level - 1);
    levelOrder(root.getRight(),level - 1);
}

@Override
public Iterator<T> iterator() {
    return iteratorInOrder();
}

public String toString() {
    UnorderedListADT<BinaryTreeNode<T>> nodes = new ArrayListUnordered<BinaryTreeNode<T>>();
    UnorderedListADT<Integer> levelList = new ArrayListUnordered<Integer>();

    BinaryTreeNode<T> current;
    String result = "";
    int Depth = this.getHeight();
    int possibleNodes = (int) Math.pow(2, Depth + 1);
    int countNodes = 0;

    nodes.addToRear(root);
    Integer curLevel = 0;
    Integer preLevel = -1;
    levelList.addToRear(curLevel);

    while (countNodes < possibleNodes) {
        countNodes = countNodes + 1;
        current = nodes.removeFirst();
        curLevel = levelList.removeFirst();
        if (curLevel > preLevel) {
            result = result+ "\n\n";
            preLevel = curLevel;
            for (int j = 0; j < ((Math.pow(2, (Depth - curLevel))) - 1); j++)
                result = result + " ";
        } else {
            for (int i = 0; i < (Math.pow(2, (Depth - curLevel + 1)) - 1); i++) {
                result = result + " ";
            }
        }
        if (current != null) {
            result = result + (current.getElement()).toString();
            nodes.addToRear(current.getLeft());
            levelList.addToRear(curLevel + 1);
            nodes.addToRear(current.getRight());
            levelList.addToRear(curLevel + 1);
        } else {
            nodes.addToRear(null);
            levelList.addToRear(curLevel + 1);
            result = result + " ";
        }
    }
    return result;
}
public void setRight(LinkedBinaryTree<T> right) {
    this.right = right;
}

private class TreeIterator implements Iterator<T> {
    private int expectedModCount;
    private Iterator<T> iter;

    public TreeIterator(Iterator<T> iter) {
        this.iter = iter;
        expectedModCount = modCount;
    }


    public boolean hasNext() throws ConcurrentModificationException {
        if (!(modCount == expectedModCount))
            throw new ConcurrentModificationException();

        return (iter.hasNext());
    }


    public T next() throws NoSuchElementException {
        if (hasNext())
            return (iter.next());
        else
            throw new NoSuchElementException();
    }


    public void remove() {
        throw new UnsupportedOperationException();
    }
}

} package test8;

import java.util.Iterator;

public interface BinaryTreeADT {

public T getRootElement() throws EmptyCollectionException ;

public boolean isEmpty();


public int size();


public boolean contains(T targetElement);


public T find(T targetElement);


public String toString();

public Iterator<T> iterator();


public Iterator<T> iteratorInOrder();


public Iterator<T> iteratorPreOrder();


public Iterator<T> iteratorPostOrder();


public Iterator<T> iteratorLevelOrder();

}
package test8;

import java.util.Iterator;

public class LinkedBinaryTreeTest {
public static void main(String[] args) {
LinkedBinaryTree num1 = new LinkedBinaryTree("2");
LinkedBinaryTree num2 = new LinkedBinaryTree("0");
LinkedBinaryTree num3 = new LinkedBinaryTree("1");
LinkedBinaryTree num4 = new LinkedBinaryTree("9",num1,num3);
LinkedBinaryTree num5 = new LinkedBinaryTree("0",num2,num4);
LinkedBinaryTree num6 = new LinkedBinaryTree("2",num4,num5);

    Iterator it;
    System.out.println("right of 8: ");
    System.out.println(num4.getRight());
    System.out.println("Contains 2? ");
    System.out.println(num1.contains("2"));

    System.out.println("PreOrder:  ");
    num6 .toPreString() ;

    System.out.println();

    System.out.println("PostOrder: ");
    num6 .toPostString() ;


    System.out.println(num6.toString());

}

} package test8;

public class EmptyCollectionException extends RuntimeException
{

public EmptyCollectionException (String collection)
{
    super ("The " + collection + " is empty.");
}

}
package test8;

public interface UnorderedListADT extends ListADT
{

public void addToFront(T element);

public void addToRear(T element);

public void addAfter(T element, T target);

} package test8;

public class ElementNotFoundException extends RuntimeException
{

public ElementNotFoundException (String collection)
{
    super ("The target element is not in this " + collection);
}

} package test8;

import java.util.Iterator;

public interface ListADT extends Iterable
{

public T removeFirst();


public T removeLast();

public T remove(T element);


public T first();


public T last();


public boolean contains(T target);

public boolean isEmpty();


public int size();


public Iterator<T> iterator();


public String toString();

}
package test8;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
public class ArrayIterator extends ArrayList implements Iterator {
int iteratorModCount;
int current;
public ArrayIterator()
{
iteratorModCount = modCount;
current = 0;
}
public boolean hasNext() throws ConcurrentModificationException
{
return super.iterator().hasNext();
}
public T next() throws ConcurrentModificationException
{
return super.iterator().next();
}

public void remove() throws UnsupportedOperationException
{
    throw new UnsupportedOperationException();
}

}
package test8;

public class ArrayListUnordered extends ArrayList implements UnorderedListADT {
@Override
public void addToFront(T element) {
if (size() == list.length)
expandCapacity();
for (int i=rear;i > 0; i--)
list[i] = list[i - 1];
list[0] = element;
rear++;
modCount++;
}

@Override
public void addToRear(T element) {
    if (size() == list.length)
        expandCapacity();
    list[rear] = element;
    rear++;
    modCount++;
}

@Override
public void addAfter(T element, T target) {
    if (size() == list.length)
        expandCapacity();

    int scan = 0;

    //find the insertion point
    while (scan < rear && !target.equals(list[scan]))
        scan++;
    if (scan == rear)
        try {
            throw new ElementNotFoundException("UnorderedList");
        } catch (ElementNotFoundException e) {
            e.printStackTrace();
        }

    scan++;

    for (int shilt = rear; shilt > scan; shilt--)
        list[shilt] = list[shilt - 1];

    list[scan] = element;
    rear++;
    modCount++;
}

} package test8;

import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public abstract class ArrayList implements ListADT, Iterable
{
private final static int DEFAULT_CAPACITY = 100;
private final static int NOT_FOUND = -1;

protected int rear;
protected T[] list;
protected int modCount;

public ArrayList()
{
    this(DEFAULT_CAPACITY);
}

public ArrayList(int initialCapacity)
{
    rear = 0;
    list = (T[])(new Object[initialCapacity]);
    modCount = 0;
}

protected void expandCapacity(){
    list = Arrays.copyOf(list,list.length*2);
}

@Override
public T removeLast() throws EmptyCollectionException {
    T result = list[rear-1];
    list[rear]=null;
    rear --;
    return result;
}

@Override
public T removeFirst() throws EmptyCollectionException {
    T result =list[0];
    rear--;
    for(int i = 0; i< rear; i++){
        list[i] = list[i + 1];
    }
    list[rear] = null;
    return result;
}

@Override
public T remove(T element)
{
    T result;
    int index = find(element);

    if (index == NOT_FOUND)
        try {
            throw new ElementNotFoundException("ArrayList");
        } catch (ElementNotFoundException e) {
            e.printStackTrace();
        }

    result = list[index];
    rear--;

    for (int scan=index; scan < rear; scan++)
        list[scan] = list[scan+1];

    list[rear] = null;
    modCount++;

    return result;
}

@Override
public T first() throws EmptyCollectionException
{
    T result = list[0];
    return result;
}

@Override
public T last() throws EmptyCollectionException
{
    T result = list[rear-1];
    return result;
}

@Override
public int size(){
    return rear;

}

@Override
public boolean contains(T target)
{
    return (find(target) != NOT_FOUND);
}

private int find(T target)
{
    int scan = 0;
    int result = NOT_FOUND;

    if (!isEmpty()) {
        while (result == NOT_FOUND && scan < rear)
            if (target.equals(list[scan]))
                result = scan;
            else
                scan++;
    }

    return result;
}

@Override
public boolean isEmpty(){
    if(size() == 0){
        return true;
    }else
        return false;
}

@Override
public String toString(){
    String string = "";
    for (int i = 0;i < rear;i++){
        string += list[i] + " ";
    }
    return string;
}

@Override
public Iterator<T> iterator(){
    return new ArrayListIterator();
}

private class ArrayListIterator implements Iterator<T>
{
    int iteratorModCount;
    int current;


    public ArrayListIterator()
    {
        iteratorModCount = modCount;
        current = 0;
    }

    @Override
    public boolean hasNext() throws ConcurrentModificationException
    {
        if (iteratorModCount != modCount)
            throw new ConcurrentModificationException();

        return (current < rear);
    }

    @Override
    public T next() throws ConcurrentModificationException
    {
        if (!hasNext())
            throw new NoSuchElementException();

        current++;

        return list[current - 1];
    }

    @Override
    public void remove() throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException();
    }

}

}
package test8;

public class BinaryTreeNode {

protected T element;
protected BinaryTreeNode<T> left;
protected BinaryTreeNode<T> right;


public BinaryTreeNode(T obj) {
    this.element = obj;
    this.left = null;
    this.right = null;
}



public BinaryTreeNode(T obj, LinkedBinaryTree<T> left,
                      LinkedBinaryTree<T> right)throws EmptyCollectionException {
    element = obj;
    if (left == null)
        this.left = null;
    else
        this.left = left.getRootNode();

    if (right == null)
        this.right = null;
    else
        this.right = right.getRootNode();
}


public int numChildren() {
    int children = 0;

    if (left != null)
        children = 1 + left.numChildren();

    if (right != null)
        children = children + 1 + right.numChildren();

    return children;
}

public T getElement() {
    return element;
}

public BinaryTreeNode<T> getRight() {
    return right;
}

public void setRight(BinaryTreeNode<T> node) {
    right = node;
}

public BinaryTreeNode<T> getLeft() {
    return left;
}

public void setLeft(BinaryTreeNode<T> node) {
    left = node;
}

public boolean judge(){
    if(right == null && left == null)

        return true;
    else
        return false;
}

}
`

  • (2)基於LinkedBinaryTree,實現基於(中序,先序)序列構造唯一一棵二㕚樹的功能,比如給出中序HDIBEMJNAFCKGL和先序ABDHIEJMNCFGKL,構造出附圖中的樹
    用JUnit或自己編寫驅動類對自己實現的功能進行測試,提交測試程式碼執行截圖,要全屏,包含自己的學號資訊
    `package test8;

import java.util.Scanner;
import java.util.StringTokenizer;

public class CreateTree {
public LinkedBinaryTree CreatTree(String inorder[],String preorder[]){
LinkedBinaryTree binaryTree = null;
if(inorder.length == preorder.length && inorder.length != 0 && preorder.length != 0){
int n = 0;

        while (!inorder[n].equals(preorder[0])) {
            n++;
        }

        String[] preLeft = new String[n];
        String[] inLeft = new String[n];

        String[] preRight = new String[preorder.length - n - 1];
        String[] inRight = new String[inorder.length - n - 1];
        for (int t = 0; t < inorder.length; t++) {
            if (t < n) {
                preLeft[t] = preorder[t + 1];//左子樹生成
                inLeft[t] = inorder[t];//左子樹生成
            }
            if (t > n) {
                preRight[t - n - 1] = preorder[t];//右子樹生成
                inRight[t - n - 1] = inorder[t];//右子樹生成
            }
            if(t == n){//
                continue;
            }
        }

        LinkedBinaryTree<String> left = CreatTree(inLeft, preLeft);
        LinkedBinaryTree<String> right = CreatTree(inRight, preRight);
        binaryTree = new LinkedBinaryTree<String>(preorder[0], left, right);

    }else


    {
        binaryTree = new LinkedBinaryTree<>();
    }
    return binaryTree;
}

public static void main(String[] args)
{
    String a,b;
    int i = 0,j = 0;
    Scanner scanner  = new Scanner(System.in);
    System.out.println("Input the PreOrder:");
    a = scanner.nextLine();
    System.out.println("Input the InOrder:");
    b = scanner.nextLine();

    StringTokenizer str1 = new StringTokenizer(a, " ");
    StringTokenizer  str2= new StringTokenizer(b, " ");

    String[] string1 = new String[str1.countTokens()];
    String[] string2 = new String[str2.countTokens()];

    while (str1.hasMoreTokens())
    {
        string1[i] = str1.nextToken();
        i++;
    }


    while (str2.hasMoreTokens())
    {
        string2[j] = str2.nextToken();
        j++;
    }


    CreateTree ct = new CreateTree();
    LinkedBinaryTree<String> binaryTree = ct.CreatTree(string2,string1);
    System.out.println("The Tree is:");
    System.out.println();
    System.out.println(binaryTree.toString());
}

}`

  • (3)自己設計並實現一顆決策樹
    提交測試程式碼執行截圖,要全屏,包含自己的學號資訊
    課下把程式碼推送到程式碼託管平臺
    `package test8;

import java.util.Scanner;

public class PsychologicalTest
{
private LinkedBinaryTree tree;

public PsychologicalTest()
{
    String e1 = "你玩CSGO嗎 ?";
    String e2 = "你是突破手嗎 ?";
    String e3 = "你是狙擊手嗎 ?";
    String e4 = "你是自由人嗎?";
    String e5 = "你喜歡開箱了嗎 ?";
    String e6 = "你喜歡打競技嗎 ?";
    String e7 = "你的段位達到了大地球嗎 ?";
    String e8 = "正常玩家";
    String e9 = "老六";
    String e10 = "無氪玩家";
    String e11 = "土豪";
    String e12 = "祝你早日登上大地球";
    String e13 = "休閒娛樂玩家";
    String e14 = "菜雞";
    String e15 = "pro";


    LinkedBinaryTree<String> n2, n3, n4, n5, n6, n7, n8, n9,
            n10, n11, n12, n13,n14,n15;

    n8 = new LinkedBinaryTree<String>(e8);
    n9 = new LinkedBinaryTree<String>(e9);
    n4 = new LinkedBinaryTree<String>(e4, n8, n9);

    n10 = new LinkedBinaryTree<String>(e10);
    n11 = new LinkedBinaryTree<String>(e11);
    n5 = new LinkedBinaryTree<String>(e5, n10, n11);

    n12 = new LinkedBinaryTree<String>(e12);
    n13 = new LinkedBinaryTree<String>(e13);
    n6 = new LinkedBinaryTree<String>(e6, n12, n13);

    n14 = new LinkedBinaryTree<String>(e14);
    n15 = new LinkedBinaryTree<String>(e15);
    n7 = new LinkedBinaryTree<String>(e7,n14,n15);

    n2 = new LinkedBinaryTree<String>(e2, n4, n5);
    n3 = new LinkedBinaryTree<String>(e3, n6, n7);

    tree = new LinkedBinaryTree<String>(e1, n2, n3);
}

public void start()
{
    Scanner scan = new Scanner(System.in);
    LinkedBinaryTree<String> current = tree;

    System.out.println ("讓我們聊一聊");
    while (current.size() > 1)
    {
        System.out.println (current.getRootElement());
        if (scan.nextLine().equalsIgnoreCase("N"))
            current = current.getLeft();
        else
            current = current.getRight();
    }

    System.out.println (current.getRootElement());
}

public static void main(String[] args){
    PsychologicalTest test = new PsychologicalTest();
    test.start();
}

}`

  • (4)輸入中綴表示式,使用樹將中綴表示式轉換為字尾表示式,並輸出字尾表示式和計算結果(如果沒有用樹,正常評分。如果用到了樹,即使有小的問題,也酌情給滿分)
    提交測試程式碼執行截圖,要全屏,包含自己的學號資訊
    `package test8;

import java.util.Stack;

public class Fix {
static Stack op = new Stack<>();

public static Float getv(char op, Float f1, Float f2) {
    if (op == '+') return f2 + f1;
    else if (op == '-') return f2 - f1;
    else if (op == '*') return f2 * f1;
    else if (op == '/') return f2 / f1;
    else return Float.valueOf(-0);
}

public static float calrp(String rp) {
    Stack<Float> v = new Stack<>();
    char[] arr = rp.toCharArray();
    int len = arr.length;
    for (int i = 0; i < len; i++) {
        Character ch = arr[i];
        if (ch >= '0' && ch <= '9') v.push(Float.valueOf(ch - '0'));
        else v.push(getv(ch, v.pop(), v.pop()));
    }
    return v.pop();
}

public static String getrp(String s) {
    char[] arr = s.toCharArray();
    int len = arr.length;
    String out = "";

    for (int i = 0; i < len; i++) {
        char ch = arr[i];
        if (ch == ' ') continue;
        if (ch >= '0' && ch <= '9') {
            out += ch;
            continue;
        }

        if (ch == '(')
            op.push(ch);

        if (ch == '+' || ch == '-') {
            while (!op.empty() && (op.peek() != '('))
                out += op.pop();
            op.push(ch);
            continue;
        }

        if (ch == '*' || ch == '/') {
            while (!op.empty() && (op.peek() == '*' || op.peek() == '/'))
                out += op.pop();
            op.push(ch);
            continue;
        }

        if (ch == ')') {
            while (!op.empty() && op.peek() != '(')
                out += op.pop();
            op.pop();
            continue;
        }
    }
    while (!op.empty()) out += op.pop();
    return out;
}

} package test8;

import java.util.Scanner;

public class FixTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("請輸入表示式:");
String s = scan.nextLine();
Fix fix = new Fix();
System.out.println("字尾表示式為:\n"+Fix.getrp(s));
System.out.println("計算結果為:\n"+fix.calrp(Fix.getrp(s)));
}
}
`

3. 實驗過程中遇到的問題和解決過程

  • 問題1:介面無法呼叫
  • 問題1解決方案:需要放到一個包裡

其他(感悟、思考等)

本次實驗對於樹的具體構造還是不太清楚,以後需要多複習。

參考資料