當前位置:成語大全網 - 書法字典 - 統計結果放入字典並輸出。

統計結果放入字典並輸出。

導入Java . io . *;

導入Java . awt . *;

導入Java . awt . event . *;

導入Java . util . vector;

導入Java . io . file;

導入Java . util . vector;

階級語言統計

{ Vector allWorsd,noSameWord

文字統計()

{ allWorsd = new Vector();

no same word = new Vector();

}

public void wordStatistic(文件File)

{ try { RandomAccessFile in one = new RandomAccessFile(file," rw ");//創建壹個對象,指向文件file的inOne。

RandomAccessFile int wo = new RandomAccessFile(file," rw ");//創建壹個指向文件file inTwo的對象。

long wordStarPostion = 0,wordendsposition = 0;

long length = in one . length();

int flag = 1;

int c =-1;

for(int k = 0;k & lt=長度;k++)

{ c = inone . read();// inOne調用read()方法。

布爾boo =(c & lt;= ' Z ' & amp& ampc & gt= ' A ')| |(c & lt;= ' z ' & amp& ampc & gt= ' a ');

如果(噓)

{ if(flag==1)

{ wordstarposition = in one . getfile pointer()-1;

flag = 0;

}

}

其他

{ if(flag==0)

{

if(c==-1)

wordendposion = inone . getfile pointer();

其他

wordendposion = inone . getfile pointer()-1;

intwo . seek(wordstarposition);// inTwo調用seek方法將讀寫位置移動到wordStarPostion。

byte cc[]= new byte[(int)wordendposition-(int)wordstarposition];

int wo . read fully(cc);// inTwo調用readFully(byte a)方法並將cc傳遞給a。

String word =新字符串(cc);

allworsd . add(word);

如果(!(noSameWord.contains(word)))

nosameword . add(word);

}

flag = 1;

}

}

inone . close();

int wo . close();

}

catch(異常e){}

}

公共向量getAllWorsd()

{ return allWorsd

}

公共向量getNoSameWord()

{ return noSameWord

}

}

StatisticFrame類擴展Frame實現ActionListener

語言統計學;

TextArea showMessage

按鈕打開文件;

FileDialog打開FileDialog

Vector allWord,noSameWord

公共統計框架()

{ statistic = new WordStatistic();

show message = new TextArea();

openFile =新建按鈕(“打開文件”);

openfile . addactionlistener(this);

add(openFile,BorderLayout。北);

add(showMessage,BorderLayout。中心);

openfiledialog = new file dialog(this,“打開文件對話框”,file dialog。負載);

all word = new Vector();

no same word = new Vector();

setSize(350,300);

set visible(true);

addWindowListener(新WindowAdapter()

{公共void窗口關閉(WindowEvent e)

{ system . exit(0);

}

});

validate();

}

公共無效操作已執行(操作事件e)

{ no same word . clear();

all word . clear();

show message . settext(null);

openfile dialog . set visible(true);

string fileName = openfile dialog . getfile();

如果(文件名!=空)

{ statistic.wordStatistic(新文件(文件名));

all word = statistic . getallworsd();

no same word = statistic . getno same word();

show message . append(" \ n "+filename+"包含"+allWord.size()+"英文單詞");

顯示消息。append(" \ n有"+noSameWord.size()+"不同的英文單詞");

顯示消息。追加(" \ n按使用頻率順序:\ n ");

int count[]= new int[no same word . size()];

for(int I = 0;我& ltno same word . size();i++)

{ String s 1 =(String)no same word . element at(I);

for(int j = 0;j & ltall word . size();j++)

{ String S2 =(String)all word . element at(j);

if(s1.equals(s2))

count[I]++;

}

}

for(int m = 0;m & ltno same word . size();m++)

{ for(int n = m+1;n & ltno same word . size();n++)

{ if(count[n]& gt;計數[m])

{ String temp =(String)no same word . element at(m);

no same word . setelementat((String)no same word . elementat(n),m);

noSameWord.setElementAt(temp,n);

int t = count[m];

count[m]= count[n];

count[n]= t;

}

}

}

for(int m = 0;m & ltno same word . size();m++)

{ show message . append(" \ n "+(String)no same word . element at(m)+

":"+count[m]+"/"+allWord.size()+

" = "+(1.0 * count[m])/all word . size());

}

}

}

}

公共類sy6_2

{ public static void main(String args[])

{ new statistic frame();

}

}