软件工程第二次作业

      这一周是我上软件工程课的第二周,老师在课堂上留了作业。首先得用C#或者C++去做,对于我来说,这两个语言真的都比较陌生,本科的时候学过c++,但是作为本科通信工程专业的我并没有把这门课程吃透。一直以来对于我来说,动手编程序成了遥不可及的事情。记得老师说过“不会怎么办?”“不会就学啊。”所以面对作业我再也没有选择的余地,听说C#比较好上手,所以就选择C#。显而易见,四天左右的时间精通一门语言用它编出程序是有一点难的。但是我依然从基础知识抓起,看了一些C#的入门教程,并且去图书馆翻阅了C#的基础书籍。懂得了一些基本的语法规则之后,根据老师的作业要求,去网络上搜索了以前前人的代码,并且通过他的注释和逐句搜索,花了很久的时间,终于摸清了一些思路。最后通过自己逐行敲代码,一开始编译出很多错误,自己动手查找问题所在。并且向身边的同学请教了很多次。终于实现了功能。这次作业对我的意义就是我懂得了动手的重要性,并且了解了很多的关于C#的知识。

第一部分,以下就是我的代码以及实现的功能。

功能一:统计一篇小文件中的词汇量

1.打开一个控制文件并且读取里面的东西。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string fn = @"C:\test.txt";
string all = File.ReadAllText(fn, Encoding.UTF8);
string theFileName;
Console.Write(">type ");
theFileName=Console.ReadLine();
//string fn = null;
//Console.Write(">type ");
//thePath = Console.ReadLine();
//StreamReader content = File.OpenText(fn);

 2.分隔字符串后产生的字符串数组,以便于识别单词,对文本单词进行统计,只记录单词,不记录符号。

StreamReader reader = new StreamReader(fn);
            string sline=reader.ReadLine();
            Console.WriteLine(sline.ToString());
            sline = sline.ToLower();//全部变为小写字母
            char[] c = { ' ', ',', '.', '?', '!', ':', ';', '\'', '\"' };
            string[] S = sline.Split(c);
            //Console.WriteLine();
            //Console.WriteLine(">wf -s test.txt");
            // Console.WriteLine("total " + S.Length);

3.建立哈希表,存取哈希表的值并且进行排序,最后输出结果。

 Hashtable ha = new Hashtable();
             for (int i = 0; i < S.Length; i++)
             {
                  if (ha.ContainsKey(S[i]))
                  {
                       ha[S[i]] = (int)ha[S[i]] + 1;
                   }
                   else
                    {
                        ha.Add(S[i], 1);
                     }
              }
                //int sum = 0;
                string[] arrKey = new string[ha.Count];//存哈希表的键
                int[] arrValue = new int[ha.Count];//存哈希表的值
                ha.Keys.CopyTo(arrKey, 0);
                ha.Values.CopyTo(arrValue, 0);
                Console.WriteLine();
                Console.WriteLine(">wf -s test.txt");
                Console.WriteLine("total " + ha.Count);
                Console.WriteLine();
                Array.Sort(arrValue,arrKey);//按哈希表的值进行排序
                //遍历哈希表
                //foreach (DictionaryEntry de in ha)
                   //{
                      //输出
                      //Console.WriteLine(de.Key + ":" + de.Value);
                      //Console.Write(ha.Count);
 
                  //}
                for (int i = arrKey.Length - 1; i >= 0; i--)
                {
                    if ((string)arrKey[i] != "")
                    {
                        Console.Write(arrKey[i].ToString() + ":");
                        Console.WriteLine(arrValue[i].ToString());
                    }
                }
 
        }
    }
}

 

功能二:从控制台输入文件的名字,然后统计单词的数目。指定文件目录进行遍历,分别对每一个文件中的单词进行统计,输入前十个出现频次最高的单词及相应的次数。控制台显示数据。

1.打开一个控制文件并且读取里面的东西。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string fn = @"C:\Users\administrator\desktop\test.txt";
            string all = File.ReadAllText(fn, Encoding.UTF8);
            string theFileName;
            Console.Write(">type ");
            theFileName = Console.ReadLine();
            //string fn = null;
            //Console.Write(">type ");
            //thePath = Console.ReadLine();
            //StreamReader content = File.OpenText(fn);

2.分隔字符串后产生的字符串数组,以便于识别单词,对文本单词进行统计,只记录单词,不记录符号。

StreamReader reader = new StreamReader(fn);
            string sline = reader.ReadToEnd();
            Console.WriteLine(sline.ToString());
            sline = sline.ToLower();//全部变为小写字母
            char[] c = { ' ', ',', '.', '?', '!', ':', ';', '\'', '\"' };
            //分隔字符串后产生的字符串数组
            string[] S = sline.Split(c);
            //Console.WriteLine();
            //Console.WriteLine(">wf -s test.txt");
            // Console.WriteLine("total " + S.Length + "words");

3,建立哈希表,仅仅是在上面程序的基础上做了一定的改动。打印输出前十个频率最高的单词。

 Hashtable ha = new Hashtable();
            for (int i = 0; i < S.Length; i++)
            {
                //判断文本是否进入
                if (ha.ContainsKey(S[i]))
                {
                    ha[S[i]] = (int)ha[S[i]] + 1;
                }
                else
                {
                    ha.Add(S[i], 1);
                }
            }
            //int sum = 0;
            string[] arrKey = new string[ha.Count];//存哈希表的键
            int[] arrValue = new int[ha.Count];//存哈希表的值
            ha.Keys.CopyTo(arrKey, 0);
            ha.Values.CopyTo(arrValue, 0);
            Console.WriteLine();
            Console.WriteLine(">wf -s test.txt");
            Console.WriteLine("total " + ha.Count);
            Console.WriteLine();
            Array.Sort(arrValue, arrKey);//按哈希表的值进行排序
            //遍历哈希表
            //foreach (DictionaryEntry de in ha)
            //{
            //输出
            //Console.WriteLine(de.Key + ":" + de.Value);
            // Console.Write(ha.Count);
            // }
            int n = 0;
            for (int i = arrKey.Length - 1; i >= 0; i--)
            {
                if ((string)arrKey[i] != "")
                {
                    if (n < 10)
                   {
                    Console.Write(arrKey[i].ToString().PadRight(10, ' '));
                    Console.WriteLine(arrValue[i]);
                    n++;
                    }
                }

            }
        }
    }
}

 


 
 
最后git地址:https://coding.net/u/tutu123/p/tutu1234/git
        
词频统计PSP

 

 分析:在学习C#时本以为3小时足够了,可是真正的去了解其中一些知识的时候,因为本人基础知识薄弱发现3小时远远不够,所以导致比预估的时间长了。编写功能一时精力十分集中,所以耗时比较短,因为之前查阅的资料比较丰富。编写功能二的时候遇到了一些瓶颈,所以远远超出了预估的时间范围。










 

posted on 2017-09-18 22:07  图子会变成好的程序员  阅读(412)  评论(0编辑  收藏  举报

导航