c#第一次作业

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;

namespace Check_3_1
{


    class Program
    {
        //private static int sumLines = 0;
        private static int emptyLines = 0;
        private static int noteLines = 0;
        private static int codeLines = 0;
        static void Main(string[] args)
        {
            // FileStream fs = new FileStream("C:\\Users\\Sam\\Desktop\\1.txt", FileMode.Open);

            calculate();
        }

        public static void calculate()
        {
            string sum;
            string a = null;
            string b = null;
            bool bl = false;
            //用于最后的输出判定
            int n=0;
            string[] AllFile=null;
            Console.WriteLine("输入目录或文件的完整路径:");
            a = Console.ReadLine(); 
            if (Directory.Exists(a))//判定是目录
            {
                AllFile = Directory.GetFiles(@a);
                //  读取目录下的所有文件的文件路径,存放在字符串数组
                n = AllFile.Length;                                    
                
                Console.WriteLine("输入文件类型(例如:.cs):");                                               
                b = Console.ReadLine();
            }
            else //判定是文件
            {
                AllFile = new string[1];
                //实例化
                AllFile[0] = a;
                //由于是文件,所以只有一个路径
                n = 1;
                b = Path.GetExtension(a);
                //把该文件的后缀赋值给b
               
            }

                bool note = false;
                //用于判断注释中的/**/

                
              

                
                for (int j = 0; j < n; j++)
                    //当选项一输入为一个文件而非目录时,只执行一次
                {
                    try
                    {

                        FileInfo fi = new FileInfo(AllFile[j]);
                        if (fi.Extension == b)
                      //  只有后缀和b一样的文件能读取,其他文件省略
                        {
                            bl = true;
                            //判定后缀名符合要求
                            Console.WriteLine("文件路径:" + AllFile[j]);
                            StreamReader sr = File.OpenText(@AllFile[j]);
                          //  定义流文件
                            try
                            {
                               // 下面是空格行,注释行,还有代码行的判定
                                while ((sum = sr.ReadLine()) != null)
                                {
                                    sum = sum.Trim();
                                    //去除字符串首尾的空格,方便判断
                                    if (Regex.IsMatch(sum, "^[\\n]*$"))
                                        //正则表达式含义为只有回车键
                                    {
                                        emptyLines++;
                                    }
                                    else if (sum.StartsWith("/*") && !sum.EndsWith("*/"))
                                        //注释行
                                    {
                                        noteLines++;
                                        note = true;
                                    }
                                    else if (note == true)
                                    {
                                        noteLines++;
                                        if (sum.EndsWith("*/"))
                                        {
                                            note = false;
                                        }
                                    }
                                    else if (sum.StartsWith("//"))
                                    {
                                        noteLines++;
                                    }
                                    else
                                        //除了上述情况都视为代码行
                                    {
                                        codeLines++;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                throw new Exception("抛出异常");
                            }
                            sr.Close();

                        }





                    }
                    catch (Exception e)
                    {
                        throw new Exception("抛出异常");
                    }
                    finally
                    {
                        if (bl)
                            //符合要求的文件则输出其信息
                        {
                            Console.WriteLine("空行数:" + emptyLines);
                            emptyLines = 0;
                            Console.WriteLine("注释行数:" + noteLines);
                            noteLines = 0;
                            Console.WriteLine("代码行数:" + codeLines);
                            codeLines = 0;
                            Console.WriteLine();
                            bl = false;

                        }
                       
                    }

                }
                Console.Read();
            //仅用于编译之后滞留在控制台而非闪退
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值