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

📄 main.java

📁 一个小的java程序
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package queue;import java.util.LinkedList;import java.util.Stack;/** * * @author px */public class Main {    //initialize the tested string    static String Ini = "B(einxgz)B";    /**     * @param args the command line arguments     */    public static void main(String[] args) {        // TODO code application logic here        queue tester = new queue();        String new_string;        //here we should initialize the in after stack implemention       String in = tester.Tran(Ini);               System.out.println(Ini);         tester.testFIFO(in);        System.out.println();        stack s=new stack();        if(s.ValidateBracket(Ini))          {   String tmp=in .substring(in.indexOf('(', 0), in.indexOf(')', 0)+1);       //replace the string within parenthesis   //new_string = in.replace(tmp, s.testLIFO( s.get_string(in) ) );  new_string= in.replace(tmp, s.testLIFO( s.get_string(in) ));     System.out.println( new_string);                                //            String str=s.testLIFO( s.get_string(Ini) );//            System.out.println();           dic  com=new dic();           com.compare(new_string);         }        else             System.out.print("brackets not matched!");                            }}class queue {        public LinkedList q = new LinkedList();    // constructor//define the property of queue as testFIFO    public void testFIFO(String in) {        String queueIm[] = in.split("");//change the string to array for off and poll implemention        for (int i = 0; i <= queueIm.length - 1; i++) {            q.offer(queueIm[i]);        }        Object o;        while ((o = q.poll()) != null) {            System.out.print(o);        }    }    String Tran(String Ini) {        //traversal the string           String transfer = Ini.replace("B", "tAdA");       // System.out.println(transfer);        // replace character sequence "A" with "sae"          //and output the string after replace        String Inqueue = transfer.replace("A", "sae");        //System.out.println(Instack);        return Inqueue;    }    }class stack { public Stack s=new Stack();     public LinkedList list = new LinkedList();      public String testLIFO(String in) {       //change the string to array for push and pop implemention        String stackIm[] =in.split("") ;        //here because use the in.spilit("")               for (int i = 1; i <= stackIm.length - 1; i++) {            s.push(stackIm[i]);//push into stack        }     // s.size();      Stack q=new Stack();      q.push(s.firstElement());            //while s is not empty pop the top and e       do     {              list.add(q.peek());         list.add(s.pop());    }   while(s.size()>1);       list.add(q.peek());       return list.toString();        }          //to find the matched brackets,if not matched return false   boolean ValidateBracket(String Ini) {        Stack<Character> stack = new Stack<Character>();        for (char c : Ini.toCharArray()) {            if (c == '(') {                //found left side of bracket, pushed it into stack                stack.push(c);                                              }                             else if (c == ')') {                if (stack.isEmpty()) {                    //lost left side of bracket, validating failed                    return false;                }                 else {                    //It's right side of bracket, pop up the left side one                     stack.pop();                }            }        }        if (!stack.isEmpty()) {            //lost right side of bracket, validating failed            return false;        }        return true;    }  //get the string within pranthesis   String get_string(String within_match ){     String str=within_match .substring(within_match.indexOf('(', 0)+1, within_match.indexOf(')', 0));      return str;  }}//translate the interpeter string as the given dictionaryclass dic{        public String dict[]={"t", "天",                   "d", "地",                   "s", "上",                   "a", "一个",                   "e", "鹅",                   "z", "追",                   "g", "赶",                   "x", "下",                   "n", "蛋",                   "i", "恨"               };   public void compare(String output){      String args[] =output.split("") ;         for(int i=0;i<args.length;i++){          for(int j=0;j<dict.length;j=j+2){              if(args[i].equals(dict[j]))                  System.out.print(dict[j+1]);              else                  continue;                     }                                         }                }    }      

⌨️ 快捷键说明

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