题目要求:(1)输出某个英文文本文件中 26 字母出现的频率,由高到低排列,并显示字母出现的百分比,精确到小数点后面两位
(2)输出单个文件中的前 N 个最常出现的英语单词
设计思想:
出现的问题:数据量太大,一维数组长度有限,对数据的处理过于复杂,时间复杂度太高
可能的解决方案(多选):1、分批读入,操作
2、动态分配
源代码:
package main;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;public class statist
{public static void main(String[] args) throws Exception{mathmaticwords();}public static void mathmaticnumber(){try{char shu[] = new char[1000000];char zimu[] = new char[52];int j = 0;int count[] = new int[52];String pathname = "src/newAnalysis.txt";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[i] = br.readLine();System.out.println(line[i]);}br.close();int k = 0;while (line[k] != null){for (int i = 0; i ){shu[j] = line[k].charAt(i);j++;}k++;}
// 匹配表for (int i &#61; 0; i ){switch (shu[i]){case &#39;a&#39;:zimu[0] &#61; &#39;a&#39;;count[0]&#43;&#43;;break;case &#39;b&#39;:zimu[1] &#61; &#39;b&#39;;count[1]&#43;&#43;;break;case &#39;c&#39;:zimu[2] &#61; &#39;c&#39;;count[2]&#43;&#43;;break;case &#39;d&#39;:zimu[3] &#61; &#39;d&#39;;count[3]&#43;&#43;;break;case &#39;e&#39;:zimu[4] &#61; &#39;e&#39;;count[4]&#43;&#43;;break;case &#39;f&#39;:zimu[5] &#61; &#39;f&#39;;count[5]&#43;&#43;;break;case &#39;g&#39;:zimu[6] &#61; &#39;g&#39;;count[6]&#43;&#43;;break;case &#39;h&#39;:zimu[7] &#61; &#39;h&#39;;count[7]&#43;&#43;;break;case &#39;i&#39;:zimu[8] &#61; &#39;i&#39;;count[8]&#43;&#43;;break;case &#39;j&#39;:zimu[9] &#61; &#39;j&#39;;count[9]&#43;&#43;;break;case &#39;k&#39;:zimu[10] &#61; &#39;k&#39;;count[10]&#43;&#43;;break;case &#39;l&#39;:zimu[11] &#61; &#39;l&#39;;count[11]&#43;&#43;;break;case &#39;m&#39;:zimu[12] &#61; &#39;m&#39;;count[12]&#43;&#43;;break;case &#39;n&#39;:zimu[13] &#61; &#39;n&#39;;count[13]&#43;&#43;;break;case &#39;o&#39;:zimu[14] &#61; &#39;o&#39;;count[14]&#43;&#43;;break;case &#39;p&#39;:zimu[15] &#61; &#39;p&#39;;count[15]&#43;&#43;;break;case &#39;q&#39;:zimu[16] &#61; &#39;q&#39;;count[16]&#43;&#43;;break;case &#39;r&#39;:zimu[17] &#61; &#39;r&#39;;count[17]&#43;&#43;;break;case &#39;s&#39;:zimu[18] &#61; &#39;s&#39;;count[18]&#43;&#43;;break;case &#39;t&#39;:zimu[19] &#61; &#39;t&#39;;count[19]&#43;&#43;;break;case &#39;u&#39;:zimu[20] &#61; &#39;u&#39;;count[20]&#43;&#43;;break;case &#39;v&#39;:zimu[21] &#61; &#39;v&#39;;count[21]&#43;&#43;;break;case &#39;w&#39;:zimu[22] &#61; &#39;w&#39;;count[22]&#43;&#43;;break;case &#39;x&#39;:zimu[23] &#61; &#39;x&#39;;count[23]&#43;&#43;;break;case &#39;y&#39;:zimu[24] &#61; &#39;y&#39;;count[24]&#43;&#43;;break;case &#39;z&#39;:zimu[25] &#61; &#39;z&#39;;count[25]&#43;&#43;;break;case &#39;A&#39;:zimu[26] &#61; &#39;A&#39;;count[26]&#43;&#43;;break;case &#39;B&#39;:zimu[27] &#61; &#39;B&#39;;count[27]&#43;&#43;;break;case &#39;C&#39;:zimu[28] &#61; &#39;C&#39;;count[28]&#43;&#43;;break;case &#39;D&#39;:zimu[29] &#61; &#39;D&#39;;count[29]&#43;&#43;;break;case &#39;E&#39;:zimu[30] &#61; &#39;E&#39;;count[30]&#43;&#43;;break;case &#39;F&#39;:zimu[31] &#61; &#39;F&#39;;count[31]&#43;&#43;;break;case &#39;G&#39;:zimu[32] &#61; &#39;G&#39;;count[32]&#43;&#43;;break;case &#39;H&#39;:zimu[33] &#61; &#39;H&#39;;count[33]&#43;&#43;;break;case &#39;I&#39;:zimu[34] &#61; &#39;I&#39;;count[34]&#43;&#43;;break;case &#39;J&#39;:zimu[35] &#61; &#39;G&#39;;count[35]&#43;&#43;;break;case &#39;K&#39;:zimu[36] &#61; &#39;K&#39;;count[36]&#43;&#43;;break;case &#39;L&#39;:zimu[37] &#61; &#39;L&#39;;count[37]&#43;&#43;;break;case &#39;M&#39;:zimu[38] &#61; &#39;M&#39;;count[38]&#43;&#43;;break;case &#39;N&#39;:zimu[39] &#61; &#39;N&#39;;count[39]&#43;&#43;;break;case &#39;O&#39;:zimu[40] &#61; &#39;O&#39;;count[40]&#43;&#43;;break;case &#39;P&#39;:zimu[41] &#61; &#39;P&#39;;count[41]&#43;&#43;;break;case &#39;Q&#39;:zimu[42] &#61; &#39;Q&#39;;count[42]&#43;&#43;;break;case &#39;R&#39;:zimu[43] &#61; &#39;R&#39;;count[43]&#43;&#43;;break;case &#39;S&#39;:zimu[44] &#61; &#39;S&#39;;count[44]&#43;&#43;;break;case &#39;T&#39;:zimu[45] &#61; &#39;T&#39;;count[45]&#43;&#43;;break;case &#39;U&#39;:zimu[46] &#61; &#39;U&#39;;count[46]&#43;&#43;;break;case &#39;V&#39;:zimu[47] &#61; &#39;V&#39;;count[47]&#43;&#43;;break;case &#39;W&#39;:zimu[48] &#61; &#39;W&#39;;count[48]&#43;&#43;;break;case &#39;X&#39;:zimu[49] &#61; &#39;X&#39;;count[49]&#43;&#43;;break;case &#39;Y&#39;:zimu[50] &#61; &#39;Y&#39;;count[50]&#43;&#43;;break;case &#39;Z&#39;:zimu[51] &#61; &#39;Z&#39;;count[51]&#43;&#43;;}}int ci &#61; 0;int sum &#61; 0;System.out.println("短文中各字母出现情况统计如下&#xff1a;");for (int i &#61; 0; i <52; i&#43;&#43;){if (count[i] !&#61; 0){ci&#43;&#43;;sum &#43;&#61; count[i];}}ci &#61; 0;for (int i &#61; 0; i <52; i&#43;&#43;){if (count[i] !&#61; 0){ci&#43;&#43;;System.out.println(count[i]);double a &#61; (double) ((Math.round(count[i] * 100) / 100.0) / sum) * 100;double b &#61; (double) (Math.round(a * 100) / 100.0);System.out.println(ci &#43; ".字母" &#43; zimu[i] &#43; "的出现次数是:" &#43; b);}}System.out.println("字母共计&#xff1a;" &#43; sum &#43; "个");} catch (Exception e){e.printStackTrace();}}public static void mathmaticwords() throws IOException{BufferedReader br &#61; new BufferedReader(new FileReader("src/newAnalysis.txt"));StringBuffer sb &#61; new StringBuffer();String text &#61; null;while ((text &#61; br.readLine()) !&#61; null){sb.append(text);// 将读取出的字符追加到stringbuffer中
}br.close(); // 关闭读入流
String str &#61; sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写String[] words &#61; str.split("[^(a-zA-Z)]&#43;"); // 非单词的字符来分割&#xff0c;得到所有单词Map map &#61; new HashMap();for (String word : words){if (map.get(word) &#61;&#61; null){ // 若不存在说明是第一次&#xff0c;则加入到map,出现次数为1map.put(word, 1);} else{map.put(word, map.get(word) &#43; 1); // 若存在&#xff0c;次数累加1
}}// 排序List> list &#61; new ArrayList>(map.entrySet());Comparator> comparator &#61; new Comparator>(){public int compare(Map.Entry left, Map.Entry right){return (left.getValue().compareTo(right.getValue()));}};// 集合默认升序升序
Collections.sort(list, comparator);for (int i &#61; 0; i ){// 由高到低输出System.out.println(list.get(list.size() - i - 1).getKey() &#43; ":" &#43; list.get(list.size() - i - 1).getValue());}}
}
结果截图&#xff1a;
总结&#xff1a;
通过这种小练习&#xff0c;认识到两点&#xff1a;1、无论程序板块或是软件项目多大&#xff0c;它都是由一个一个极小极小的基本程序单元组成&#xff0c;我们平常在写代码时要始终贯穿模块化基本化编程思想&#xff0c;把问题的规模和复杂性降维。
2、打铁还需自身硬&#xff0c;一段时间没有编写过小程序模块&#xff0c;竟然在面对问题时不知所措&#xff0c;这看出的我的基本功不是很扎实&#xff0c;计划复习JAVA基本语法&#xff0c;以期达到熟练。