实现项目WC

软件的需求分析

程序处理用户需求的模式为:

  • wc.exe [parameter][filename]

在[parameter]中,用户通过输入参数与程序交互,需实现的功能如下:

1、基本功能

  • 支持 -c  统计文件字符数
  • 支持 -w 统计文件单词数
  • 支持 -l  统计文件总行数

2、拓展功能

  • 支持 -a 返回高级选项(代码行 空行 注释行)
  • 支持 -s 递归处理符合条件的文件

3、高级功能

  • 支持 -x 程序以图形界面与用户交互

[filename] 是待处理文件名。

从周五到今天一直在弄VS 2013,感觉有的那几个错误我都遇到了,算了算20多个小时了,郁闷的不行。不能单步调试、新建不了项目,卸载修复各种问题,太不容易了。

总算在作业结束之前把基本的功能写出来了,下面分解一下程序。

主程序主要是创建对象,把输入的字符串已空格为间隔拆分成几个字符串,并记录最后一个字符串为文件路径。最后按照输入命令一次返回不同值,执行不同功能。

            Wc c = new Wc();//创建对象
            Console.Write("wc.exe");
            string msg = Console.ReadLine();
            string[] newm = msg.Split(new char[] { ' ' });//去掉空格 拆分成几个字符串
            int n = newm.Length;
            string str = newm[n - 1];//记录文件路径
            foreach (string i in newm)
            {
                if (i == "-c")
                {
                    c.Read(str, 1);//字符数目功能
                }
                else if (i == "-w")
                {
                    c.Read(str, 2);//单词数
                }
                else if (i == "-l")
                {
                    c.Read(str, 3);//行数目
                }

Read函数首先访问传过来的字符串路径,并打开。

        //byte[] byData = new byte[10000];//应该注释掉,一开始用的byte类型,后来发现有点问题
        FileStream file = new FileStream(a, FileMode.Open);
        StreamReader sr = new StreamReader(file);//访问文件并读取
        //file.Read(byData, 0, 10000);
        switch (b) 
        {

switch  结构 统计出各功能数目。

case 1:
                {
                    /*while (byData[i] != '\0')
                    {
                        c++;
                        i++;
                    }*/
                    int Ichar = 0;
                    while ((Ichar = sr.Read()) != -1)    // 不再有可用的字符,则为 -1  
                    {
                        if (Ichar != ' ')
                        {
                            c++;
                        }
                    }
                    Console.WriteLine("字符数为:" + c);
                    file.Close();
                } break;
            case 2:
                {
                    int Ichar = 0; //增加 char[] no = { ' ', '\n', '{', '}', '(', ')', '=', '+', '_', '*', '%' };                   
            while ((Ichar = sr.Read()) != -1)
                    {
                        if (Ichar == ' ' || Ichar == '\n')//这我在想想更准确的判断方法 - -!
                        {                               //  for (int ss = 0; ss < 11; ss++)
                            w++;                        //  {
                        }                              //    if (Ichar ==no[ss])    这个更加准确!
                    }
                    Console.WriteLine("单词数为:" + w);
                    file.Close();
                } break;
            case 3:
                {
                    int Ichar = 0;
                    while ((Ichar = sr.Read()) != -1)    
                    {
                        if (sr.ReadLine() != null)
                        {
                            l++;
                        }
                    }
                    Console.WriteLine("行数为:" + l);
                    file.Close();
                } break;
        }

下面贴上文本与结果。

增加-a 高级功能,首先在主函数添加代码,使程序能跳转到read函数。

 else if(i=="-a")
                {
                    c.Read(str, 5);
                }

在read函数中添加case 5,增加三个行数功能。

int Line_daima = 0;
                    int Line_kong = 0;
                    int Line_zhushi = 0;//初始化三个行数变量
                    string stra; 
                    while ((stra = sr.ReadLine())!=null)//读到末尾停止
                    {
                       
                        if (stra=="")//百度了好多次,总算明白了readline函数对空行和末尾的区别  
                        { 
                            Line_kong++;
                        } 
                        else if((stra.IndexOf('/'))>-1)//判断字符串stra中是否存在’/‘字符
                        {
                            Line_zhushi++;
                        }
                        //int cc = s.IndexOf("//");
                        //string[] newn=str.Split(new char[]{'//'});
                       // if ( > -1)
                       // {
                          //  Line_zhushi++;
                       // }                       //中间这段是错误案例,还没想好怎么能写出来。
                        else
                            Line_daima++;
                    }
                    Console.WriteLine("空行数为:"+Line_kong);
                    Console.WriteLine("注释行数为:"+Line_zhushi);
                    Console.WriteLine("代码行数为:"+Line_daima);
                    file.Close();                 //这个很重要,一定要关闭访问过的文件。
                }break;

 

posted @ 2016-03-21 19:35  suisx  阅读(433)  评论(0编辑  收藏  举报