单词统计续

package Word;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;

public class WordFind {

    public static void main(String[] args) {
        WordFind2[] word=new WordFind2[100];
        for(int i=0;i<100;i++) {
            word[i]=new WordFind2();
        }
        word[0].input(word);
        word[0].showAsc(word);
    }

}

class WordFind2{
    private char word;
    private int num;
    private static double time=0;
    public WordFind2() {
        word='0';
        num=1;
    }
    public static double getTime() {
        return time;
    }
    public char getWord() {
        return word;
    }
    public void setWord(char word) {
        this.word = word;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public void addnum() {
        num++;
    }
    public void input(WordFind2[] word) {
        String str;
        try {
            BufferedReader in=new BufferedReader(new FileReader("word.txt"));
            while((str = in.readLine())!=null) {
                change(str,word);
            }
            in.close();
        }
        catch (IOException e) {
            System.out.println("缓冲区错误");
    }
    }
    private void change(String str, WordFind2[] word2) {
        char[] s1=str.toCharArray();
        for(int i=0,j=0;i<s1.length;i++) {
            if((s1[i]>=65&&s1[i]<=90)||(s1[i]>=97&&s1[i]<=122)) {
                add(s1[i],word2);
                time++;
            }
        }
        
    }
    private void add(char s, WordFind2[] word2) {
        int j=0;
        boolean f=false;
        for(int i=0;i<word2.length;i++) {
            if(word2[i].getWord()=='0') {f=true;j=i;break;}
            if(s==word2[i].getWord()) {word2[i].addnum();break;}
        }
        if(f==true) {
            word2[j]=new WordFind2();
            word2[j].setWord(s);
        }
    }
    void show(WordFind2[] word2) {
        DecimalFormat df = new DecimalFormat( "0.0000");
        double b=0;
        for(int i=0;word2[i].getWord()!='0';i++) {
            b=word2[i].getNum()/word2[i].getTime();
            System.out.println(word2[i].getWord()+"\t"+df.format(b));
        }
    }
    void showAsc(WordFind2[] word2) {
        WordFind2 word=new WordFind2();
        for(int i=0;word2[i].getWord()!='0';i++) {
            //if(word2[i].getWord().equals("")) continue;
            for(int j=i+1;word2[j].getWord()!='0';j++) {
                    if(word2[i].getNum()<word2[j].getNum()) {
                        word.setWord(word2[i].getWord());
                        word.setNum(word2[i].getNum());
                        word2[i].setWord(word2[j].getWord());
                        word2[i].setNum(word2[j].getNum());
                        word2[j].setWord(word.getWord());
                        word2[j].setNum(word.getNum());
                    }
                
            }
        }
        for(int i=0;word2[i].getWord()!='0';i++) {
            //if(word2[i].getWord().equals(""))continue;
            System.out.println(word2[i].getWord()+"\t"+word2[i].getNum());//
        }
    }

}

package Word;

import java.util.Scanner;

public class Menu {

    public static void main(String[] args) {
        Scanner in =new Scanner(System.in);
        ShowMenu();
        int n=0;
        while((n=in.nextInt())!=4) {
            switch(n) {
            case 0:{
                WordFind2[] word=new WordFind2[100];
                for(int i=0;i<100;i++) {
                    word[i]=new WordFind2();
                }
                word[0].input(word);
                word[0].showAsc(word);
            };break;
            case 1:{
                WordFind3[] word=new WordFind3[999999];
                for(int i=0;i<word.length;i++) {
                    word[i]=new WordFind3();
                }
                word[0].input(word);
                word[0].showAsc(word);
            };break;
            case 2:{
                System.out.println("请输入n的数值");
                int n2=in.nextInt();
                WordFind3[] word=new WordFind3[999999];
                for(int i=0;i<word.length;i++) {
                    word[i]=new WordFind3();
                }
                word[0].input(word);
                word[0].showAsc(word,n2);
            };break;
            default:System.out.println("输入错误!请重新输入");
            }
            ShowMenu();
        }
        
    }
    
    public static void ShowMenu() {
        System.out.println("请选择:");
        System.out.println("0:输出全部的字母");
        System.out.println("1:输出全部的单词");
        System.out.println("2:输出前n项单词");
        System.out.println("3:退出");
    }

}

 

posted on 2019-05-12 19:50  哈萨K  阅读(148)  评论(0编辑  收藏  举报