今天的任务是用控制台实现一个简单的计算器,我想了一下,决定用很臃肿的办法做.即每个字符都由我去控制输入,这样做的话就是每次刷新都遍历了一个两层的循环,不过好处是我可以精确的控制每次输出的字符,目前功能就是按键高亮显示,这也算增强用户体验吧,代码如下
class Program
    {
        private void ShowClick(string pushBtn, string whatToShow, string showIt)   //该方法用于高亮所按的数字
        {
            if (pushBtn == whatToShow)
            {
                Console.BackgroundColor = ConsoleColor.DarkGray;
                Console.Write(showIt);
                Console.BackgroundColor = ConsoleColor.Black;
            }
            else
            {
                Console.Write(showIt);
            }
        }
        private float MyMath(string getHowToDo, string keyInfoString, string getNumFirst, string getNumSecond)  //用于加减乘除的算法,返回屏幕数字
        {
            float sum = 0F;
            if (getHowToDo == "+")
            {
                sum = float.Parse(getNumFirst) + float.Parse(getNumSecond);
                PaintCalc(keyInfoString, sum.ToString());
            }
            else if (getHowToDo == "-")
            {
                sum = float.Parse(getNumFirst) - float.Parse(getNumSecond);
                PaintCalc(keyInfoString, sum.ToString());
            }
            else if (getHowToDo == "*")
            {
                sum = float.Parse(getNumFirst) * float.Parse(getNumSecond);
                PaintCalc(keyInfoString, sum.ToString());
            }
            else if (getHowToDo == @"/")
            {
                sum = float.Parse(getNumFirst) / float.Parse(getNumSecond);
                PaintCalc(keyInfoString, sum.ToString());
            }
            return sum;
        }
        static void Main(string[] args)
        {
            //▁▁▁▁▁▁▁▁▁");
            //▏              ▕");
            //▏12345678901234▕");
            //▏              ▕");
            //▏①  ②  ③  . ▕");
            //▏              ▕");
            //▏④  ⑤  ⑥  + ▕");
            //▏              ▕");
            //▏⑦  ⑧  ⑨  - ▕");
            //▏              ▕");
            //▏0   =   /   * ▕");
            //▏              ▕");
            //▔▔▔▔▔▔▔▔▔");
            string getNumFirst = "";       //获取第一个数字
            string getNumSecond = "";      //获取第二个数字
            float getNumPrint = 0;       //获取输入=号后屏幕是的数字
            string getHowToDo = "";         //获取运算符
            int point = 0;                  //获取是否已经存在运算符
            Program d = new Program();
            while (true)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey(true);
                string keyInfoString = keyInfo.KeyChar.ToString();  //keyInfoString获取按键的char类型转化为string类型
                Console.Clear();              //清空屏幕
                string[] numArraya = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "." };
                string[] numArrayb = { "+", "-", "*", @"/" };
                if (numArraya.Contains(keyInfoString) && point == 0)
                {
                    if (!getNumFirst.Contains("."))
                    {
                        getNumFirst = getNumFirst + keyInfoString;
                    }
                    else if (getNumFirst.Contains(".") && keyInfo.KeyChar != '.')
                    {
                        getNumFirst = getNumFirst + keyInfoString;
                    }
                    getNumPrint = float.Parse(getNumFirst);        //获取屏幕数字
                    d.PaintCalc(keyInfoString, getNumFirst);
                }
                else if (numArrayb.Contains(keyInfoString) && getNumFirst != "" && point == 0)
                {
                    point = 1;
                    getHowToDo = keyInfoString;
                    d.PaintCalc(keyInfoString, getNumFirst);
                }
                else if (numArrayb.Contains(keyInfoString) && getNumFirst == "" && point == 0) //当输入过等号时再输入运算符的处理方法
                {
                    point = 1;
                    getNumFirst = getNumPrint.ToString();
                    getHowToDo = keyInfoString;
                    d.PaintCalc(keyInfoString, getNumFirst);
                }
                else if (numArrayb.Contains(keyInfoString) && point == 1) //当连加时,先连加之前的,再改变符号
                {
                    getNumFirst = d.MyMath(getHowToDo, keyInfoString, getNumFirst, getNumSecond).ToString();  //完成累加的功能
                    getNumSecond = "";
                    getHowToDo = keyInfoString;
                    Console.Clear();
                    d.PaintCalc(keyInfoString, getNumFirst);
                }
                else if (numArraya.Contains(keyInfoString) && point == 1)
                {
                    if (!getNumSecond.Contains("."))
                    {
                        getNumSecond = getNumSecond + keyInfoString;
                    }
                    else if (getNumSecond.Contains(".") && keyInfo.KeyChar != '.')
                    {
                        getNumSecond = getNumSecond + keyInfoString;
                    }
                    d.PaintCalc(keyInfoString, getNumSecond);
                }
                else if (keyInfo.KeyChar == '=' && point != 0)
                {
                    getNumPrint = d.MyMath(getHowToDo, keyInfoString, getNumFirst, getNumSecond);
                    point = 0;
                    getNumFirst = "";
                    getNumSecond = "";
                }
            }
        }
        private void PaintCalc(string pushBtn, string NumString)    //计算器绘制方法,获取按键和要显示的数字
        {
            int tempLoc = 0;      //用来定位数字显示时需要分批显示的字符的位置
            for (int i = 0; i < 13; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (i == 0 && j != 8)
                    {
                        Console.Write("▁");
                        continue;
                    }
                    else if (i == 0 && j == 8)
                    {
                        Console.WriteLine("▁");
                        continue;
                    }
                    else if (i == 12 && j != 8)
                    {
                        Console.Write("▁");
                        continue;
                    }
                    else if (i == 12 && j == 8)
                    {
                        Console.WriteLine("▁");
                        continue;
                    }
                    else if (i != 0 && j == 0 && i != 12)
                    {
                        Console.Write("▏");
                        continue;
                    }
                    else if (i != 0 && j == 8 && i != 12)
                    {
                        Console.WriteLine("▕");
                        continue;
                    }
                    else if (i == 1 && j != 0 && j != 8)
                    {
                        Console.Write("  ");
                        continue;
                    }
                    else if (i == 3 && j != 0 && j != 8)
                    {
                        Console.Write("  ");
                        continue;
                    }
                    else if (i == 4 && j == 1)
                    {
                        ShowClick(pushBtn, "1", "①");
                        continue;
                    }
                    else if (i == 4 && j == 3)
                    {
                        ShowClick(pushBtn, "2", "②");
                        continue;
                    }
                    else if (i == 4 && j == 5)
                    {
                        ShowClick(pushBtn, "3", "③");
                        continue;
                    }
                    else if (i == 6 && j == 1)
                    {
                        ShowClick(pushBtn, "4", "④");
                        continue;
                    }
                    else if (i == 6 && j == 3)
                    {
                        ShowClick(pushBtn, "5", "⑤");
                        continue;
                    }
                    else if (i == 6 && j == 5)
                    {
                        ShowClick(pushBtn, "6", "⑥");
                        continue;
                    }
                    else if (i == 8 && j == 1)
                    {
                        ShowClick(pushBtn, "7", "⑦");
                        continue;
                    }
                    else if (i == 8 && j == 3)
                    {
                        ShowClick(pushBtn, "8", "⑧");
                        continue;
                    }
                    else if (i == 8 && j == 5)
                    {
                        ShowClick(pushBtn, "9", "⑨");
                        continue;
                    }
                    else if (i == 4 && j == 7)
                    {
                        ShowClick(pushBtn, ".", ". ");
                        continue;
                    }
                    else if (i == 6 && j == 7)
                    {
                        ShowClick(pushBtn, "+", "+ ");
                        continue;
                    }
                    else if (i == 8 && j == 7)
                    {
                        ShowClick(pushBtn, "-", "- ");
                        continue;
                    }
                    else if (i == 10 && j == 1)
                    {
                        ShowClick(pushBtn, "0", "0 ");
                        continue;
                    }
                    else if (i == 10 && j == 3)
                    {
                        ShowClick(pushBtn, "=", "= ");
                        continue;
                    }
                    else if (i == 10 && j == 5)
                    {
                        ShowClick(pushBtn, @"/", @"/ ");
                        continue;
                    }
                    else if (i == 10 && j == 7)
                    {
                        ShowClick(pushBtn, "*", "* ");
                        continue;
                    }
                    //*******************************************最关键的处理,显示数字的一排
                    else if (i == 2 && j != 0 && j != 8 && j > (14 - NumString.Length) / 2)
                    {
                        if (NumString.Length % 2 == 1)    //当为奇数时,前面加个空格
                        {
                            NumString = " " + NumString;
                            Console.Write(NumString.Substring(tempLoc, 2));
                            tempLoc = tempLoc + 2;
                        }
                        else
                        {
                            Console.Write(NumString.Substring(tempLoc, 2));
                            tempLoc = tempLoc + 2;
                        }
                        continue;
                    }
                    //******************************************
                    else
                    {
                        Console.Write("  ");
                    }
                }
            }
        }
    }