zoukankan      html  css  js  c++  java
  • 字母统计|英语的26 个字母在一本小说中是如何分布的

        public static String input()
        {
            String s="";
        
            //表示磁盘路径的两种表示方式:1、\   2、/
            try {
    
                //1、建立连接
                InputStream is = new FileInputStream("F:/新建文件夹/tongji/aaa.txt");
                //2、开始读取信息
                //先定义一个字节数组存放数据
                byte[] b = new byte[10000];//把所有的数据读取到这个字节当中
                //声明一个int存储每次读取到的数据
                int i = 0;
                //定义一个记录索引的变量
                int index = 0;
                //循环读取每个数据
                while((i=is.read())!=-1){//把读取的数据放到i中
                    b[index]=(byte) i;
                    index++;
                }
                //把字节数组转成字符串
                
                //关闭流
                is.close();
                s=new String(b);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                //系统强制解决的问题:文件没有找到
                e.printStackTrace();
            } catch (IOException e) {
                //文件读写异常
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return s;    
        }
    public static void main(String[] args)
            {
                //定义需要计算字母出现次数的文本
                String text=daoruchu.input();
                //定义26个字母
                String letter="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                calculate(text,letter);
            }
            public static void calculate(String text,String letter)
            {
                //定义存储各字母出现次数的数组
                int []counter=new int[52];
                //定义字母总个数的计数器。排除标点符号,空格和数字。
                int total_counter=0;
                //将string类型转化成char类型
                char[] text_tr=text.toCharArray();
                char[] letter_tr=letter.toCharArray();
                //计算各字母出现次数以及总字母数
                //外层循环,遍历26个字母
                for(int i=0;i<52;i++)
                {
                    //内层循环,遍历待计算文本
                    for(int j=0;j<text_tr.length;j++)
                    {
                        //字母每在文本中出现一次,字母计数器加一,总字母个数计数器加一
                        if(text_tr[j]==letter_tr[i])
                        {
                            counter[i]++;
                            total_counter++;
                        }
                    }
                }
            
                for(int i=0;i<52;i++)
                {
                
                    //输出结果
    
                    System.out.println(letter_tr[i]+"'s number is "+counter[i]);
                }
                System.out.println("sum:"+total_counter);
            }
        

    这个程序分为两个部分:

    1、第一个类实现文件的导入导出

    2、第二个类实现字母的统计;

    目前的问题:

    文件导出仍然存在问题;主要问题是无法把统计结果转换成文件导出的字节;

  • 相关阅读:
    hdu5360 Hiking(水题)
    hdu5348 MZL's endless loop(欧拉回路)
    hdu5351 MZL's Border(规律题,java)
    hdu5347 MZL's chemistry(打表)
    hdu5344 MZL's xor(水题)
    hdu5338 ZZX and Permutations(贪心、线段树)
    hdu 5325 Crazy Bobo (树形dp)
    hdu5323 Solve this interesting problem(爆搜)
    hdu5322 Hope(dp)
    Lightoj1009 Back to Underworld(带权并查集)
  • 原文地址:https://www.cnblogs.com/KYin/p/9788259.html
Copyright © 2011-2022 走看看