C#源代码计算器实现

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


namespace 源码计算器
{
    class Program
    {
        static bool isAnnotationed = false;           // 判断是否启动多行注释的行数计数
        static int lines = 0;                                   //  代码行
         static int useless = 0;                              //  总行数
        static StreamReader content = null;




        static void Main(string[] args)
        {
            Console.WriteLine("请输入要检查的源代码文件:");
            String path = Console.ReadLine();
            try
            {
                FileStream openFile = new FileStream(@"path", FileMode.Open, FileAccess.Read);        //  打开文件并设置读取权限
                content = new StreamReader(openFile, System.Text.Encoding.ASCII);   //  一种特定的方法从字节流中读取字符
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("未知文件");
                Console.ReadLine();

            }


            String str = content.ReadLine(); //   读取一行

            useless++;


            while (str != null)              // 读取的行数不为空
            {
                if (isEmptyLine1(str) || isAnnotationed)                        //  判断是否为空行,或者处于多行注释计数中
                {
                    //  isAnnotationMulti(str);              // 判断是否终止多行注释计数
                    isStopMultiCount(str);          // 终止多行注释计数
                    str = content.ReadLine();
                    useless++;
                    continue;

                }


                if (isAnnotationSimple(str) || isAnnotationed)                     // 判断是否为单行注释,或者处于多行注释计数中
                {
                    isStopMultiCount(str);// 终止多行注释计数
                    str = content.ReadLine();
                    useless++;
                    continue;

                }


                if (isAnnotationMulti(str))                            //  判断是否为多行注释,多行注释开启,与结束
                {
                    str = content.ReadLine();
                    useless++;
                    continue;

                }


                lines++;                                        //  既不是空行也不是注释行,则代码行数自增1
                str = content.ReadLine();
                useless++;
                continue;

            }


            if (isAnnotationed == true)
            {
                Console.WriteLine("注释没有闭合");
                isAnnotationed = false;
                Console.ReadLine();

            }


            Console.WriteLine("代码的行数:{0}", lines);
            Console.ReadLine();

        }



        /// <summary>
        /// 多行注释判断 ,有四种情况 ,  多行注释计数启动器 isAnnotationed(bool值)判断是否为多行注释
        /// </summary>
        /// <param name="tmp"></param>

        /// <returns></returns>


        public static bool isAnnotationMulti(string tmp)
        {
            string str = tmp;
            int n = str.IndexOf("/*");
            int m = str.IndexOf("*/");


            if (n < 0 && m < 0)
            {                        //  如果 语句中既没有/*也没有*/, 则不是注释行
                return false;
            }
            if (n > 0 && m > 0)            //  语句中既有/*也有*/
            {

                string subStr = str.Substring(0, n);


                if (!isAllSpace(subStr))                         //  /**/注释  规范的写法是:  /*前面为空,若不为空代码会杂乱无章
                {
                 //   Console.WriteLine("可能存在代码书写不规范情况");              // 若*/在/*前面,也是书写不规范的表现
         
                    return false;

                }


                if (isAllSpace(subStr) && m > n)        //  注释大小为一行
                {
                    return true;    //  是注释行
                }
            }
            isStopMultiCount(str);// 终止多行注释计数
           


            if (m < 0 || n > 0)           // 若只有/* 没有*/  ,则开启多行注释计数
            {
                isAnnotationed = true;


                return true;
            }
            return true;

        }   




  // 判断是否为多行注释

        public static bool isAnnotationSimple(string tmp)
        {
            string str = tmp;
            int n = str.IndexOf("//");
            if (n < 0)
            {
                return false;
            }
            string subStr = str.Substring(0, n);
            if (isAllSpace(subStr))
            {
                return true;    //  是注释行
            }
            return false;         // 不是注释行

        }    



 // 判断是否为//单行注释行

        public static bool isEmptyLine1(string tmp)
        {
            string str = tmp;
            if (str == "")
            {
                return true;
            }
            if (isAllSpace(str))
            {
                return true;
            }
            return false;
        }          //  判断是否为空行
        public static bool isAllSpace(string tmp)
        {
            string str = tmp;
            for (int i = 0; i < str.Length; i++)
            {
                if ((int)str[i] != 32)
                {
                    return false;
                }
            }
            return true;

        }    



         //   判断行的内容是否为空格

        public static bool isStopMultiCount(string tmp)
        {
            string str = tmp;
            int n = str.IndexOf("/*");
            int m = str.IndexOf("*/");
            if (m >= 0 && n < 0)
            {      //  若只有*/并且之前存在/* , 则结束多行注释计数。 
                if (isAnnotationed)
                {
                    isAnnotationed = false;
                    return true;
                }
                else
                {
                    Console.WriteLine("可能存在代码书写不规范情况");              // 若*/在/*前面,也是书写不规范的表现
                    return false;
                }
            }
            return false;
        }           // 终止多行注释计数
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值