上次看到烟云同学写的计算器实现一个当前字符的高亮效果的计算器,也模仿做了一个(方法不同,人家的更好些)。今天没事点了点这个计算器,突然想到上面的“按钮” 1,2,3,+,=....也就是一个样子,有与没有都不影响计算器的功能。我就想能不能模拟一下wform的计算器计算过程,上面的按钮可以点,但是控制台不支持鼠标动作。我就用方形键来移动“焦点”按Enter来确认。
就如图上显示的我移动到“=”号处按Enter 就表示输入了“=”号就开始计算结果了。
说下我的思路:我这这些字符123456789.+*-/=保存在一个数组里,一维二维都可,为了方便操作我用一维数组checkchar[]来保存。但是看的时候,要把他当做二维数组来看
我这样排着看这16个字符: 1  2  3  . 
                         4  5  6  + 
                         7  8  9  - 
                         0  =  *  /
对应的索引
0   1   2    3
4   5   6    7
8   9   10   11
12  13  14    15
这样容易发现规律。比如0 4 8 12这一排数 mod4都等于0
3 7 11 15加1之后mod 4等于0
这样可以作为边界值的判断依据。详细的代码中有。
声明一个int 变量 index 他默认时候就就指向字符'1',当我按右方向键是 index就index加1 这样就 就指向了'2' ,当然还要考虑边界值的问题,如果我按上方向键不能指丢了。有了索引我就根据checkchar[index]找到我当前选中是哪个字符,按enter就告诉程序我输入了哪个字符。然后程序对这个字符做处理。
贴下代码,这个程序还没实现允许使用退格键,如果用户想用数字键输入也兼容也没实现。也没做容错机制
 class Program
    {
        static string display = "";//用于显示用户的输入
       
        static string first = "";//保存输入的第一个数
        static string second = "";//保存输入的第二个数
        static string res = "";//用于保存结果
        static char operatorChar = '0';//用于接收用户输入的运算符 0表示没有运算符
        static string[] charDnumber = new string[] { "(1)", "(2)", "(3)", "(.)", "(4)", "(5)", "(6)", "(+)", "(7)", "(8)", "(9)", "(-)", "(0)", "(=)", "(*)", "(/)" };
        static int[] operatorchar = new int[] {7,11,14,15};//对应 +-*/
        static ConsoleKey[] direction = new ConsoleKey[] { ConsoleKey.UpArrow,ConsoleKey.DownArrow,ConsoleKey.LeftArrow,ConsoleKey.RightArrow};
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("\n\t\t\t请用方向键,Enter键,退格键操作");
            Console.ResetColor();
            char[] charTable = new char[16] {  '1', '2', '3', '.' , '4', '5', '6', '+' ,  '7', '8', '9', '-' , '0', '=', '*', '/'  };//构建一维的数组。
            print(display, -1);//打印一个界面
            bool flog = true;
            int currentIndex = -1;//当前的-1默认指向为空
            while (flog)
            {
                ConsoleKeyInfo cki = Console.ReadKey();
                if (isdirection(cki.Key)&& currentIndex == -1)
                {
                    currentIndex = 0;//输入了方向键就默认指向0号索引
                    Console.Clear();
                    print(display, 0);
                    continue;
                }
                if (isdirection(cki.Key) && currentIndex != -1)//如果用户输入了方向键
                {
                    switch (cki.Key)
                    {
                        case ConsoleKey.UpArrow:
                          currentIndex =up(currentIndex);
                            break;
                        case ConsoleKey.DownArrow:
                            currentIndex = down(currentIndex);
                            break;
                        case ConsoleKey.LeftArrow:
                            currentIndex = left(currentIndex);
                            break;
                        case ConsoleKey.RightArrow:
                            currentIndex = right(currentIndex);
                            break;
                        default:
                            break;
                    }                  
                    Console.Clear();
                    print(display, currentIndex);
                }
                if(cki.Key==ConsoleKey.Enter)//如果输入了enter键
                {
                    if (isOperateor(currentIndex))//此时输入了运算符
                    {
                        operatorChar = charTable[currentIndex];
                        display += charTable[currentIndex].ToString();
                        Console.Clear();
                        print(operatorChar.ToString(), currentIndex);
                        continue;
                    }
                    if (operatorChar == '0')//此时没有输入运算符 ,就把输入保存到first
                    {
                        first = first + charTable[currentIndex].ToString();
                        display = display + charTable[currentIndex].ToString();
                       //first = tempint;
                       Console.Clear();
                       print(first, currentIndex);                 
                       continue;
                    }
                    if (currentIndex == 13 && first != "" && second != "" && operatorChar != '0')//此时已经输入运算符 ,两个操作数也有,就根据操作数运算
                    {
                        switch (operatorChar)
                        {
                            case '+':
                                res = add(first, second);
                                break;
                            case '-':
                                res = minus(first, second);
                                break;
                            case '*':
                                res = multiply(first, second);
                                break;
                            case '/':
                                res = divide(first, second);
                                break;
                            default:
                                break;
                        }
                        Console.Clear();
                        print(res, -1);
                        init();//初始化一下各个变量                     
                    }
                    if (operatorChar != '0' && first != "")//此时已经输入运算符 ,first也不为空,就把输入保存到second
                    {
                        second = second + charTable[currentIndex].ToString();
                        display = display + charTable[currentIndex].ToString();
                        Console.Clear();
                        print(second, currentIndex);
                        continue;
                    }  
                }
            }           
        }
        /// <summary>
        /// 初始化程序..
        /// </summary>
        static void init()
        {
            display = "";
           
            first = "";
            second = "";
            operatorChar = '0';
        }
       /// <summary>
       /// 打印界面
       /// </summary>
       /// <param name="number">显示的数据</param>
       /// <param name="index">要高亮字符的索引</param>
        static void print(string number, int index)
        {

            string[] charNumber = new string[] { " 1 ", " 2 ", " 3 ", " . ", " 4 ", " 5 ", " 6 ", " + ", " 7 ", " 8 ", " 9 ", " - ", " 0 ", " = ", " * ", " / " };

            if (index > -1 && index < 16)
            {
                charNumber[index ] = charDnumber[index ];
            }
            Console.Write("\n\n\n\n\t\t\t┏");
            Console.Write("━━━━━━━━━━");
            Console.WriteLine("┓");
            Console.WriteLine("\t\t\t┃\t\t      ┃");
            int count = 0;
            count = number.Length;
            Console.Write("\t\t\t┃");
            for (int i = 0; i < 19 - count; i++)
            {
                Console.Write(" ");
            }
            Console.Write(number);
            Console.WriteLine(" ┃");
            Console.WriteLine("\t\t\t┃--------------------┃");
            Console.WriteLine("\t\t\t┃  " + charNumber[0] + "  " + charNumber[1] + "  " + charNumber[2] + "  " + charNumber[3] + "┃");
            Console.WriteLine("\t\t\t┃\t\t      ┃");
            Console.WriteLine("\t\t\t┃  " + charNumber[4] + "  " + charNumber[5] + "  " + charNumber[6] + "  " + charNumber[7] + "┃");
            Console.WriteLine("\t\t\t┃\t\t      ┃");
            Console.WriteLine("\t\t\t┃  " + charNumber[8] + "  " + charNumber[9] + "  " + charNumber[10] + "  " + charNumber[11] + "┃");
            Console.WriteLine("\t\t\t┃\t\t      ┃");
            Console.WriteLine("\t\t\t┃  " + charNumber[12] + "  "+charNumber[13]+"  " + charNumber[14] + "  " + charNumber[15] + "┃");
            Console.WriteLine("\t\t\t┃\t\t      ┃");
            Console.WriteLine("\t\t\t┗━━━━━━━━━━┛");


        
        }
        //获取输入字符的索引
        static int getIndex(char flog)
        {
            switch (flog)
            {
                case '1':
                    return 1;
                case '2':
                    return 2;
                case '3':
                    return 3;
                case '.':
                    return 4;
                case '4':
                    return 5;
                case '5':
                    return 6;
                case '6':
                    return 7;
                case '+':
                    return 8;
                case '7':
                    return 9;
                case '8':
                    return 10;
                case '9':
                    return 11;
                case '-':
                    return 12;
                case '0':
                    return 13;
                case '=':
                    return 14;
                case '*':
                    return 15;
                case '/':
                    return 16;
            }
            return -1;
        }
        //加运算
        static string add(string a, string b)
        {

            double i = Double.Parse(a);
            double j = Double.Parse(b);
            return (j + i).ToString();
        }
        //减运算
        static string minus(string a, string b)
        {
            double i = Double.Parse(a);
            double j = Double.Parse(b);
            return (i - j).ToString();
        }
        //×运算
        static string multiply(string a, string b)
        {
            double i = Double.Parse(a);
            double j = Double.Parse(b);
            return (j * i).ToString();
        }
        //除运算
        static string divide(string a, string b)
        {
            double i = Double.Parse(a);
            double j = Double.Parse(b);
            if (j == 0)
            {
                return "0";
            }
            return (i / j).ToString();
        }
        //按up键
        static int up(int current)
        {
          
            if (current>=0&&current<=3)//说明现在键在0-3索引出
            {
                return current+12;
            }
            else
            {
                return current - 4;
            }
        }
        //按下down键
        static int down(int current)
        {
          
            if (current>=12&&current<=15)//说明现在键在12-15索引出
            {
                return current-12;
            }
            else
            {
                return current + 4;
            }
        }
        //按下left键
        static int left(int current)
        {
            if (current % 4 == 0)
            {
                return current + 3;
            }
            else
            {
                return current - 1;
            }
        }
        //按下reght键
        static int right(int current)
        {
            if ((current + 1) % 4 == 0)
            {
                return current - 3;
            }
            else
            {
                return current + 1;
            }
        }
        //判断是不是操作符
        static bool isOperateor(int tempindex)
        {
            if (operatorchar.Contains(tempindex))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //判断是不是输入了方向键
        static bool isdirection(ConsoleKey tempchar)
        {
            if (direction.Contains(tempchar))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }