第四周作业2.2

[实验任务二]:递归方法1)    使用递归方式判断某个字串是否是回文( palindrome );
“回文”是指正着读、反着读都一样的句子。比如“我是谁是我”
使用递归算法检测回文的算法描述如下:
A single or zero-character string is a palindrome.
Any other string is a palindrome if the first and last characters are the same, and the string that remains, excepting those characters, is a palindrome.
package first;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/*
 * 使用递归方式判断某个字串是否是回文( palindrome )
 * 从文件中读入
 */
public class palindrome {
     
    public static void main(String[] args) throws IOException {
        String url = "Text1.txt";
        File file = new File(url);// 指定操作文件
        if (!file.exists() || file.isDirectory()) {
            System.out.println("文件不存在!");
            return;
        } else
            System.out.println("文件导出成功");
        StringBuffer sb0 = new StringBuffer();

        BufferedReader br;
        try {
            br = new BufferedReader(new FileReader(file));
            String temp = null;
            //sb = new StringBuffer();
            temp = br.readLine();
            while (temp != null) {
                sb0.append(temp + "\r");
                temp = br.readLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String sb = sb0.toString();
        boolean flag = find(sb,0,sb.length());
        
        System.out.println(flag);
    }
 
    private static boolean find(String sb, int start, int length) {
        
        if(length<=1)
            return true;
        else if(sb.toCharArray()[start]==sb.toCharArray()[length-1]){
            return find(sb,start+1,length-1);
        }
        return false;
    }
}

[实验任务三]统计分析

1)  用户需求:英语的26 个字母的频率在一本小说中是如何分布的?某类型文章中常出现的单词是什么?某作家最常用的词汇是什么?《哈利波特》 中最常用的短语是什么,等等。

(2)  要求:输出单个文件中的前 N 个最常出现的英语单词,并将结果输入到文本文件中。

package pinlv;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Pinlv {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader("D:\\wenzhang.txt"));
        StringBuffer buffer = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        reader.close();
        Pattern expression = Pattern.compile("[a-zA-Z]+");
        String string = buffer.toString();
        Matcher matcher = expression.matcher(string);//
        Map<String, Integer> map = new TreeMap<String, Integer>();
        String word = "";
        int times = 0;
        while (matcher.find()) {
            word = matcher.group();
            if (map.containsKey(word)) {
                times = map.get(word);
                map.put(word, times + 1);
            } else {
                map.put(word, 1);
            }
        }
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
        Comparator<Map.Entry<String, Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> left,Map.Entry<String, Integer> right) {
                return (left.getValue()).compareTo(right.getValue());
            }
        };
        Collections.sort(list, comparator);
        int last = list.size() - 1;
        for (int i = last; i > last - 6; i--) {
            String key = list.get(i).getKey();
            Integer value = list.get(i).getValue();
            System.out.println(key + " :" + value);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/daisy99lijing/p/9787251.html