-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeek.java
More file actions
executable file
·59 lines (49 loc) · 1.64 KB
/
Seek.java
File metadata and controls
executable file
·59 lines (49 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.io.IOException;
import java.io.RandomAccessFile;
public class Seek
{
public static void main(String[] args)throws Exception
{
RandomAccessFile rf = new RandomAccessFile("hello2.txt", "rw");
System.out.println("全文共有"+rf.length()+"个字节。");
int[] array = new int[128];
for (int i=0;i<rf.length() ;i++ ) {
int j = rf.readByte();
array[j]++;
}
int[] array1 = new int[26];
int total=0,single=0;int all=0;
for(int i=0;i<26;i++){ array1[i]=array[i+65]+array[i+97];if(array1[i]!=0)total++;if(array1[i]==1)single++;}
System.out.print("全文中共出现了"+total+"个字母,");
System.out.println("其中有"+single+"个字母只出现了一次。");
System.out.println("每个字母出现的频率如下:");
for(int i=0;i<26;i++){all=all+array1[i];}
System.out.println("字母的总数量为:"+all);
int[] sortedArray =(int[])array1.clone();bubbleSort(sortedArray);
for(int i=0;i<26;i++){
for(int j=0;j<26;j++){
if(array1[j]==sortedArray[i]&&array1[j]!=-1){
char c=(char)(j+97);
//为了更好的控制输出的数据的形式,使之都保持小数点后四位,此处用了printf
System.out.printf("%s:%.4f(共%d个)%n",c,(float)array1[j]/all,array1[j]);
array1[j]=-1;}
}}
}
public static void bubbleSort(int[] numbers)
{
int temp = 0;
int size = numbers.length;
for(int i = 0 ; i < size-1; i ++)
{
for(int j = 0 ;j < size-1-i ; j++)
{
if(numbers[j] < numbers[j+1])
{
temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
}
}