Test1.java
package class_five; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.text.DecimalFormat; public class Test1 { int j=0; int ci=0; double sum=0; double a1; static char zimu[] = new char[26];//存储字母‘a-z’ static char shu[] = new char[2000];//存储单个字母内容 double count[]=new double[26]; public static void main(String[] args) { Test1 t1=new Test1(); t1.test("D:\Test\a.txt"); t1.display(); } public void test(String pathname) { Test1 t1=new Test1(); try { File filename=new File(pathname); InputStreamReader reader=new InputStreamReader(new FileInputStream(filename)); BufferedReader br=new BufferedReader(reader); String line[]=new String[100];; for(int i=0;i<line.length;i++) { line[i]=br.readLine(); } br.close(); int k=0; while(line[k]!=null) { for(int i=0;i<line[k].length();i++) { shu[j]=line[k].charAt(i); j++; } k++; } /*初始化字母数组*/ t1.reLetter(); /*依次判断字母并计数*/ for(int i=0;i<shu.length;i++) { switch(shu[i]) { case 'a'|'A':count[0]++;break; case 'b'|'B':count[1]++;break; case 'c'|'C':count[2]++;break; case 'd'|'D':count[3]++;break; case 'e'|'E':count[4]++;break; case 'f'|'F':count[5]++;break; case 'g'|'G':count[6]++;break; case 'h'|'H':count[7]++;break; case 'i'|'I':count[8]++;break; case 'j'|'J':count[9]++;break; case 'k'|'K':count[10]++;break; case 'l'|'L':count[11]++;break; case 'm'|'M':count[12]++;break; case 'n'|'N':count[13]++;break; case 'o'|'O':count[14]++;break; case 'p'|'P':count[15]++;break; case 'q'|'Q':count[16]++;break; case 'r'|'R':count[17]++;break; case 's'|'S':count[18]++;break; case 't'|'T':count[19]++;break; case 'u'|'U':count[20]++;break; case 'v'|'V':count[21]++;break; case 'w'|'W':count[22]++;break; case 'x'|'X':count[23]++;break; case 'y'|'Y':count[24]++;break; case 'z'|'Z':count[25]++;break; } } }catch (Exception e) { e.printStackTrace(); } } public void display() { DecimalFormat df = new DecimalFormat("0.00"); System.out.println("短文中各字母出现情况统计如下:"); /*求字母总数*/ for(int i=0;i<26;i++) { sum+=count[i]; } /*比较出现频率大小排序、冒泡法、相同频率按字母先后顺序排序*/ for(int x=0;x<26-1;x++) { for (int y=x+1;y<26;y++) { if (count[x]>count[y]) { double temp=count[x]; count[x]=count[y]; count[y]=temp; char temp1=zimu[x]; zimu[x]=zimu[y]; zimu[y]=temp1; } } } /*循环显示字母出现频率*/ for(int i=0;i<=25;i++) { ci++; a1=count[i]/sum*100; System.out.println(ci+".字母"+zimu[i]+"的出现频率是:"+df.format(a1)+"%"); } /*显示字母总数*/ System.out.println("共有"+(int)sum+"个字母"); } /*初始化字母数组*/ public void reLetter() { for(int i=0;i<26;i++) { zimu[0]='a'; zimu[1]='b'; zimu[2]='c'; zimu[3]='d'; zimu[4]='e'; zimu[5]='f'; zimu[6]='g'; zimu[7]='h'; zimu[8]='i'; zimu[9]='j'; zimu[10]='k'; zimu[11]='l'; zimu[12]='m'; zimu[13]='n'; zimu[14]='o'; zimu[15]='p'; zimu[16]='q'; zimu[17]='r'; zimu[18]='s'; zimu[19]='t'; zimu[20]='u'; zimu[21]='v'; zimu[22]='w'; zimu[23]='x'; zimu[24]='y'; zimu[25]='z'; } } }
Test2.java
package class_five; import class_third_copy.Test1; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class Test2 extends Test1{ static String wd=""; static String le[]=new String[1000]; static String wd2[]=new String[1000]; static String wd3[]=new String[1000]; static String wd4[]=new String[1000]; public static void main(String[] args) { Test2 t2=new Test2(); //t1.test("D:\Test\a.txt"); //t1.display(); //t2.test2(); t2.test3("D:\Test\input1.txt"); /* 法一 利用缓冲流写入String // BufferedWriter bw=new BufferedWriter(new FileWriter("D:\Test\input1.txt")); // bw.write(new String));//或者bw.append(new String(buf)); // bw.newLine();//写入一个换行符,根据需要 // bw.flush(); // bw.close(); */ /*法二 使用PrintWriter的print()系列方法*/ // PrintWriter pw = null; // try { // pw = new PrintWriter(new BufferedWriter(new FileWriter("D:\Test\output1.txt"))); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // pw.print(wd4); // pw.close(); } /*提取出单词*/ public void test2(String path) { Test1 t1=new Test1(); int count1=0; try { t1.test(path); for(int i=0;i<Test1.shu.length;i++) { if((Test1.shu[i]>='a'&&Test1.shu[i]<='z')||(Test1.shu[i]>='A'&&Test1.shu[i]<='Z')) { wd+=Test1.shu[i]; continue; }else { wd2[count1]=wd; wd=""; if(wd2[count1]!=""||wd2[count1]!=null) { count1++; } } } for(int i=0;i<count1;i++) { } }catch(Exception e) { e.printStackTrace(); } } /*去掉重复的单词*/ public void test3(String path) { Test1 t1=new Test1(); int count1=0; try { t1.test(path); for(int i=0;i<Test1.shu.length;i++) { if((Test1.shu[i]>='a'&&Test1.shu[i]<='z')||(Test1.shu[i]>='A'&&Test1.shu[i]<='Z')) { wd+=Test1.shu[i]; continue; }else if(wd.length()!=1){ wd2[count1]=wd; wd=""; if(wd2[count1]!=""||wd2[count1]==null) { count1++; } }else { wd=""; } } /* int count2=0; for(int i=0;i<count1;i++) { for(int j=0;j<count1;j++) { if(j!=i) { if(wd2[i].equals(wd2[j])==true) { break; } } if(j==count1-1) { wd3[count2]=wd2[i]; System.out.println(wd2[i]); System.out.println(wd3[count2]); count2++; } } } for(int i=0;i<count1;i++) { for(int j=0;j<count1;j++) { if(j!=i) { if(wd2[i].equals(wd2[j])==true) { wd3[count2]=wd2[i]; count2++; break; } } } } */ for(int i=0;i<count1;i++) { System.out.println(wd2[i]); } int num1=1; String str=wd2[0]; wd4[0]=str; for(int i=1;i<count1;i++) { char c=str.charAt(str.length()-1); if(c>='A' && c<='Z'){ c+=32; } String str1= String.valueOf(c); System.out.println(); for(int j=i+1;j<count1;j++) { char d=wd2[j].charAt(0); if(d>='A' && d<='Z'){ d+=32; } String str2= String.valueOf(d); if(str1.equals(str2)) { str=wd2[j]; wd4[num1]=wd2[j]; num1++; break; } } } for(int i=0;i<num1;i++) { System.out.println(wd4[i]); } /*int message_a=0; int num_3=1; wd3[0]=wd2[0]; for(int i=0;i<count1;i++) { System.out.println(num_3); for(int j=i+1;j<num_3;j++) { if(wd2[i].equals(wd3[j])==true) { break; } if(j==num_3-1) { message_a=1; } } if(message_a==1) { message_a=0; wd3[num_3+1]=wd2[i]; num_3++; } } for(int i=0;i<num_3;i++) { System.out.println(wd3[4]); }*/ }catch(Exception e) { e.printStackTrace(); } } }