zoukankan      html  css  js  c++  java
  • 课堂练习之单词统计

     1 package com.wenjian;
     2 import java.util.Scanner;
     3 import java.util.HashMap;
     4 import java.util.Iterator;
     5 import java.util.Set;
     6 import java.io.*;
     7 public class hali {
     8     public static <type> void main (String[] args) throws IOException {
     9         File file=new File("E:\Harry Potter and the Sorcerer's Stone.txt");                  //��ȡ�ļ�
    10         if(!file.exists()){
    11             System.out.println("文件打不开");
    12             return;
    13         }  
    14         Scanner scanner=new Scanner(file);
    15         BufferedReader buf = new BufferedReader(new FileReader(file));
    16         int []num=new int[100];//计数数组
    17         char []zimu=new char[100];//字母表数组
    18         char a='A';char b='a';
    19         for(int i=1;i<=52;i++)
    20         {
    21             if(i<=26)
    22                 zimu[i]=a++;
    23             else
    24                 zimu[i]=b++;
    25         }
    26         //计数
    27         String s=buf.readLine();
    28         while(s!=null) {
    29             String[] lineWords=s.split(" ");          
    30             for(int i=0;i<lineWords.length;i++) {
    31                 for(int j=0;j<lineWords[i].length();j++)
    32                 {
    33                     if(lineWords[i].charAt(j)>='A'&&lineWords[i].charAt(j)<='Z')
    34                         num[lineWords[i].charAt(j)-'A'+1]++;
    35                     else if(lineWords[i].charAt(j)>='a'&&lineWords[i].charAt(j)<='z')
    36                         num[lineWords[i].charAt(j)-'a'+1+24]++;
    37                 }
    38             }
    39             s=buf.readLine();
    40         }
    41         //求总次数
    42         int sum=0;
    43         for(int i=1;i<=52;i++)
    44         {
    45             sum+=num[i];
    46         }
    47         //排序
    48         for(int i=1;i<=52;i++)
    49         {
    50             for(int j=i+1;j<=52;j++)
    51             {
    52                 if(num[i]<num[j])
    53                 {
    54                     int t=num[i];
    55                     num[i]=num[j];
    56                     num[j]=t;
    57                     
    58                     char p=zimu[i];
    59                     zimu[i]=zimu[j];
    60                     zimu[j]=p;
    61                 }
    62             }
    63         }
    64         System.out.println(sum);
    65         for(int i=1;i<=52;i++)
    66         {
    67             double ans=num[i]*1.0/sum*100;
    68             System.out.println(zimu[i]+":"+String.format("%.2f", ans)+"%");
    69         }
    70 }
    71 }
     1 package com.wenjian;
     2 import java.util.Scanner;
     3 import java.util.HashMap;
     4 import java.util.Iterator;
     5 import java.util.Set;
     6 import java.io.*;
     7 public class hali {
     8     public static <type> void main (String[] args) throws IOException {
     9         File file=new File("E:\Harry Potter and the Sorcerer's Stone.txt");                  //��ȡ�ļ�
    10         if(!file.exists()){
    11             System.out.println("文件打不开");
    12             return;
    13         }  
    14         Scanner scanner=new Scanner(file);
    15         BufferedReader buf = new BufferedReader(new FileReader(file));
    16         int []num=new int[100];//计数数组
    17         char []zimu=new char[100];//字母表数组
    18         char a='A';char b='a';
    19         for(int i=1;i<=52;i++)
    20         {
    21             if(i<=26)
    22                 zimu[i]=a++;
    23             else
    24                 zimu[i]=b++;
    25         }
    26         //计数
    27         String s=buf.readLine();
    28         while(s!=null) {
    29             String[] lineWords=s.split(" ");          
    30             for(int i=0;i<lineWords.length;i++) {
    31                 for(int j=0;j<lineWords[i].length();j++)
    32                 {
    33                     if(lineWords[i].charAt(j)>='A'&&lineWords[i].charAt(j)<='Z')
    34                         num[lineWords[i].charAt(j)-'A'+1]++;
    35                     else if(lineWords[i].charAt(j)>='a'&&lineWords[i].charAt(j)<='z')
    36                         num[lineWords[i].charAt(j)-'a'+1+24]++;
    37                 }
    38             }
    39             s=buf.readLine();
    40         }
    41         //求总次数
    42         int sum=0;
    43         for(int i=1;i<=52;i++)
    44         {
    45             sum+=num[i];
    46         }
    47         //排序
    48         for(int i=1;i<=52;i++)
    49         {
    50             for(int j=i+1;j<=52;j++)
    51             {
    52                 if(num[i]<num[j])
    53                 {
    54                     int t=num[i];
    55                     num[i]=num[j];
    56                     num[j]=t;
    57                     
    58                     char p=zimu[i];
    59                     zimu[i]=zimu[j];
    60                     zimu[j]=p;
    61                 }
    62             }
    63         }
    64         System.out.println(sum);
    65         for(int i=1;i<=52;i++)
    66         {
    67             double ans=num[i]*1.0/sum*100;
    68             System.out.println(zimu[i]+":"+String.format("%.2f", ans)+"%");
    69         }
    70 }
    71 }
  • 相关阅读:
    LeetCode-860. Lemonade Change
    LeetCode-455.Assign Cookies
    LeetCode-122.Best Time to Buy and Sell Stock II
    LeetCode-438.Find All Anagrams in a String
    LeetCode-50.Pow(x,n)
    LeetCode-236.Lowest Common Ancestor of a Binary Tree
    LeetCode-235.Lowest Common Ancestor of a Binary Search Tree
    LeetCode-98.Validate Binary Search Tree
    LeetCode-18.4Sum
    LeetCode-15.3Sum
  • 原文地址:https://www.cnblogs.com/w669399221/p/13084332.html
Copyright © 2011-2022 走看看