使用C#sortedList类(可自动排序类)统计文件中单词频率

本文介绍了一个使用 C# 编写的简单程序,该程序能够读取文本文件中的单词,并利用 sortedList 类对这些单词出现的频率进行统计和排序。通过分割文件中的每一行并检查每个单词是否已经存在于 sortedList 中来实现这一目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * sortedList类可以对存进去的对象进行默认的排序,即其内的元素是按顺序排放的。
 * 该测试程序,对文件中的单词进行简单的统计然后排序输出。
 */
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;

namespace WorldCount
{
    class Program
    {
        static void Main( string[] args )
        {
           
                try
                {
                    StreamReader sr = new StreamReader ( "word.txt" ); //创建输入流
                    SortedList sortedlist = new SortedList ( );//创建sortelist对象,该对象可以自动排序
                    char[] delimiter = new char[] { '~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '=', ',', ' ', '>', '<', '?', '/', '\\' };
                    string line;
                    int count = 0;
                    line = sr.ReadLine ( );  
                    while ( line != null )
                    {
                        count++;
                        string[] temp = line.Split ( delimiter );
                        for ( int i = 0; i < temp.Length; i++ )
                        {
                            if ( !sortedlist.ContainsKey ( temp[i] ) )   //检测该list中是否有该字符
                            {
                                sortedlist.Add ( temp[i], 1 ); //如果数组中没有该单词加进去
                            }
                            else
                            {
                                int index = sortedlist.IndexOfKey ( temp[i] ); //取得指定键的索引  //如果有则更改单词的次数
                                
                                int value = (int)sortedlist.GetByIndex ( index );//取得指定索引的值
                                value++;
                                sortedlist.SetByIndex ( index, value );
                            }
                        }
                        line = sr.ReadLine ( );
                    }
                    sr.Close ( );
                    Console.Write(count);
                    StreamWriter sw = new StreamWriter ( "result.txt" );  //打开输出流
                    for ( int i = 0; i < sortedlist.Count; i++ )
                    {
                        string temp = (string)sortedlist.GetKey ( i );
                        if ( temp == null || temp.Equals ( "" ) )
                            continue;
                        else
                        {
                            sw.WriteLine ( "{0} {1} ", sortedlist.GetKey ( i ), sortedlist.GetByIndex ( i ) );
                        }
                    }
                    sw.Close ( );
                }
                catch ( Exception e )
                {
                    Console.WriteLine ( "Can not read!" );
                    Console.WriteLine ( e.Message );
                }
                Console.ReadKey ( );
        }
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值