⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 re_to_et.java

📁 Regular Expression to Expression Tree
💻 JAVA
字号:
import java.util.*;
import java.io.*;
public class RE_to_ET {
		static final int NUMBER = 0, OPERATOR = 1, CONJ = 2;	// values for kind.
    	private String infixExp;								// input infix expression
		private String postfixExp = ""; 						// output postfix expression
        Stack<String> stringStack; 								// create stack for inpix to postfix
		private Stack<Node> chkStack;
		private String output = "";
		String scan = "";
		int x = 400;
		int y = 200;
		int size = 0;
		double chk = 0;
		char chk2 = ' ';
		static int lx = 2, s = 0;

//------------------Create Stack Postfix----------------------------------------------------
	public void Postfix(String in){
            infixExp = in;
            stringStack = new Stack<String>();
	}

//------------------Input to Postfix---------------------------------------------------------
	public String In2PostExp()throws IOException   {
		for (int j = 0; j < infixExp.length(); j++) {
                    char ch = infixExp.charAt(j);
                    String chx = String.valueOf(ch);
                    switch (ch) {
			case '|': gotOper(chx, 1);          break;
			case '.': gotOper(chx, 2);          break;
			case '(': stringStack.push(chx);    break;
			case ')': gotParen();               break;
			default:
                            postfixExp = postfixExp + " " + chx;
                            break;
			}
		}

		while (!stringStack.isEmpty()) {
			String chx = stringStack.pop();
			postfixExp = postfixExp +" "+ chx;
		}
		FileWriter svg = new FileWriter("ET.svg",true);
	    BufferedWriter out = new BufferedWriter(svg);
		out.write("<?xml version=\"1.0\" standalone=\"no\"?>\n");
		out.write("<svg viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n");
		out.close();

		Post_to_ET(postfixExp);

		FileWriter svg3 = new FileWriter("ET.svg",true);
		BufferedWriter out3 = new BufferedWriter(svg3);
		out3.write("</svg>");
		out3.close();

		return postfixExp;

	}

//------------------Got Operator---------------------------------------------------------
	public void gotOper(String opThis, int prec1) {
		while (!stringStack.isEmpty()) {
			String chx = stringStack.pop();
			char opTop = chx.charAt(0);
			if (opTop == '(') {
                            stringStack.push(chx); break;
			} else {
                            int prec2;
                            if (opTop == '|') { prec2 = 1;
                            } else { prec2 = 2; }
                            if (prec2 < prec1){
                                    stringStack.push(chx); break;
                            }else if (prec2 == prec1){
                                    stringStack.push(chx); break;
                            } else { postfixExp = postfixExp + " " + chx; }
			}
		}
		stringStack.push(opThis);
	}

//------------------gotParen--------------------------------------------------------------
	public void gotParen(){
		while (!stringStack.isEmpty()) {
			String chx = stringStack.pop();
			char ch = chx.charAt(0);
			if (ch == '(') {break;
			} else { postfixExp = postfixExp + " " + chx; }
		}
	}

//------------------Infix input---------------------------------------------------------
    public static void main(String[] args)throws IOException  {
        //String input = "(0+1?)";
		//String input = "(0+1?|01)";
        String input = "(0|1)+((10|01)?1)*00";
        //String input = "(000|110+)10";
        //String input = "(0|1)*101(0|1)*";
        //String input = "0(01|10)+0";
		//String input = "1100?1";

//----------------Create-New-input--------------------------------------------------------
        String Ninput = "";
        for (int i =0; i < input.length(); i++ ){
            char ch1 = input.charAt(i);
            char ch2; String ST;
            if((i+1) < input.length()){ ch2 = input.charAt(i+1); }else{ ch2='x'; }
            switch (ch1) {
		case '0':case '1':
                    ST = String.valueOf(ch1);  Ninput = Ninput+ST;
                        switch (ch2) {
                            case '0':case '1':case '(': Ninput = Ninput+"."; break;
                            default: break;
                        }
                    break;
                case '*':case '+':case '?':
                    ST = String.valueOf(ch1);  Ninput = Ninput+ST;
                        switch (ch2) {
                            case 'x': case '|': case ')': break;
                            default: Ninput = Ninput+"."; break;
                        }
                    break;
                case '(':case '|':
                    String ST2 = String.valueOf(ch1);  Ninput = Ninput+ST2; break;
                case ')':
                    ST = String.valueOf(ch1);  Ninput = Ninput+ST;
                        switch (ch2) {
                            case '*':case '+':case '?': break;
                            case '0':case '1':case '(': Ninput = Ninput+"."; break;
                            default: break;
                        }
                    break;
		default: break;
            }
        }
        System.out.println("Input:"+input);
        System.out.println("New-Input:"+Ninput);

//----------------Create-Postfix--------------------------------------------------------
        RE_to_ET p = new RE_to_ET();
        p.Postfix(Ninput);
        System.out.println("Postfix: " + p.In2PostExp() );
    }

  public void Post_to_ET(String in)throws IOException {
	  chkStack = new Stack<Node>();
	  for (int j = 0; j < in.length(); j++) {
		  char c = in.charAt(j);
		  char cn;
		  if ((j+1) < in.length()){
			  cn = in.charAt(j+1);
		  }else{
			  cn = 'y';
		  }
		  switch (c) {
			  case '|':
			  case '.':
						Node t1 = chkStack.pop();
						Node t2 = chkStack.pop();
						Node t3 = new Node(c, t1, t2);
						chkStack.push(t3);
						break;
			  case '+':
			  case '*':
			  case '?':
						Node t4 = chkStack.pop();
						Node t5 = new Node(c, t4);
						chkStack.push(t5);
						break;
			  case ' ':
						break;
			  default:									// must be an operand
						Node t6 = new Node(c);
						chkStack.push(t6);
						break;
		  }
	  }
	  Node t = chkStack.pop();
	  noderTest(t,x,y);
  }


	public static void noderTest(Node node,int xc ,int yc)throws IOException{
		Node t = node;
		FileWriter svg2 = new FileWriter("ET.svg",true);
		BufferedWriter out2 = new BufferedWriter(svg2);
		if ( t.kind == NUMBER) {
					out2.write("<circle cx=\""+xc+"\" cy=\""+yc+"\" r=\"30\" stroke='black' stroke-width='2'  fill='white' fill-opacity='.85' />\n");
					out2.write("<text class='Label' x=\""+(xc-2)+"\" y=\""+(yc+5)+"\">"+t.op+"</text>\n");
					out2.close();

		}
	    else if (t.kind == CONJ) {
					out2.write("<circle cx=\""+xc+"\" cy=\""+yc+"\" r=\"30\" stroke='black' stroke-width='2'  fill='white' fill-opacity='.85' />\n");
					out2.write("<text class='Label' x=\""+(xc-2)+"\" y=\""+(yc+5)+"\">"+t.con+"</text>\n");
					out2.write("<line x1 = \""+(xc)+"\" y1 = \""+(yc+30)+"\" x2 = \""+(xc)+"\" y2 = \""+(yc+70)+"\" stroke = \"black\" stroke-width = \"3\"/>\n");
					if (t.down != null){
						noderTest(t.down,(xc),(yc+100));
					}
		}
		else {
					out2.write("<circle cx=\""+xc+"\" cy=\""+yc+"\" r=\"30\" stroke='black' stroke-width='2'  fill='white' fill-opacity='.85' />\n");
					out2.write("<text class='Label' x=\""+(xc-2)+"\" y=\""+(yc+5)+"\">"+t.op+"</text>\n");
		}
		if (t.left != null) {
			lx++;
			if (s == 1){
				lx++;
			}
			s = 0;
			out2.write("<line x1 = \""+(xc-30)+"\" y1 = \""+(yc+10)+"\" x2 = \""+(xc-(1200/lx)+30 )+"\" y2 = \""+(yc+90)+"\" stroke = \"black\" stroke-width = \"3\"/>\n");
			noderTest(t.left,(xc-1200/lx),(yc +100));
		}
		if (node.right != null) {
			xc = xc +(1200/lx);
			yc += 100;
			out2.write("<line x1 = \""+(xc-20)+"\" y1 = \""+(yc-20)+"\" x2 = \""+(xc-(1200/lx)+30 )+"\" y2 = \""+(yc-90)+"\" stroke = \"black\" stroke-width = \"3\"/>\n");

			lx--;
			s = 1;
			noderTest(t.right,(xc),(yc));
		}
		out2.close();
	}


  class Stack<Item> implements Iterable<Item> {
    private int N;										// size of the stack
    private Node first;									// top of stack

// helper linked list class
    private class Node {
        private Item item;
        private Node next;
    }

// create an empty stack
    public Stack() {
        first = null;
        N = 0;
    }

// is the stack empty?
// number of elements on the stack
    public boolean isEmpty() { return first == null; }
    public int size()        { return N;             }


// return most recently added item
    public Item peek() {
        return first.item;
    }


// add an item to the stack
    public void push(Item item) {
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
        N++;
    }

// delete and return the most recently added item
    public Item pop() {
        if (isEmpty()) throw new RuntimeException("Stack underflow");
        Item item = first.item;        // save item to return
        first = first.next;            // delete first node
        N--;
        return item;                   // return the saved item
    }

    public Iterator<Item> iterator()  { return new StackIterator();  }

// an iterator, doesn't implement remove() since it's optional
    private class StackIterator implements Iterator<Item> {
        private Node current = first;
        public boolean hasNext()  { return current != null;                     }
        public void remove()      { throw new UnsupportedOperationException();  }

        public Item next() {
            if (!hasNext()) throw new NoSuchElementException();
            Item item = current.item;
            current = current.next;
            return item;
        }
    }
  }
  class Node {
    int kind;				// type of node
    char op;				// The operator in a node
	char con;
    Node left;				// left node
    Node right;				// rightleft node
	Node down;	

    Node(char val) {
       kind = NUMBER;
       this.op = val;
    }

	Node(char con, Node down) {
       kind = CONJ;
       this.con = con;
	   this.down = down;
    }

    Node(char op, Node right, Node left) {
       kind = OPERATOR;
       this.op = op;
       this.left = left;
       this.right = right;
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -