课堂测试1

ⅰ亾dé卋堺 提交于 2019-12-03 20:10:06

         输出某个英文文本文件中 26 字母出现的频率,由高到低排列,并显示字母出现的百分比,精确到小数点后面两位。

         字母频率 = 这个字母出现的次数 / (所有A-Z,a-z字母出现的总数)

         如果两个字母出现的频率一样,那么就按照字典序排列。

        

package Statistics;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileOutput {
    private static  int[][] letter=new int[2][52];
    public static void main(String[] args) throws IOException {
        StringBuilder sb = new StringBuilder();
        File directory = new File("");//参数为空 
        String courseFile = directory.getCanonicalPath()+"/Harry Potter and the Sorcerer's Stone.txt"; 
        BufferedReader bf = new BufferedReader(new FileReader(courseFile));
        String content = null;
        while((content = bf.readLine()) != null){
            sb.append(content.trim());
        }
        for(int j=0;j<52;j++)
        {
            letter[0][j]=j;
            letter[1][j]=0;
        }    
        countCracts(sb.toString(), 'a', 'z');
        countCracts(sb.toString(), 'A', 'Z');
        for(int i=0;i<52;i++)
        {
            int max=letter[1][i],idmax=i;
            for(int j=i+1;j<52;j++)
            {
                if(max<letter[1][j])
                {
                    max=letter[1][j];
                    idmax=j;
                }
            }
            int k,t;
            k=letter[1][i];
            letter[1][i]=letter[1][idmax];
            letter[1][idmax]=k;
            t=letter[0][i];
            letter[0][i]=letter[0][idmax];
            letter[0][idmax]=t;
        }
        double m=0;
        for(int i=0;i<52;i++)
        {
            m+=letter[1][i];
        }
        for(int i=0;i<52;i++)
        {
            switch(letter[0][i])
            {
            case 0:System.out.print("A:");break;
            case 1:System.out.print("B:");break;
            case 2:System.out.print("C:");break;
            case 3:System.out.print("D:");break;
            case 4:System.out.print("E:");break;
            case 5:System.out.print("F:");break;
            case 6:System.out.print("G:");break;
            case 7:System.out.print("H:");break;
            case 8:System.out.print("I:");break;
            case 9:System.out.print("J:");break;
            case 10:System.out.print("K:");break;
            case 11:System.out.print("L:");break;
            case 12:System.out.print("M:");break;
            case 13:System.out.print("N:");break;
            case 14:System.out.print("O:");break;
            case 15:System.out.print("P:");break;
            case 16:System.out.print("Q:");break;
            case 17:System.out.print("R:");break;
            case 18:System.out.print("S:");break;
            case 19:System.out.print("T:");break;
            case 20:System.out.print("U:");break;
            case 21:System.out.print("V:");break;
            case 22:System.out.print("W:");break;
            case 23:System.out.print("X:");break;
            case 24:System.out.print("Y:");break;
            case 25:System.out.print("Z:");break;
            case 26:System.out.print("a:");break;
            case 27:System.out.print("b:");break;
            case 28:System.out.print("c:");break;
            case 29:System.out.print("d:");break;
            case 30:System.out.print("e:");break;
            case 31:System.out.print("f:");break;
            case 32:System.out.print("g:");break;
            case 33:System.out.print("h:");break;
            case 34:System.out.print("i:");break;
            case 35:System.out.print("j:");break;
            case 36:System.out.print("k:");break;
            case 37:System.out.print("l:");break;
            case 38:System.out.print("m:");break;
            case 39:System.out.print("n:");break;
            case 40:System.out.print("o:");break;
            case 41:System.out.print("p:");break;
            case 42:System.out.print("q:");break;
            case 43:System.out.print("r:");break;
            case 44:System.out.print("s:");break;
            case 45:System.out.print("t:");break;
            case 46:System.out.print("u:");break;
            case 47:System.out.print("v:");break;
            case 48:System.out.print("w:");break;
            case 49:System.out.print("x:");break;
            case 50:System.out.print("y:");break;
            case 51:System.out.print("z:");break;
            }
            System.out.println(String.format("%.2f",(double)(letter[1][i]/m*100))+"%");
        }
    }
    private static void countCracts(String str, char start, char end) {
        for(char a = start; a <= end; a++){
            int cractCount = str.length() - str.replaceAll(String.valueOf(a), "").length();
            if(a>='A'&&a<='Z')
            {
                int m=(int)(a-'A'+1);
                switch(m)
                {
                case 1:letter[1][0]=cractCount;break;
                case 2:letter[1][1]=cractCount;break;
                case 3:letter[1][2]=cractCount;break;
                case 4:letter[1][3]=cractCount;break;
                case 5:letter[1][4]=cractCount;break;
                case 6:letter[1][5]=cractCount;break;
                case 7:letter[1][6]=cractCount;break;
                case 8:letter[1][7]=cractCount;break;
                case 9:letter[1][8]=cractCount;break;
                case 10:letter[1][9]=cractCount;break;
                case 11:letter[1][10]=cractCount;break;
                case 12:letter[1][11]=cractCount;break;
                case 13:letter[1][12]=cractCount;break;
                case 14:letter[1][13]=cractCount;break;
                case 15:letter[1][14]=cractCount;break;
                case 16:letter[1][15]=cractCount;break;
                case 17:letter[1][16]=cractCount;break;
                case 18:letter[1][17]=cractCount;break;
                case 19:letter[1][18]=cractCount;break;
                case 20:letter[1][19]=cractCount;break;
                case 21:letter[1][20]=cractCount;break;
                case 22:letter[1][21]=cractCount;break;
                case 23:letter[1][22]=cractCount;break;
                case 24:letter[1][23]=cractCount;break;
                case 25:letter[1][24]=cractCount;break;
                case 26:letter[1][25]=cractCount;break;
                }
            }
            if(a>='a'&&a<='z')
            {
                int m=(int)(a-'a'+1);
                switch(m)
                {
                case 1:letter[1][26]=cractCount;break;
                case 2:letter[1][27]=cractCount;break;
                case 3:letter[1][28]=cractCount;break;
                case 4:letter[1][29]=cractCount;break;
                case 5:letter[1][30]=cractCount;break;
                case 6:letter[1][31]=cractCount;break;
                case 7:letter[1][32]=cractCount;break;
                case 8:letter[1][33]=cractCount;break;
                case 9:letter[1][34]=cractCount;break;
                case 10:letter[1][35]=cractCount;break;
                case 11:letter[1][36]=cractCount;break;
                case 12:letter[1][37]=cractCount;break;
                case 13:letter[1][38]=cractCount;break;
                case 14:letter[1][39]=cractCount;break;
                case 15:letter[1][40]=cractCount;break;
                case 16:letter[1][41]=cractCount;break;
                case 17:letter[1][42]=cractCount;break;
                case 18:letter[1][43]=cractCount;break;
                case 19:letter[1][44]=cractCount;break;
                case 20:letter[1][45]=cractCount;break;
                case 21:letter[1][46]=cractCount;break;
                case 22:letter[1][47]=cractCount;break;
                case 23:letter[1][48]=cractCount;break;
                case 24:letter[1][49]=cractCount;break;
                case 25:letter[1][50]=cractCount;break;
                case 26:letter[1][51]=cractCount;break;
                }
            }
        }
    }
    
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!