3云大软件学院C# 实验2 单词计数

  • 格式:pdf
  • 大小:218.83 KB
  • 文档页数:4
4.对于 WordCount 程序主要就是把文本文件一行一行的读入以后保存在一个动态 数组中,然后再对每行文本进行分割保存在一二维数组中,然后用 Hash 表来 保存每个单词和出现的次数,然后把结果写入到一个文件中;
5.对于 C#的基础知识还要进一步去理解和学习,然后多进行程序的编写,这样才 能把知识真正掌握和运用。
//分割句子的分隔符数组 char[] separators = {'','\n','\t','.','\"',';',',','?','!','(',')','<','>','[',']'};
DateTime d1 = DateTime.Now; //读取文本之前的系统时间 StreamReader freader = File.OpenText(file_name); //读取文本文件 StreamWriter fwriter = File.CreateText(@"F:\gnome.diag"); //写入文本文件
(2)用 s 命令显示读文本、统计、写文本的时间,截图如下:
1.通过实验对 C#下面向对象的设计有了初步的认识;
结论 (结 果)
2.对 C#下面向对象编程的基本方法能够熟练应用; 3.对 C#的基础知识,如:命名空间、数据类型、流程控制、数组应用、装箱和拆
箱、异常处理、流的输入与输出等有了深刻的理解和认识;
freader.Close(); //关闭输入流 DateTime d2 = DateTime.Now; //读取完文本时的系统时间
text.TrimToSize(); sentences = new string[text.Count][]; //初始化数组的第一维为文本行数
//把动态数组的元素存入二维数组中 for (int ix = 0; ix < text.Count; ++ix) {
实验内容 (算法、 程序、步 骤和方 法)
词和输出结果的时间)"); string ch = Console.ReadLine();
if(ch.Equals("t")) //显示统计结果 {
foreach (string key in aKeys)
Console.WriteLine("{0} :: {1}", key, words[key]);
4
//格式化存储每个单词和它们出现的次数 foreach (string key in aKeys)
fwriter.WriteLine("{0} :: {1}", key, words[key]);
fwriter.Close(); //关闭输出流 DateTime d4 = DateTime.Now; //输出统计结果完成时的系统时间
1
实验内容 (算法、 程序、步 骤和方 法)
while ((text_line = freader.ReadLine()) != null) //判断是否已读完 {
if (text_line.Length == 0) //如果读到的是空行就不作处理 continue;
text.Add(text_line); //把非空的一行文本添加到动态数组的尾部 }
str = (String)text[ix]; sentences[ix] = str.Split(separators); //分割字符串 }
dim1_length = sentences.GetLength(0); //取得文本的行数 //统计每个单词及其出现的次数 for (int ix = 0; ix < dim1_length; ++ix) {
TimeSpan ts1 = d2 - d1; //读取文本的时间 TimeSpan ts2 = d3 - d2; //统计单词的时间 TimeSpan ts3 = d4 - d3; //输出结果的时间
Console.WriteLine("请选择你所需要的功能:\n(t:显示统计结果;s:显示读取文本、统计单
else words[key] = (int)words[key] + 1; //单词在Hash表中时单词的数量加1
} }
DateTime d3 = DateTime.Now; //统计完单词时的系统时间
2
ArrayList aKeys = new ArrayList(words.Keys); aKeys.Sort(); //对每个单词进行排序
实验报告
序 号: 课程名称: C#与.NET 实验 学 号:
实验老师: 实验名称: 实验 2:WordCount
姓 名:
指标等级 A B C D 功能完整 程序质量 按时检查 提问回答
检查时间
总评成绩
课程名称
C#与.NET 实验
实验项目
实验 2:单词统计 WordCount
实验目的
熟悉并掌握 C#下面向对象设计和编程的基本方法。
Console.WriteLine("输出结果的时间为: " + liseconds + "毫秒");
}
else
Console.WriteLine("你不需要显示任何信息或你的输入有错。");
Console.ReadKey();
}
}
}
3
(1)用 t 命令显示统计结果,截图如下:
数据记录 和计算
}
else if (ch.Equals("s")) //显示各项所用时间
{
Console.WriteLine("读取文本的时间为: " + liseconds + "毫秒");
Console.WriteLine("统计单词的时间为: " + liseconds + "毫秒");
实验内容 (算法、 程、步
骤和方 法)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Collections; namespace WordCount {
class Program {
static void Main(string[] args) {
string file_name = @"F:\Words.txt"; //保存文件路径的字符串 string text_line; //用于保存文本文件的每一行文本 ArrayList text = new ArrayList(); //用动态数组存储整篇文本 string[][] sentences; //用缺口型数组存储整篇文本 string str; Hashtable words = new Hashtable(); //用于存储单词和单词个数的Hash表 int dim1_length;
//处理文本的每一行 foreach (string st in sentences[ix]) {
if (st.Equals("")) //遇到空格时不作处理 continue;
string key = st.ToLower(); //把单词转换为小写
if (!words.Contains(key)) //如果单词不在Hash表中 words.Add(key, 1);