11.4Java课堂训练

昨天Java课堂上做文件的导入,检索小说内的字母单词出现频率。

package javatest1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeMap;
import java.util.TreeSet;
public class Test1 {
public static void main(String [] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("C:\\Users\\lenovo\\Desktop\\2.txt"));
TreeMap<Character,Integer> hm=new TreeMap<>();
int bb;
while((bb=br.read())!=-1) {
if((bb>='A'&&bb<='Z')||(bb>'a'&&bb<='z')) {
hm.put((char)bb,hm.containsKey((char)bb)?hm.get((char)bb)+1:1);
}

}
br.close();
int max=0;
int sum=0;
int t=0;
for(Character k: hm.keySet()) {
sum=sum+hm.get(k);
}
TreeSet<Character> ts=new TreeSet<>(new Comparator<Character>()
{
public int compare(Character a,Character b) {
int num=hm.get(a)-hm.get(b);
return num==0?1:(-num);
}
});
for(Character k: hm.keySet()) {
ts.add(k);
}
DecimalFormat df = new DecimalFormat("0.00%");
for (Character c : ts)
{
float bai=(float)hm.get(c)/sum;

System.out.println(c+" "+hm.get(c)+" "+df.format(bai));
}
}
}

///////////2

package javatest1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class Test3 {

class Word//定义单词类
{
String value;//具体的单词
int geshu;//出现的个数
Word next;//将单词链起来
public Word(String value,int geshu)//带参构造函数
{
this.value=value;
this.geshu=geshu;
next=null;
}
public Word()//空构造函数
{
this.value="";
this.geshu=0;
next=null;
}
}
/*
* 读取指定路径下的文件名和目录名
*/
public void getFileList() throws IOException {
BufferedReader br=new BufferedReader(new FileReader("C:\\Users\\lenovo\\Desktop\\2.txt"));
TreeMap<Character,Integer> hm=new TreeMap<>();
int bb;
while((bb=br.read())!=-1) {
Word word=new Word();//单词的链头
Word lian,xin;
String str="";//读取英文文件
char[] c=new char[1];//每次读取一个字母
int b=0;
boolean exist=false;//判断单词是否存在于 word 链中
while((b=br.read(c))!=-1)//每次读取一个字母直到最后
{
//如果字符为 换行、空格、单引号、双引号、逗号、句号 则为一个单词的结束及另一个单词的开始
if(String.valueOf(c).equals("\r")||String.valueOf(c).equals("\n")||String.valueOf(c).equals(" ")||String.valueOf(c).equals(",")||String.valueOf(c).equals(".")||String.valueOf(c).equals("\"")||String.valueOf(c).equals("'"))
{
lian=word;
while(lian!=null)
{
if(lian.value.equalsIgnoreCase(str))//如果单词在单词链中存在,则单词个数++
{
lian.geshu++;exist=true;
break;
}
else
{
lian=lian.next;
}
}
if(exist==false)//如果不存在,则在单词链中添加
{
xin=new Word(str,1);
xin.next=word.next;
word.next=xin;
str="";
}
else
{
exist=false;
str="";
}
}
else//单词
{
str+=String.valueOf(c);
}
}
int N=6726;
for(int i=1;i<=N;i++)
{
xin=new Word("",0);
lian=word.next;
//找到单词链中个数最多的
while(lian!=null)
{
if(lian.geshu>xin.geshu)
{
xin=lian;
}
lian=lian.next;
}
//输出单词链中个数最多的
System.out.println("第"+i+"个 :"+xin.value+" 个数:"+xin.geshu);
lian=word;
//删除单词链中单词个数最多的
while(lian.next!=null)
{
if(lian.next.value.equalsIgnoreCase(xin.value))
{
lian.next=lian.next.next;
break;
}
lian=lian.next;
}
}
}
}
public static void main(String[] args) throws IOException {
Test3 rf = new Test3();
rf.getFileList();
}
}

其中要求2我使用了链表,有点儿乱。

原文地址:https://www.cnblogs.com/aiyyue/p/11802155.html