zgk666

题目要求:

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

源码:

  

package demo;
import java.io.File; 
import java.io.InputStreamReader; 
import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import java.io.FileWriter; 
 
public class Demo { 
  static String[] save = new String[50000];
   static float[] num1 = new float[26];
   static float[] num2 = new float[26];
   static float[] num3 = new float[52];
   static char zimu[]={\'A\',\'B\',\'C\',\'D\',\'E\',\'F\',\'G\',\'H\',\'I\',\'J\',\'K\',\'L\',\'M\',\'N\',\'O\',\'P\',\'Q\',\'R\',\'S\',\'T\',\'U\',\'V\',\'W\',\'X\',\'Y\',\'Z\',\'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'h\',\'i\',\'j\',\'k\',\'l\',\'m\',\'n\',\'o\',\'p\',\'q\',\'r\',\'s\',\'t\',\'u\',\'v\',\'w\',\'x\',\'y\',\'z\'};
  static int i = 0;
 
 
 
    public static void main(String args[]) { 
     try {
         System.out.println(System.in);
         FileReader fileReader = new FileReader("C:\\Users\\dell\\Documents\\Tencent Files\\2436124704\\FileRecv\\Harry Potter and the Sorcerer\'s Stone.txt");
         BufferedReader buf = new BufferedReader(fileReader);
         String bufToString = "";
         String readLine = "";
         String[] myArray = new String[50000];  //100:这个值你自己定义,但不宜过大,要根据你文件的大小了,或者文件的行数
         while((readLine = buf.readLine()) != null){
             myArray[i] = readLine;
           
             save[i]=myArray[i];
             i++;
           
         }
    }
     catch (Exception e) {
        // e.printStackTrace();
      System.out.println("连不上");
     }
int length;
int j=0;
int t=0;
for(t=0;t<i;++t)
{
 char ss[] = save[t].toCharArray();
 for(j=0;j<ss.length;++j)
 {
  if(ss[j]>=\'A\'&&ss[j]<=\'Z\')
  {int k=(int)ss[j];
   num1[k-65]++;
   
  }
  else if(ss[j]>=\'a\'&&ss[j]<=\'z\')
  {int k=(int)ss[j];
   
   num2[k-97]++;
  }
 }
 
  
  
 
}

/*for(j=0;j<26;++j)
{System.out.println(num2[j]);
   
    }*/
float all=0;
for(j=0;j<26;++j)
{
 all+=num1[j];
 all+=num2[j];
    }
for(j=0;j<26;++j)
{
 num1[j]=num1[j]/all*100;
 num2[j]=num2[j]/all*100;
    }
for(j=0;j<26;++j)
{
 num3[j]=num1[j];
 num3[j+26]=num2[j];
}
for(j=0;j<52;++j)
{float[] max = new float[26];
int[] flag = new int[52];
int p=0;
 for(t=0;t<52;++t)
 {
  
  if(num3[t]>max[0])
   {
   flag[0]=t;
   max[0]=num3[t];
   
   }
  /*else if(num3[t]==max[0])
  {
   p++;
   flag[p]=t;
   max[p]=num3[t];
  }*/
 }
 /*for(int k=0;k<p;++k)
 {
  System.out.println(zimu[flag[p]]+":"+max[p]+"%"); 
 
 
 }*/
 System.out.print(zimu[flag[p]]+":");
 float fff=(float)(Math.round(max[p]*100))/100;
 System.out.println(fff+"%");
 num3[flag[p]]=0;
}
}
}
 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-12-05
  • 2021-11-11
  • 2021-09-23
  • 2022-02-15
  • 2022-12-23
  • 2021-10-12
  • 2021-06-24
猜你喜欢
  • 2021-12-19
  • 2022-12-23
  • 2021-10-03
  • 2021-08-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案