zoukankan      html  css  js  c++  java
  • 2019.11.4 上课测试

    首先统计字母的个数,和字母的百分比,区分大小写;

    第二步统计单词的个数,不区分大小写;输出单词,和单词的个数和;

    1、先将单词找出来

    2、将单词存进数组中;

    3、将重复的单词进行去重,然后再找与去重单词之后相同的个数;进行记录;

    4、输出这些单词;

    package src.zy03;
    import java.io.*;
    import java.text.DecimalFormat;
    import java.util.Comparator;
    import java.util.Scanner;
    import java.util.TreeMap;
    import java.util.TreeSet;
    public class halibote 
    { 
        private static String str="";
        private static Scanner sc=new Scanner(System.in);
        private static BufferedReader br=null;
        private static String a[]=new String[90000000]; 
        private static String c[]=new String[90000000]; 
        private static int b[]=new int[9000000]; 
        private static int l=0;
        private static int l1=0;
        private static int n=0;
        private static int j=0;
    public static void cunfang() throws IOException
    {//将单词存到数组a
        while(str!=null) 
       {
          int i=0;
          str=str.toLowerCase(); //把大写改成小写
          for(i=0;i<str.length();i++) 
          {
           if((str.charAt(i)>96&&str.charAt(i)<123)) 
           { 
            a[j]=a[j]+str.charAt(i); 
           }
            if(str.charAt(i)==' '||str.charAt(i)==','||str.charAt(i)=='.')
            {
              if(!a[j].equals(""))
              {
                 j=j+1;
                 a[j]="";
              }
            }
           } 
                 str=br.readLine();
         }
               l=j;            
    }
    public static void show() //显示
    {
        for(int k=0;k<l1;k++) 
        {
         System.out.println(c[k]+"	"+b[k]);
        }
    }
    public static void paixu() //排序
    {
       int t3=0;
       int t2=0;
       String sr="";
       for(int i=0;i<l-1;i++)
         {
            t3=i;
            for(int j=i+1;j<l1;j++) 
            {
              if(b[t3]<b[j])
              {
                t3=j;
              }
            }
        if(t3!=i) 
        {
          t2=b[i];
          b[i]=b[t3];
          b[t3]=t2;
          sr=c[i];
          c[i]=c[t3];
          c[t3]=sr;
         }
        }
    }
    public static void quchong()
    {//去重
         for(int k=0;k<l;k++) 
         {
          b[k]=0;
         }
       c[0]=a[0];
       int tt=1;
       Boolean rt=true;
    
       for(int i=1;i<l;i++)
       {
        rt=false;
        for(int j=0;j<tt;j++)
        {
          if(a[i].equals(c[j])) 
          {
            rt=true;
            break;
          }
         }
          if(!rt) 
          {
           c[tt]=a[i];
           tt++;
          }
         }
         l1=tt;
         for(int i=0;i<l1;i++) 
         {
          for(int j=0;j<l;j++)
          {
            if(c[i].equals(a[j])) 
            {
             b[i]++;
            }
           }
          }
    }
    public static void Readfile() 
    {
        File file=new File("D:\大二java\halibote\Harry Potter and the Sorcerer's Stone.txt"); 
        try 
        {
          InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8");//InputStreamReader实现从字节流到字符流的转换
          br=new BufferedReader(read);
          str=br.readLine();
          cunfang();
          br.close();
          read.close();
         }
        catch(IOException e) 
        {
          System.out.println("读取失败!");
          e.printStackTrace();
        }
        System.out.print(123);
    }
    
    public static void Writefile() throws IOException
    {
         File file=new File("D:\大二java\halibote\Harry Potter and the Sorcerer's Stone.txt");
           if(!file.exists())
           file.createNewFile();
           FileWriter write = new FileWriter(file,true);
          BufferedWriter out=new BufferedWriter(write);
          for(int i=0;i<n;i++)
          {
          StringBuffer sb=new StringBuffer();
          out.write("这是第"+(i+1)+"个: ");
          out.write(c[i]+"	"+b[i]);
          out.write("
    ");
          } 
         
          out.close();
    }
    
    public  static void zimu() throws IOException
    {
    
        BufferedReader br=new BufferedReader(new FileReader("D:\大二java\halibote\Harry Potter and the Sorcerer's Stone.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));
        }
        
        System.out.println(sum);
    
    }
    
    public static void main(String[] args) throws IOException 
    {    
         zimu();
         System.out.println("请输入您要统计前几个最常出现的单词:");
         n=sc.nextInt();
         a[0]="";
         Readfile(); 
         quchong();
        paixu();
         show(); 
         Writefile();
    
    } 
    
    } 

     将.txt文档放进目录中,进行递归查找;先查找目录是否存在,将.txt文档存放进数组中;依次递归调用,输出相同单词的个数;

    package Ioliu;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    
    import Ioliu.*;
    
    public class wenjianjia 
    {
        public static List<String> getAllFilePaths( String filePath, List<String> filePathList ) {
            File[] files = new File( filePath ).listFiles();
            if ( files == null ) {
                return filePathList;
            }
            for ( File file : files ) {
                if ( file.isDirectory() ) {
                    filePathList.add( file.getPath() + "注:此处为文件夹" );
                    getAllFilePaths( file.getAbsolutePath(), filePathList );
                } else {
                    filePathList.add( file.getPath() );
                }
            }
            return filePathList;
        }
    
    
        public static void main(String[] args) throws IOException 
        {    
         
            piao p=new piao();
             File file=null;
             List<String> filePaths = new ArrayList<>();
             filePaths = getAllFilePaths( "D:\\大二java\\halibote", filePaths );
             for ( String path : filePaths ) {
                  System.out.println( path );
                  file=new File(path);
                  p.Readfile(file);
                  p.quchong();
                  p.paixu();
                  p.show();
                  
             }
    
        }
    }

    这部分主要是部分函数的功能不知道;还有对数组的掌握并不是很清楚;很容易混乱,学习的内容不完善,希望可以多敲代码来完善自己的知识;

  • 相关阅读:
    父容器直接清除浮动的css
    [原创 js]指定DOM中添加字符串函数
    跨浏览器的透明度解决方案(包括IE8 Beta 2)
    [原创 js]验证表单强度的js
    帧率、码流与分辩率之间关系
    隔行扫描和逐行扫描(interlace and progressive )
    VGA管脚定义
    红眼消除
    HDMI_VGA_CBVS同时显示
    分辨率与行场同步信号的关系
  • 原文地址:https://www.cnblogs.com/1234yyf/p/11808812.html
Copyright © 2011-2022 走看看