package zy03;
import java.io.*; 

import java.util.*;

public class ceshi 
{ 
private static String str="";
private static Scanner sc=new Scanner(System.in);
private static BufferedReader br=null;
private static String a[]=new String[1000000]; 
private static String c[]=new String[10000000]; 
private static int b[]=new int[1000000]; 
private static int l=0;
private static int l1=0;
private static int n=0;
private static int j=0;
//private static Boolean false;
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<n;k++) 
    {
     System.out.println(c[k]+"\t"+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//飘.txt"); 
    try 
    {
      InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8");
      br=new BufferedReader(read);
      str=br.readLine();
      cunfang();
      br.close();
      read.close();
     }
    catch(IOException e) 
    {
      System.out.println("读取失败!");
      e.printStackTrace();
    }
}

public static void Writefile() throws IOException
{
     File file=new File("D://大二java//飘.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]+"\t"+b[i]);
      out.write("\r\n");

      } 
      out.close();
}
public static void main(String[] args) throws IOException 
{
     System.out.println("请输入您要统计前几个最常出现的单词:");
     n=sc.nextInt();
     a[0]="";
     Readfile(); 
     quchong();
    paixu();
     show(); 
     Writefile();

} 

} 

 

转载于:https://www.cnblogs.com/1234yyf/p/11600236.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值