C#控制台小程序 700行 查看及十进制转化的8421,5421,2421,余三码,余三循环码

来源:互联网 发布:越南历史 知乎 编辑:程序博客网 时间:2024/05/17 04:36
镇场诗:慈心积善融学习,技术誓为大家学。善心速造多好事,前人栽树后乘凉。我今于此写经验,愿见文者得启发。
——————————————————————————————————————————————————————————




program.cs

using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication8{    class Program    {        static void Main(string[] args)        {            //时间 2016-09-06 23:57            //检验函数输出是否正确            //LookUphCode.Output8421BCD();            //LookUphCode.Output5421BCD();            //LookUphCode.Output2421BCD();            //LookUphCode.OutputExcessThreeCode();            //LookUphCode.OutputThreeCyclicCodes();            //LookUphCode.LookListIntorduction();            //LookUpCode.LookBCDListHello();            //LookUpCode.DecimalToBCDIntroduction();            // LookUpCode.UserContents();            //string UserContentsArray = Console.ReadLine();            //char[] chs = UserContentsArray.ToCharArray();            LookUpCode.WelcomeToUse();            Console.ReadKey();        }    }}


10ToBCD.cs

using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace ConsoleApplication8{    /// <summary>    /// 这个分部类,用户输入一个或者一串十进制数字,翻译成各种码    /// </summary>    public static partial class LookUpCode    {        private static void DecimalToBCDHello()        {            DecimalToBCDIntroduction();            UserContentsOf10();            UserContentsOfCode();            PrepareOutput10ToCode();        }        /// <summary>        /// 向用户展示程序运行的流程        /// </summary>        private static void DecimalToBCDIntroduction()        {            Console.WriteLine("在这个模块中您可以输入单个或多个十进制数字,\n并选择查看它所对应的8421BCD,5421BCD,2421BCD,余三码,余三循环码中的一个或者多个");            Console.WriteLine();            Console.WriteLine("接下来举个例子:");            Console.WriteLine("输入yes开始自动演示,输入no跳过演示");            bool permission = false;            while (true)            {                Console.ForegroundColor = ConsoleColor.Green;                Console.Write("your turn:");                Console.ResetColor();                string choice = Console.ReadLine();                if(choice.ToLower()=="yes")                {                    permission = true;                    break;                }                else if(choice.ToLower()=="no")                {                    break;                }                else                {                    Console.WriteLine("只能输入yes或者no,请从新输入");                }            }            if(permission)            {                #region 自动演示                Console.Clear();                Thread.Sleep(2000);                Console.WriteLine("请输入您想查看的十进制数:");                Console.Write("your turn:");                Thread.Sleep(2000);//暂停两百毫秒                Console.Write("1");                Thread.Sleep(2000);                Console.Write("2");                Thread.Sleep(2000);                Console.WriteLine();                Console.WriteLine("请输入您想查看的码值所对应的数字,用空格隔开每个数字");                Console.WriteLine("查看 类型码--需要输入的数字,");                Console.WriteLine("8421BCD------1");                Console.WriteLine("5421BCD------2");                Console.WriteLine("2421BCD------3");                Console.WriteLine("余三码-------4");                Console.WriteLine("余三循环码---5");                Console.WriteLine();                Console.Write("your turn:");                Thread.Sleep(2000);                Console.Write("1");                Thread.Sleep(1000);                Console.Write(" ");                Thread.Sleep(1000);                Console.Write("2");                Console.WriteLine();                Thread.Sleep(2000);                Console.WriteLine();                Console.WriteLine("8421BCD");                Console.WriteLine("1      2");                Console.WriteLine("0001   0010");                Console.WriteLine("5421BCD");                Console.WriteLine("1      2");                Console.WriteLine("0001   0010");                Console.WriteLine();                Thread.Sleep(2000);                Console.WriteLine("演示结束");                Console.WriteLine("按任意键转到查询程序");                Console.ReadKey();                Console.Clear();                #endregion            }                    }        /// <summary>        /// 用于判断用户是否一次就输入正确        /// </summary>        private static int countOfInput=0;        /// <summary>        /// 存储用户正确输入的内容        /// </summary>        private static char[] UserContentsArray;        /// <summary>        /// 接受用户输入的十进制数并检测保证字符串内不包括0-9以外的字符        /// </summary>        private static void UserContentsOf10()        {            while (true)            {                if(countOfInput==0)                {                    Console.WriteLine("请输入您想查看的十进制数:");                }                else                {                    Console.WriteLine();                    Console.WriteLine("请重新输入输入您想查看的十进制数:");                }                                Console.ForegroundColor = ConsoleColor.Green;                Console.WriteLine("your turn:");                Console.ResetColor();                string contents = Console.ReadLine();                if (contents == "")//如果用户直接敲击回车,啥都不输入                {                    countOfInput = 1;                    Console.WriteLine("直接敲回车的话,人家不能帮你完成任务的");                    Console.WriteLine("输入一些我能够处理的数据吧");                }                else                {                    #region 用户输入不为空                    //这样做除去了字符串中的tab与空格,因为我认为有的人习惯输入四个数字 空一格                    string[] res = contents.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);                    string newContents = string.Join("", res);                    //可能这里使用stringbuilder更好些吧                    //把字符串分解成字符数组,进行检测                    UserContentsArray = newContents.ToCharArray();                    bool pass = true;//我们相信每个用户都会好好输入的                    foreach (var item in UserContentsArray)                    {                        if (!(item >= '0' && item <= '9'))                        {                            pass = false;                            break;                        }                    }                    if (pass)                    {                        Console.WriteLine("用户输入正确");                        break;                    }                    else                    {                        Console.WriteLine("只能输入一个或者多个0-9数字");                        countOfInput = 1;                    }                    #endregion                }            }        }               /// <summary>        /// 用于存储用户正确输入的1-5数字        /// </summary>        private static string[] ChoiceOfCode;        /// <summary>        /// 接受用户输入的数字,并保证字符串内不包括1-5以外的数字        /// </summary>        private static void UserContentsOfCode()        {            Console.WriteLine("请输入您想查看的码值所对应的数字,用空格隔开每个数字");            Console.WriteLine("查看 类型码--需要输入的数字,");            Console.WriteLine("8421BCD------1");            Console.WriteLine("5421BCD------2");            Console.WriteLine("2421BCD------3");            Console.WriteLine("余三码-------4");            Console.WriteLine("余三循环码---5");            Console.WriteLine();            YourTurn();            while (true)            {                //接受用户的输入                string UserChoices = Console.ReadLine();                //如果用户的输入跳出了这个while循环,那么证明用户的输入中存在 1 2 3 4 5                while (true)                {                    if (!(UserChoices.Contains("1") || UserChoices.Contains("2") || UserChoices.Contains("3") || UserChoices.Contains("4") || UserChoices.Contains("5")))                    {                        Console.WriteLine("需要您输入想看的对应码值的对应数字序号");                        UserChoices = Console.ReadLine();                    }                    else                    {                        break;                    }                }                //将用户的输入分离,并除去字符串中的空。                //看到下面的那句,new char[] { ' ' },必须有。                ChoiceOfCode = UserChoices.Split(new char[] { ' ','\t' }, StringSplitOptions.RemoveEmptyEntries);                //这样处理的话,用户的输入会被分解成多个单一的选择                bool noError = true;                //检测是否有非法值,如果有那么提示用户重新输入,直到输入的值为合法值为止                for (int i = 0; i < ChoiceOfCode.Length; i++)                {                    //用户的输入是否包含非法值                    switch (ChoiceOfCode[i])                    {                        case "1":                        case "2":                        case "3":                        case "4":                        case "5":                            break;                        default:                            noError = false;//用户没有按照要求输入                            break;                    }                    if (!noError)                    {                        Console.WriteLine("您的输入出现非法值,请输入1-5中的,再次输入您的选择");                        break;                    }                }                if (noError)//如果用户的输入合法,那么就跳出这个验证输入的for循环                {                    Console.WriteLine("输入正确");                    break;                }            }        }            /// <summary>        /// 输出your turn        /// </summary>        private static void YourTurn()        {            Console.ForegroundColor = ConsoleColor.Green;            Console.WriteLine("your turn:");            Console.ResetColor();        }        /// <summary>        /// 检测需要输出哪几个Code,并输出        /// </summary>        private static void PrepareOutput10ToCode()        {            if(ChoiceOfCode.Contains("1"))            {                Console.WriteLine( );                Console.WriteLine();                Console.WriteLine("8421");                Output10ToCode(LookUp8421BCD);            }            if (ChoiceOfCode.Contains("2"))            {                Console.WriteLine();                Console.WriteLine();                Console.WriteLine("5421");                Output10ToCode(LookUp5421BCD);            }            if (ChoiceOfCode.Contains("3"))            {                Console.WriteLine();                Console.WriteLine();                Console.WriteLine("2421");                Output10ToCode(LookUp2421BCD);            }            if (ChoiceOfCode.Contains("4"))            {                Console.WriteLine();                Console.WriteLine();                Console.WriteLine("余三码");                Output10ToCode(LookUpExcessThreeCode);            }            if (ChoiceOfCode.Contains("5"))            {                Console.WriteLine();                Console.WriteLine();                Console.WriteLine("余三循环码");                Output10ToCode(LookUpThreeCyclicCodes);            }        }        /// <summary>        /// 输出 对应的码值        /// </summary>        private static void Output10ToCode(Hashtable LookUp8421BCD)        {            //为了实现每行输出4个,而且不超出数组的范围。。。            int n1 = UserContentsArray.Length / 4;            int n2 = UserContentsArray.Length % 4;            int i = 0;//记录十进制的输出            int k = 0;//记录码值的输出            string jianGe1 = "       ";//存储十进制数之间的空格            string jianGe2 = "   ";//存储码值之间的空格            for (int j = 0; j < n1; j++)            {                Console.WriteLine(UserContentsArray[i++] + jianGe1 + UserContentsArray[i++] + jianGe1 + UserContentsArray[i++] + jianGe1 + UserContentsArray[i++] + jianGe1);                Console.WriteLine(LookUp8421BCD[UserContentsArray[k++]]+jianGe2+ LookUp8421BCD[UserContentsArray[k++]] + jianGe2 + LookUp8421BCD[UserContentsArray[k++]] + jianGe2 + LookUp8421BCD[UserContentsArray[k++]] + jianGe2);                            }            for (int j = 0; j < n2; j++)            {                Console.Write(UserContentsArray[i++] + jianGe1);                            }            Console.WriteLine();            for (int j = 0; j < n2; j++)            {                Console.Write(LookUp8421BCD[UserContentsArray[k++]] + jianGe2);            }        }        //这个函数的参数是为了方便,才起重名的。用了局部变量不写this.的时候有屏蔽字段的作用    }}


LookBCDList.cs

using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication8{    //静态类,工具类!    //分部类,    //这一部分就是用来,查看从零到九 BCD码列表的        //一个缺陷就是 只能查询一遍     public  static partial class LookUpCode    {        //第一次忘记写new hashtable();导致抛异常        #region 创建 8421,5421,2421,余三,余三循环hashtable        //新建一个 以十进制为键,以8421BCD为值的Hashtable        static private Hashtable LookUp8421BCD=new Hashtable();        //新建一个 以十进制为键,以5421BCD为值的Hashtable        static private Hashtable LookUp5421BCD= new Hashtable();        //新建一个 以十进制为键,以2421BCD为值的Hashtable        static private Hashtable LookUp2421BCD=new Hashtable();        //新建一个 以十进制为键,以余三码为值的Hashtable        static private Hashtable LookUpExcessThreeCode=new Hashtable();        //新建一个 以十进制为键,以余三循环码为值的Hashtable        static private Hashtable LookUpThreeCyclicCodes=new Hashtable();        #endregion        public static void WelcomeToUse()        {            Console.WriteLine("1、这个小工具可以查看0-9所对应的8421BCD,5421BCD,2421BCD,余三码,余三循环码");            Console.WriteLine("2、这个小工具可以将任意长度的十进制数翻译成五种码中的一种或者多种");            bool b = false;            while (true)            {                string c = Console.ReadLine();                switch (c)                {                    case "1": b = true; Console.Clear(); LookUpCode.LookBCDListHello(); break;                    case "2": b = true; Console.Clear(); LookUpCode.DecimalToBCDHello(); break;                    default: Console.WriteLine("只能输入1或者2哦,请您从新输入"); break;                }                if (b)                {                    break;                }                else                {                    Console.Clear();                    Console.WriteLine("1、这个小工具可以查看0-9所对应的8421BCD,5421BCD,2421BCD,余三码,余三循环码");                    Console.WriteLine("2、这个小工具可以将任意长度的十进制数翻译成五种码中的一种或者多种");                }            }        }        //这个枚举应该很有用的,但是我不知道该如何用它        private enum Code:int        {            BCD8421,            BCD5421,            BCD2421,            ExcessThree,            ThreeCyclic        }        //用户想看哪几个列表        private static string[] Choice;        //外界只能访问到这一个函数,撤销外界对他的访问权限        private static void LookBCDListHello()        {            LookListIntorduction();            LookListUserChoice();            OutputUserChoice();        }        /// <summary>        /// 初始化所有的用于查找的Hashtable,在第一次调用该类成员进行构造        /// </summary>        static LookUpCode()        {            #region 装载8421BCD            LookUp8421BCD.Add('0', "0000");            LookUp8421BCD.Add('1', "0001");            LookUp8421BCD.Add('2', "0010");            LookUp8421BCD.Add('3', "0011");            LookUp8421BCD.Add('4', "0100");            LookUp8421BCD.Add('5', "0101");            LookUp8421BCD.Add('6', "0110");            LookUp8421BCD.Add('7', "0111");            LookUp8421BCD.Add('8', "1000");            LookUp8421BCD.Add('9', "1001");            #endregion            #region 装载5421BCD            LookUp5421BCD.Add('0', "0000");            LookUp5421BCD.Add('1', "0001");            LookUp5421BCD.Add('2', "0010");            LookUp5421BCD.Add('3', "0011");            LookUp5421BCD.Add('4', "0100");            LookUp5421BCD.Add('5', "1000");            LookUp5421BCD.Add('6', "1001");            LookUp5421BCD.Add('7', "1010");            LookUp5421BCD.Add('8', "1011");            LookUp5421BCD.Add('9', "1100");            #endregion            #region 装载2421BCD            LookUp2421BCD.Add('0', "0000");            LookUp2421BCD.Add('1', "0001");            LookUp2421BCD.Add('2', "0010");            LookUp2421BCD.Add('3', "0011");            LookUp2421BCD.Add('4', "0100");            LookUp2421BCD.Add('5', "1011");            LookUp2421BCD.Add('6', "1100");            LookUp2421BCD.Add('7', "1101");            LookUp2421BCD.Add('8', "1110");            LookUp2421BCD.Add('9', "1111");            #endregion            #region 装载余三码            LookUpExcessThreeCode.Add('0', "0011");            LookUpExcessThreeCode.Add('1', "0100");            LookUpExcessThreeCode.Add('2', "0101");            LookUpExcessThreeCode.Add('3', "0110");            LookUpExcessThreeCode.Add('4', "0111");            LookUpExcessThreeCode.Add('5', "1000");            LookUpExcessThreeCode.Add('6', "1001");            LookUpExcessThreeCode.Add('7', "1010");            LookUpExcessThreeCode.Add('8', "1011");            LookUpExcessThreeCode.Add('9', "1100");            #endregion            #region 装载余三循环码            LookUpThreeCyclicCodes.Add('0', "0010");            LookUpThreeCyclicCodes.Add('1', "0110");            LookUpThreeCyclicCodes.Add('2', "0111");            LookUpThreeCyclicCodes.Add('3', "0101");            LookUpThreeCyclicCodes.Add('4', "0100");            LookUpThreeCyclicCodes.Add('5', "1100");            LookUpThreeCyclicCodes.Add('6', "1101");            LookUpThreeCyclicCodes.Add('7', "1111");            LookUpThreeCyclicCodes.Add('8', "1110");            LookUpThreeCyclicCodes.Add('9', "1010");            #endregion        }        /// <summary>        /// 按照十进制顺序输出8421BCD码        /// </summary>        private static void Output8421BCD()        {            Console.WriteLine();            Console.WriteLine("8421BCD码");            OutPut(LookUp8421BCD);        }                /// <summary>        /// 按照十进制顺序输出5421BCD码        /// </summary>        private static void Output5421BCD()        {            Console.WriteLine();            Console.WriteLine("5421BCD码");            OutPut(LookUp5421BCD);        }        /// <summary>        /// 按照十进制顺序输出2421BCD码        /// </summary>        private static void Output2421BCD()        {            Console.WriteLine();            Console.WriteLine("2421BCD码");            OutPut(LookUp2421BCD);        }        /// <summary>        /// 按照十进制顺序输出余三码        /// </summary>        private static void OutputExcessThreeCode()        {            Console.WriteLine();            Console.WriteLine("余三码");            OutPut(LookUpExcessThreeCode);        }        /// <summary>        /// 按照十进制顺序输出余三循环码        /// </summary>        private static void OutputThreeCyclicCodes()        {            Console.WriteLine();            Console.WriteLine("余三循环码");            OutPut(LookUpThreeCyclicCodes);        }        /// <summary>        /// 所有按顺序输出码值的所需要调用的函数,类似于基函数的感觉        /// </summary>        /// <param name="codeType">8421,5421,2421,余三,余三循环中的一种</param>        private static void OutPut(Hashtable codeType)        {                        for (char item = '0'; item <= '9'; item++)            {                Console.WriteLine(item + " " + codeType[item]);            }            Console.WriteLine();        }                /// <summary>        /// 查看各个类型码之前的程序使用文字介绍        /// </summary>        private static void LookListIntorduction()        {            Console.WriteLine("查看 类型码--需要输入的数字,用空格隔开每个数字");            Console.WriteLine("8421BCD------1");            Console.WriteLine("5421BCD------2");            Console.WriteLine("2421BCD------3");            Console.WriteLine("余三码-------4");            Console.WriteLine("余三循环码---5");            Console.WriteLine();            Console.WriteLine("tip:您可以输入单独的数字,例如 1,程序将只展示8421BCD码的列表");            Console.WriteLine("tip:您可以输入多个数字,例如:1 2,程序将同时展示8421BCD与5421BCD码的列表");            Console.WriteLine();            Console.WriteLine("您的选择是(输入数字,以空格键隔开,用回车键结束输入):");        }        /// <summary>        /// 接受用户的输入并检测,直到用户输入合乎规定的值        /// </summary>        private static void LookListUserChoice()        {            while (true)            {                //接受用户的输入                string UserChoices = Console.ReadLine();                //如果用户的输入跳出了这个while循环,那么证明用户的输入中存在 1 2 3 4 5                while (true)                {                    if (!(UserChoices.Contains("1") || UserChoices.Contains("2") || UserChoices.Contains("3") || UserChoices.Contains("4") || UserChoices.Contains("5")))                    {                        Console.WriteLine("需要您输入想看的列表的数字序号");                        UserChoices = Console.ReadLine();                    }                    else                    {                        break;                    }                }                //将用户的输入分离,并除去字符串中的空。                //看到下面的那句,new char[] { ' ' },必须有。                Choice = UserChoices.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);                //这样处理的话,用户的输入会被分解成多个单一的选择                bool noError = true;                //检测是否有非法值,如果有那么提示用户重新输入,直到输入的值为合法值为止                for (int i = 0; i < Choice.Length; i++)                {                    //用户的输入是否包含非法值                    switch (Choice[i])                    {                        case "1":                        case "2":                        case "3":                        case "4":                        case "5":                            break;                        default:                            noError = false;//用户没有按照要求输入                            break;                    }                    if (!noError)                    {                        Console.WriteLine("您的输入出现非法值,请重新阅读程序说明,再次输入您的选择");                        break;                    }                }                if (noError)//如果用户的输入合法,那么就跳出这个验证输入的for循环                {                    break;                }            }        }        //这个接受用户输入并验证是否正确,很复杂        private static void OutputUserChoice()        {            //刚开始用的是if--else if,结果只能输出一个。            if(Choice.Contains("1"))            {                Output8421BCD();            }            if(Choice.Contains("2"))            {                Output5421BCD();            }            if(Choice.Contains("3"))            {                Output2421BCD();            }            if(Choice.Contains("4"))            {                OutputExcessThreeCode();            }            if(Choice.Contains("5"))            {                OutputThreeCyclicCodes();            }        }    }}

程序运行展示:













小程序反思:

要真的想写出来一个优秀的程序就得需求分析啥的,一步一步的走。

平常看书,写几十行的代码,都在积累砖块,认识砖块。

写上千行的代码,就好像盖房子,一块一块砖头,慢慢盖起来。

别看书本那么厚,那么多知识点,但是真到盖房子的时候,不会说你学到的全都用到。
所以,不要死扣一个知识点。

写博客很重要,我这次写代码,忘记了一个功能如何实现的了,我上网找,找到了自己的博文,一看,懂了!
技术博客,为他人,也为自己。

身体我写这个代码,夜里一点多才睡觉,坐在床上写,这腰。。。我会中医养生的知识,所以可以调养。
身体很重要!

写注释,代码多了,自己都会忘记这段代码什么意思。所以,注释特别重要。

分部类,我一个类写了300多行,再往下写的时候,写不下去了,我就把这个类改为分部类。新建一个类文件,继续实现其他的功能。

项目经验,说自己期末考试多少分的都是扯淡,真得做项目,真得写代码。几十行,几百行,几千行,上万行,量变会引发质变的。

变量起名字,得起的自己认识。一个字母的话,给自己找麻烦。

见多识广!c#提供了许多函数,你得知道有啥。别自己这个算法,写半天,人家c#自带就有。多尴尬。不过,写算法锻炼人!

调试!我的代码到了最后整合的时候出了问题,几百行已经不能像从前一样,我用眼睛看就可以看出来,从前学校基本上几十行。。。所以,调试就很重要。

你说没项目可练手?我说,处处是项目,就看你眼睛看得见不,手勤不。

我这些经验,只是几百行代码的水准。希望能给您一些启发。


欢迎批评!


——————————————————————————————————————————————————————————
感恩帮助过我的人。博客的精髓在技术部分,更在镇场一诗。
我是一个新手,代码还有许多不完善的地方,请您看代码的时候多多思考。
C#是一个优秀的语言,VS是一个优秀的编译软件,二者值得学习。如果您有一些不会的知识,咱们可以相互讨论。
如果您认为代码可以有改进的地方,有错误的地方,请留下评论,我会处理的。
注:如果我的博文无意中侵犯了您的权益,请告知。看到您的告知后,我将及时作出处理。
0 0
原创粉丝点击