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

📄 cworddeal.java

📁 java写的c 语言子集编辑器
💻 JAVA
字号:
package CWord;import java.util.*;import javax.swing.*;import java.io.*;/** * @author jjl * @Date 2008.12.31 17.09~21:20 * * * the introduction for C WordDeal *  * main int while if for do else * 0    1   2     3  4   5   6 * the user definition -1 * the user number -2 * the >=   -6 * the <=   -5 * the ==   -4 * the !=   -3 * the ++ -7 * the -- -8 */public class CWordDeal extends Thread{    StringBuffer strBuf;// The C Simple Word    StringBuffer strDes;//the destination word here    StringBuffer strCur;//the current word    Vector vWord;//the usr word    int vwNum=0;    Vector vNum;//the user number    int vnNum=0;    Vector vCnst=new Vector();//the constrait    int vcNum=0;    listen l;    int preId;//The Pointer to Point the previous  char of the user input         Boolean RUNFLAGS=true;//wheather the thread is running    int status;// the current status    final static int Number=0;//the number ,user define    final static int UsrWord=1;//the  word , user define    final static int InrWord=2;// the cal or logic    final static int Begin=3;//the start for every word    final static int End=4;//the program end    final static int error=5;//the eror happen    static int Row=0;    public CWordDeal(){        status=error;        l=new listen();        strBuf=new StringBuffer();        strDes=new StringBuffer();        strCur=new StringBuffer();        vWord=new Vector();        vNum=new Vector();        this.start();    }    @Override    public void run(){        do{            while(true){               if(status==Begin){                   l.addMessage("Start Deal C Word");                   break;               }             }            dealCWord();            if(status==error){                l.addMessage("C Word exit with err");            }            if(status==End){                l.addMessage("C word Anaysics End!");                System.out.println(strDes);            }        }while(RUNFLAGS);    }    public void dsyCWordDeal(){        RUNFLAGS=false;    }    public StringBuffer getStrDes(){        startDealCWord();        StringBuffer  stb=new StringBuffer();        stb=strDes;        strDes.delete(0, strDes.length());        preId=0;//        this.TestNumberTableAndUserTable();        return stb;    }    public Vector getvUsr(){        return vWord;    }    public Vector getvNum(){        return vNum;    }    protected void startDealCWord(){        status=Begin;    }    public void inputCWord(StringBuffer strbuf){        if(strBuf.length()>0)        strBuf.delete(0, strBuf.length()-1);        strBuf.append(strbuf);        l.addMessage("the Input Word Of Test"+strBuf);           }    //the mian body to deal the cWord    protected synchronized void dealCWord(){        while(true){            char c=getNextWord();            if(c=='?'){                try{                    this.wait();                     l.addMessage("Now C Word Deal Waiting");                }catch(Exception e){                    e.printStackTrace();                    l.addMessage("Now C Word Deal Wait Error");                }                }            if(c==' '){              //  l.addMessage("deal the space sign"+c  );                if(status==Begin){// if start                    status=Begin;                    continue;                }                if(status==End){//if end                    status=End;                    preId=0;                    break;                }                if(status==Number){                    dealNumber();                    status=Begin;                    continue;                }                if(status==UsrWord){                    dealUsrWord();                    status=Begin;                    continue;                }                if(status==InrWord){                    status=Begin;                    dealInrWord();                    continue;                }            }            if(c=='#'){              //  l.addMessage("deal the space sign"+c  );                Row++;                strDes.append('#');                if(status==Begin){// if start                    status=Begin;                    continue;                }                if(status==End){//if end                    status=End;                    preId=0;                    break;                }                if(status==Number){                    dealNumber();                    status=Begin;                    continue;                }                if(status==UsrWord){                    dealUsrWord();                    status=Begin;                    continue;                }                if(status==InrWord){                    status=Begin;                    dealInrWord();                    continue;                }            }            if(checkIfNum(c)){                if(status==Begin){// if start                    strCur.append(c);                    status=Number;                    continue;                }                if(status==End){//if end                    status=error;                    break;                }                if(status==Number){                    strCur.append(c);                    status=Number;                    continue;                }                if(status==UsrWord){                    strCur.append(c);                    status=UsrWord;                    continue;                }                if(status==InrWord){                    dealInrWord();                    strCur.append(c);                    status=Number;                    continue;                }            }            if(checkIfChar(c)){                if(status==Begin){// if start                    status=UsrWord;                    strCur.append(c);                    continue;                }                if(status==End){//if end                    status=error;                    break;                }                if(status==Number){                    status=error;                    break;                }                if(status==UsrWord){                    status=UsrWord;                    strCur.append(c);                    continue;                }                if(status==InrWord){                    dealInrWord();                    strCur.append(c);                    status=UsrWord;                    continue;                }            }            if(checkIfCal(c)){                if(status==Begin){// if start                    status=InrWord;                    strCur.append(c);                    continue;                }                if(status==End){//if end                    status=error;                    break;                }                if(status==Number){                    dealNumber();                    strCur.append(c);                    status=InrWord;                    continue;                }                if(status==UsrWord){                    dealUsrWord();                    status=InrWord;                    strCur.append(c);                    continue;                }                if(status==InrWord){                    status=Begin;                    strCur.append(c);                    dealInrWord();                    continue;                }            }            if(checkIfSplt(c)){                if(status==Begin){// if start                    status=Begin;                    strCur.append(c);                    dealSplit();                    continue;                }                if(status==End){//if end                    status=End;                    strCur.append(c);                    dealSplit();                    preId=0;                    break;                }                if(status==Number){                    status=Begin;                    dealNumber();                    strCur.append(c);                    dealSplit();                    continue;                }                if(status==UsrWord){                    status=Begin;                    dealUsrWord();                    strCur.append(c);                    dealSplit();                    continue;                }                if(status==InrWord){                    status=error;                    break;                }            }          //  l.addMessage("a error word!+at Row"+Row);          //  break;        }    }    protected char getNextWord(){//ingore the differ of  the space and enter         // deal the space or enter sign as the one space        if(strBuf==null){            l.addMessage("no file of user input !");            return '?';//when error return ?         }        if(preId==strBuf.length()){            l.addMessage("The error happened!,Please Check The Error "+preId);            return '?';//when the space or enter to end the file of user design        }        if(preId==strBuf.length()-1){            status=End;        }       // l.addMessage("The Previous Char is "+strBuf.charAt(preId));        if(strBuf.charAt(preId)=='\n'){            preId++;            return '#';        }         if(strBuf.charAt(preId)=='\t'){            preId++;            return ' ';        }        char c=strBuf.charAt(preId);        preId++;        return c;    }    protected Boolean checkIfNum(char c){        char[] num=new char[]{'0','1','2','3','4','5','6','7','8','9'};        Boolean flag=false;        for(int i=0;i<num.length;i++){            if(c==num[i]){                flag=true;                break;            }        }        return flag;    }    protected Boolean checkIfChar(char c){        char[] wrd=                new char[]{'a','b','c','d','e','f','g','h',    'i','g','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};        Boolean flag=false;        for(int i=0;i<wrd.length;i++){            if(c==wrd[i]){                flag=true;                break;            }        }        return flag;    }    protected Boolean checkIfCal(char c){        char[] cal=new char[]{          '+','-','*','/','>','<','=','!'          };         Boolean flag=false;        for(int i=0;i<cal.length;i++){            if(c==cal[i]){                flag=true;                break;            }        }        return flag;    }    protected Boolean checkIfSplt(char c){        char[] splt=new char []{            ',',';','\n','(',')','{','}'        };        Boolean flag=false;        for(int i=0;i<splt.length;i++){            if(c==splt[i]){                flag=true;                break;            }        }        return flag;    }    protected void dealUsrWord(){      //  l.addMessage("deal the UserWord "+strCur);        String conStr[]=new String[]{            "main","int","while","if","for","do","else"        };        Boolean flag=false;      //  l.addMessage("Now Check If The Constraint! ");        for(int i=0;i<conStr.length;i++){             if(strCur.toString().equals(conStr[i])){//if the constraint                flag=true;          //      l.addMessage("get a constraint "+strCur);                strDes.append(i);                vCnst.addElement(conStr[i]);                vcNum++;                strCur.delete(0,strCur.length());                break;            }        }        if(!flag){// if user definition            l.addMessage("get a usr definition "+strCur);            vWord.addElement(strCur.toString());            strDes.append(-1);//the user defination            strCur.delete(0,strBuf.length());            vwNum++;        }    }    protected void dealNumber(){      //  l.addMessage("deal the Number "+strCur);        int iCur;        strDes.append(-2);        iCur=Integer.parseInt(strCur.toString());      //  l.addMessage("get A number "+iCur);        vNum.addElement(iCur);        vnNum++;        strCur.delete(0,strCur.length());    }    protected void dealInrWord(){   //     l.addMessage("deal the InrWord "+strCur);        String[] inr=new String[]{         "++" ,"--" ,">=","<=","==","!="        };        if(strCur.length()==1){            strDes.append(strCur);     //       l.addMessage("get a one inr word "+strCur);            strCur.delete(0,strCur.length());            return;        }        else{            for(int i=0;i<inr.length;i++){                if(strCur.toString().equals(inr[i]))                {                    l.addMessage("get a two inr word "+strCur);                    strDes.append(i-8);                    strCur.delete(0,strCur.length());                    return;                }            }            strCur.delete(0,strCur.length());            status=error;        }     }    protected void dealSplit(){     //   l.addMessage("deal the split "+strCur);     //   l.addMessage("Get The Split"+strCur);        strDes.append(strCur);        strCur.delete(0,strCur.length());    }    private void TestNumberTableAndUserTable(){        l.addMessage("Test   The Tables");        for(int i=0;i<vWord.size();i++){            System.out.println(vWord.get(i).toString());        }        for(int i=0;i<vNum.size();i++){            System.out.println(vNum.get(i));        }        for(int i=0;i<vCnst.size();i++){            System.out.println(vCnst.get(i).toString());        }    }}

⌨️ 快捷键说明

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