一、测试
英语的26 个字母的频率在一本小说中是如何分布的?某类型文章中常出现的单词是什么?某作家最常用的词汇是什么?《飘》 中最常用的短语是什么,等等
1、要求1:
输出某个英文文本文件中 26 字母出现的频率,由高到低排列,并显示字母出现的百分比,精确到小数点后面两位
(注:1、字母频率 = 这个字母出现的次数 / (所有A-Z,a-z字母出现的总数)
2、如果两个字母出现的频率一样,那么就按照字典序排列)
程序设计思路:
1、创建一个char 数组word,存入a-Z 52个字母,再创建一个52个空间的double数组wordnum ,用来存储a-Z出现的次数,再定义一个double型总字母数sum
2、循环读入字符,每次按32个字符读取文件,遍历这32个字符中每个字符与word中字母进行匹配,匹配成功,则对应的wordnum加1,并且总字母数sum加1
3、用比较排序法对wordnum中最大元素下标max进行查找,找到就输出wordnum[max]和word[max],然后根据wordnum[max]和总字母数sum计算并控制位数输出百分比
4、再将max和wordnum[max]都等于0,循环52次第三步即可
源代码:
import java.io.*; import java.text.DecimalFormat; public class Sumzimu { static DecimalFormat df=new DecimalFormat("######0.00"); public static void main(String[] args) throws IOException { FileReader fr = null; try { //创建字符输入流 fr = new FileReader("C:\\Users\\米羊\\Desktop\\Harry Potter and the Sorcerer‘s Stone.txt"); //创建一个长度为32的“竹筒” char[] cbuf = new char[32]; char [] word= {‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘,‘h‘,‘i‘,‘j‘,‘k‘,‘l‘,‘m‘,‘n‘,‘o‘,‘p‘,‘q‘,‘r‘,‘s‘,‘t‘,‘u‘,‘v‘,‘w‘,‘x‘,‘y‘,‘z‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘,‘H‘,‘I‘,‘J‘,‘K‘,‘L‘,‘M‘,‘N‘,‘O‘,‘P‘,‘Q‘,‘R‘,‘S‘,‘T‘,‘U‘,‘V‘,‘W‘,‘X‘,‘Y‘,‘Z‘}; double [] wordnum = new double [52]; double sum=0; for(int i=0;i<52;i++) { wordnum[i]=0; } //用于保存实际读取的字符数 int hasRead = 0; char input ; //使用循环来重复“取水”过程 while ((hasRead = fr.read(cbuf)) > 0 ) { for(int i=0;i<32;i++) { input = cbuf[i]; for(int j=0;j<52;j++) { if(input==word[j]) { wordnum[j]++; sum++; } } } //取出“竹筒”中水滴(字节),将字符数组转换成字符串输入! //System.out.print(new String(cbuf , 0 , hasRead)); } //System.out.println(sum); int maxnum=0; for(int i=0;i<52;i++) { maxnum = 0; for(int j=0;j<52;j++) { if(wordnum[maxnum]<wordnum[j]) maxnum=j; } System.out.println(word[maxnum]+" "+df.format(wordnum[maxnum]/sum*100)+"%"); wordnum[maxnum]=0; } } catch (IOException ioe) { ioe.printStackTrace(); } finally { //使用finally块来关闭文件输入流 if (fr != null) { fr.close(); } } } }
测试截图
2、要求2
输出单个文件中的前 N 个最常出现的英语单词
要求以英文字母开头,由英文字母和字母数字符号组成的字符串视为一个单词。单词以分隔符分割且不区分大小写。在输出时,所有单词都用小写字符表示
程序设计思路:
(1)循环读取,按行读取文件,并将读取出的行用toLowerCase()把大写改成小写,并按空格分割存进String数组中
(2)对String数组进行去重。并存到另一个String数组中
(3)对所有单词进行遍历,求出每个不重复单词的个数并将其存入int数组。
(4)用比较排序法输出前N个出现次数最多的单词
源代码
import java.util.*; import java.io.*; public class Sumdanci { private static String str=""; private static BufferedReader cin = null; private static String a[]= new String[100000]; private static int b[]= new int[100000]; private static String c[]= new String[100000]; private static int length = 0; private static int rlength = 0; private static int n = 0; private static int m = 0; public static void Input() throws IOException{ { while(str!=null) { str=str.toLowerCase();//大写字母变小写 for(int i=0;i<str.length();i++) { if((str.charAt(i)>96&&str.charAt(i)<123)) { a[m]=a[m]+str.charAt(i); } if(str.charAt(i)==‘ ‘||str.charAt(i)==‘,‘||str.charAt(i)==‘.‘) { if(!a[m].equals("")) { m=m+1; a[m]=""; } } } str=cin.readLine(); } length=m; } } public static void Removesame() { for(int i=0;i<length;i++) { b[i]=0; } c[0]=a[0]; int count = 1; boolean s = true; for(int i=1;i<length;i++) { s = false ; for(int j=0;j<count;j++) { if(a[i].equals(c[j])) { s = true; break; } } if(!s) { c[count] = a[i]; count++; } } rlength=count; for(int i=0;i<rlength;i++) { for(int j=0;j<length;j++) { if(c[i].equals(a[j])) b[i]=b[i]+1; } } } public static void Display() { int max=0; for(int i=1;i<=n;i++) { max=0; for(int j=0;j<rlength;j++) { if(b[max]<b[j]) { max=j; } } System.out.println("出现次数第"+i+"多的是: "+c[max]+"出现了: "+b[max]+"次"); b[max]=0; } } public static void ReadFile() { try { File file=new File("C:\\Users\\米羊\\Desktop\\Harry Potter and the Sorcerer‘s Stone.txt"); InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8");//指定字符编码 cin=new BufferedReader(read); str=cin.readLine(); Input(); cin.close(); read.close(); } catch(FileNotFoundException e) { System.out.println("找不到指定文件"); } catch(IOException e) { System.out.println("文件读取错误"); } } public static void main(String[] args) { System.out.println("请输入你想统计前N个出现次数最多单词的N"); Scanner input =new Scanner(System.in); n=input.nextInt(); a[0]=""; ReadFile(); Removesame(); Display(); input.close(); System.out.println("输出完毕"); } }
测试截图
3、要求3的功能1
输出文件中所有不重复的单词,按照出现次数由多到少排列,出现次数同样多的,以字典序排列
程序设计思路:
(1)循环读取,按行读取文件,并将读取出的行用toLowerCase()把大写改成小写,并按空格分割存进String数组中
(2)对String数组进行去重。并存到另一个String数组中
(3)对所有单词进行遍历,求出每个不重复单词的个数并将其存入int数组。
(4)用比较排序法按从多到少输出所有单词及其个数
源代码:
import java.util.*; import java.io.*; public class Sumdanci1 { private static String str=""; private static BufferedReader cin = null; private static String a[]= new String[100000]; private static int b[]= new int[100000]; private static String c[]= new String[100000]; private static int length = 0; private static int rlength = 0; private static int n = 0; private static int m = 0; public static void Input() throws IOException{ { while(str!=null) { str=str.toLowerCase();//大写字母变小写 for(int i=0;i<str.length();i++) { if((str.charAt(i)>96&&str.charAt(i)<123)) { a[m]=a[m]+str.charAt(i); } if(str.charAt(i)==‘ ‘||str.charAt(i)==‘,‘||str.charAt(i)==‘.‘) { if(!a[m].equals("")) { m=m+1; a[m]=""; } } } str=cin.readLine(); } length=m; } } public static void Removesame() { for(int i=0;i<length;i++) { b[i]=0; } c[0]=a[0]; int count = 1; boolean s = true; for(int i=1;i<length;i++) { s = false ; for(int j=0;j<count;j++) { if(a[i].equals(c[j])) { s = true; break; } } if(!s) { c[count] = a[i]; count++; } } rlength=count; for(int i=0;i<rlength;i++) { for(int j=0;j<length;j++) { if(c[i].equals(a[j])) b[i]=b[i]+1; } } } public static void ReadFile() { try { File file=new File("C:\\Users\\米羊\\Desktop\\Harry Potter and the Sorcerer‘s Stone.txt"); InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8");//指定字符编码 cin=new BufferedReader(read); str=cin.readLine(); Input(); cin.close(); read.close(); } catch(FileNotFoundException e) { System.out.println("找不到指定文件"); } catch(IOException e) { System.out.println("文件读取错误"); } } public static void Writefile() throws IOException { File file=new File("C:\\Users\\米羊\\Desktop\\fenxijieguo.txt"); if(!file.exists()) file.createNewFile(); FileWriter write = new FileWriter(file,true); BufferedWriter out=new BufferedWriter(write); int max=0; for(int i=1;i<=rlength;i++) { max=0; for(int j=0;j<rlength;j++) { if(b[max]<b[j]) { max=j; } } System.out.println("出现次数第"+i+"多的是: "+c[max]+"出现了: "+b[max]+"次"); out.write("出现次数第"+i+"多的是: "+c[max]+"出现了: "+b[max]+"次"); out.newLine(); b[max]=0; } out.close(); } public static void main(String[] args) throws IOException { a[0]=""; ReadFile(); Removesame(); Writefile(); System.out.println("输出完毕"); } }
测试截图
这是输出到文件中
4、要求3的功能2:
指定文件目录,对目录下每一个文件执行 功能1的操作
程序设计思路:
(1)找出用户指定目录中的所有文件,将文件路径名存入String数组
(2)对所有文件路径名进行去重。并存到另一个String数组中
(3)对所有文件路径名进行遍历,求出每个文件路径名的个数存入一个int数组
(4)用比较排序法对int数组进行排序
(4)输出所有文件路径名及其对应int数组中元素的值
源代码:
import java.io.*; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.*; public class Sumdanci2 { private static String str=""; private static BufferedReader cin = null; private static String a[]= new String[100000]; private static int b[]= new int[100000]; private static String c[]= new String[100000]; private static int length = 0; private static int rlength = 0; private static int n = 0; private static int m = 0; static File[] list = new File("D:\\Test").listFiles(); public static void Removesame() { for(int i=0;i<length;i++) { b[i]=0; } c[0]=a[0]; int count = 1; boolean s = true; for(int i=1;i<length;i++) { s = false ; for(int j=0;j<count;j++) { if(a[i].equals(c[j])) { s = true; break; } } if(!s) { c[count] = a[i]; count++; } } rlength=count; for(int i=0;i<rlength;i++) { for(int j=0;j<length;j++) { if(c[i].equals(a[j])) b[i]=b[i]+1; } } } public static void Display() { int max=0; for(int i=0;i<rlength;i++) { max=0; for(int j=0;j<rlength;j++) { if(b[max]<b[j]) { max=j; } } System.out.print(c[i]+" "+b[i]+" "); System.out.printf("%.2f",(double)b[i]/rlength*100); System.out.print("%"); System.out.println(""); b[max]=0; } } public static void rode(File[] list) { for(File file : list) { if(file.isFile()) { a[length]=file.getAbsolutePath(); length++; } } } public static void main(String[] args) throws IOException { rode(list); Removesame(); Display(); System.out.println("输出完毕"); } }
测试截图
5、要求3的功能3
指定文件目录, 但是会递归遍历目录下的所有子目录,每个文件执行功能1的做操作
程序设计思路:
(1)找出用户指定目录中的所有文件路径名存入一个String数组,如果是文件夹再递归调用之前操作(找出用户指定目录中的所有文件路径名存入一个String数组)
(2)对所有文件路径名进行去重。并存到另一个String数组中
(3)对所有文件路径名进行遍历,求出每个文件路径名的个数存入一个int数组
(4)用比较排序法对int数组进行排序
(4)输出所有文件路径名及其对应int数组中元素的值
源代码:
import java.io.*; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; public class Sumdanci3{ private static String str=""; private static BufferedReader cin = null; private static String a[]= new String[100000]; private static int b[]= new int[100000]; private static String c[]= new String[100000]; private static int length = 0; private static int rlength = 0; private static int n = 0; private static int m = 0; static File[] list = new File("D:\\Test").listFiles(); public static void Removesame() { for(int i=0;i<length;i++) { b[i]=0; } c[0]=a[0]; int count = 1; boolean s = true; for(int i=1;i<length;i++) { s = false ; for(int j=0;j<count;j++) { if(a[i].equals(c[j])) { s = true; break; } } if(!s) { c[count] = a[i]; count++; } } rlength=count; for(int i=0;i<rlength;i++) { for(int j=0;j<length;j++) { if(c[i].equals(a[j])) b[i]=b[i]+1; } } } public static void Display() { int max=0; for(int i=0;i<rlength;i++) { max=0; for(int j=0;j<rlength;j++) { if(b[max]<b[j]) { max=j; } } System.out.print(c[i]+" "+b[i]+" "); System.out.printf("%.2f",(double)b[i]/rlength*100); System.out.print("%"); System.out.println(""); b[max]=0; } } public static void rode(File[] list) { for(File file : list) { if(file.isFile()) { a[length++]=file.getAbsolutePath(); } else if(file.isDirectory()) { String str3=file.getAbsolutePath(); list = new File(str3).listFiles(); rode(list); } } } public static void main(String[] args) throws IOException { rode(list); Removesame(); Display(); System.out.println("输出完毕"); } }
测试截图
原文地址:https://www.cnblogs.com/yang2000/p/11804233.html