?? wordana.java
字號:
import java.io.*;
import java.util.*;
class wordAna1{
String temp=""; //用來存入臨時字符串
public wordAna1(){
/**通過BufferedReader函數(shù)從外部讀入txt文件,運(yùn)行完后輸出vector*/
File file=new File("d:\\text.txt");
Vector vector1 = new Vector();
try {BufferedReader input=new BufferedReader(new FileReader(file));
String text;
while((text=input.readLine())!=null)
vector1.add(text);
} catch(Exception ex){}
/**因?yàn)橐氲膙ector前后分別有一個”[]”,通過substring函數(shù)截取從第二個到倒數(shù)第二個的字符存入t
emp*/
temp=vector1.toString();
String tempStr=temp.substring(1,temp.length()-1);
char []methodCha=tempStr.toCharArray();
temp="";
/**開始處理函數(shù),通過一個while循環(huán)循環(huán)整個函數(shù)*/
int I=0;
while(I<methodCha.length)
{
if(methodCha[0]==61&&methodCha[1]==61)
System.out.println("< 1,"+methodCha[0]+methodCha[1]+">");
/**判斷是否為界符或運(yùn)算符,如果是則輸出對應(yīng)序號*/
else if(((methodCha[I]>=33&&methodCha[I]<=47)
||(methodCha[I]>=58)&&methodCha[I]<=63)
&&methodCha[I]!=44)
{System.out.println("< "+((int)methodCha[I]-32)+","+methodCha[I]+">");I++;}
/**判斷是否為字母(包括大小寫)或數(shù)字,如果是通過一個while循環(huán)將字符存入temp字符串中*/
else if(((48<=methodCha[I]&&methodCha[I]<=57)
||(65<=methodCha[I]&&methodCha[I]<=90)
||(97<=methodCha[I]&&methodCha[I]<=122)
))
{while((48<=methodCha[I]&&methodCha[I]<=57)
||(65<=methodCha[I]&&methodCha[I]<=90)
||(97<=methodCha[I]&&methodCha[I]<=122)
)
{temp+=methodCha[I];
I++;
}
/**判斷temp是否為空,如果非空,則說明有字符存入temp字符串中,*如果第一個字符為字母,則說明temp所存入的是一變量名或關(guān)鍵字*/
if(!temp.equals(""))
{char []tempCha=temp.toCharArray();
if((65<=tempCha[0]&&tempCha[0]<=90)
||(97<=tempCha[0]&&tempCha[0]<=122))
{if(isKey(temp)!=0)System.out.println("< "+temp+","+ isKey(temp)+ ">");
else System.out.println("< ID ,"+temp+" >");
}
/**如果temp的長度小于2,說明存入的是數(shù)字0,輸出*/
else if(temp.length()<2)System.out.println("< NUM,"+temp+" >");
/**其它情況為整數(shù),輸出,將temp清空*/
else System.out.println("<NUM,"+temp+" >");
temp="";
}
}
/**非上述情況,為防止死循環(huán),直接I++跳出,進(jìn)入下次循環(huán)*/
else I++;
}
}
/**isKey函數(shù)用來判斷是否為關(guān)鍵字,如果是返回相應(yīng)序號,假則返回0*/
public int isKey(String str)
{
int flag=0;
if(str.equals("if")) flag=5;
if(str.equals("else")) flag=6;
if(str.equals("while")) flag=7;
if(str.equals("do")) flag=8;
if(str.equals("begin")) flag=9;
if(str.equals("call")) flag=10;
if(str.equals("const")) flag=11;
if(str.equals("odd")) flag=12;
if(str.equals("end")) flag=13;
if(str.equals("procedure")) flag=14;
if(str.equals("read")) flag=15;
if(str.equals("var")) flag=16;
if(str.equals("write")) flag=17;
return flag;
}
public static void main(String args[])
{ System.out.println("===============================");
System.out.println("===============================");
System.out.println("==....ID.. 代表...字符串......==");
System.out.println("==....NUM.. 代表...十進(jìn)制數(shù)字..==");
System.out.println("==....界符位于左邊,右邊為對應(yīng)序號....==");
System.out.println("==....運(yùn)算符位于左邊,右邊為對應(yīng)序號....==");
System.out.println("==....關(guān)鍵字位于左邊,右邊為對應(yīng)序號==");
System.out.println("===============================");
System.out.println("===============================");
wordAna1 wordAnalasis= new wordAna1();
try
{
File aFile=new File("WriteExample.txt"); //指定文件名
//建立輸出流
FileOutputStream out= new FileOutputStream(aFile);
byte[] b=new byte[4096];
String str=("< ID ,"+" >");
b=str.getBytes(); //進(jìn)行String到byte[]的轉(zhuǎn)化
out.write(b); //寫入文本內(nèi)容
}
catch (IOException e)
{
System.out.println(e.toString());
}
System.exit(0);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -