C#仿密保卡功能的简单实现代码

时间:2022-01-17 04:51:12 

不过我写的比较草率,代码结构不是很好,也没有体现OOP的思想,这几天有空会重构一下。

先把代码发出来:


public class MatrixCardManager
    {
        public static int[,] ReadMatrixCardFromString(string matrixStr)
        {
            int[,] arr1 = new int[5, 5];
            int[] tempArr = new int[25];
            int k = 0;
            string[] tempArrStr = matrixStr.Split(',');
            for (int i = 0; i < tempArr.Length; i++)
            {
                tempArr[i] = Convert.ToInt32(tempArrStr[i]);
            }
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    arr1[i, j] = tempArr[k];
                    k++;
                }
            }
            return arr1;
        }

        public static string SaveMatrixIntoString(int[,] arr)
        {
            string matrixStr = String.Empty;
            int[] lineArr = new int[25];
            int k = 0;
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    lineArr[k] = arr[i, j];
                    k++;
                }
            }
            for (int i = 0; i < lineArr.Length; i++)
            {
                matrixStr += lineArr[i];
                if (i < 24)
                {
                    matrixStr += ",";
                }
            }
            return matrixStr;
        }

        public static void PrintMatrix(int[,] arr)
        {
            Console.WriteLine("  | A\tB\tC\tD\tE");
            Console.WriteLine("-------------------------------------------");
            for (int k = 0; k < 5; k++)
            {
                Console.Write(k + " | ");
                for (int l = 0; l < 5; l++)
                {
                    Console.Write(arr[k, l] + "\t");
                }
                Console.WriteLine();
            }
        }

        public static int[,] GenerateRandomMatrix()
        {
            Random r = new Random();
            int[,] arr = new int[5, 5];
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    arr[i, j] = r.Next(0, 100);
                }
            }
            return arr;
        }

        public static char GetColCode(int colIndex)
        {
            char colCode = '-';
            switch (colIndex)
            {
                case 0:
                    colCode = 'A';
                    break;
                case 1:
                    colCode = 'B';
                    break;
                case 2:
                    colCode = 'C';
                    break;
                case 3:
                    colCode = 'D';
                    break;
                case 4:
                    colCode = 'E';
                    break;
                default:
                    break;
            }
            return colCode;
        }

        public static bool Validate(int[,] arr, int colIndex1, int rowIndex1, int colIndex2, int rowIndex2, int colIndex3, int rowIndex3, string userInput, bool validFlag)
        {
            try
            {
                string[] inputArr = userInput.Split(',');

                bool OK0 = arr[rowIndex1, colIndex1] == Convert.ToInt32(inputArr[0]);
                bool OK1 = arr[rowIndex2, colIndex2] == Convert.ToInt32(inputArr[1]);
                bool OK2 = arr[rowIndex3, colIndex3] == Convert.ToInt32(inputArr[2]);

                if (OK0 && OK1 && OK2)
                {
                    validFlag = true;
                }
                else
                {
                    validFlag = false;
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Oh, **!");
            }
            return validFlag;
        }
    }


调用:


static void Main(string[] args)
        {
            Console.WriteLine("Generate and Print Matrix Card:\n");
            int[,] arr = MatrixCardManager.GenerateRandomMatrix();
            MatrixCardManager.PrintMatrix(arr);
            Console.WriteLine("\n");

            Console.WriteLine("Save Matrix Card into string for storage:\n");
            string matrixStr = MatrixCardManager.SaveMatrixIntoString(arr);
            Console.WriteLine(matrixStr);
            Console.WriteLine("\n");

            Console.WriteLine("Read Matrix Card from string:\n");
            int[,] arr1 = MatrixCardManager.ReadMatrixCardFromString(matrixStr);
            MatrixCardManager.PrintMatrix(arr1);
            Console.WriteLine("\n");

            Console.WriteLine("Matrix Card Validation:\n");
            Random r = new Random();

            int colIndex1 = r.Next(0, 4);
            int rowIndex1 = r.Next(0, 4);
            char colCode1 = MatrixCardManager.GetColCode(colIndex1);

            int colIndex2 = r.Next(0, 4);
            int rowIndex2 = r.Next(0, 4);
            char colCode2 = MatrixCardManager.GetColCode(colIndex2);

            int colIndex3 = r.Next(0, 4);
            int rowIndex3 = r.Next(0, 4);
            char colCode3 = MatrixCardManager.GetColCode(colIndex3);

            Console.WriteLine("Please Input Card Number At {0}{1},{2}{3},{4}{5}:\n", colCode1, rowIndex1, colCode2, rowIndex2, colCode3, rowIndex3);

            string userInput = Console.ReadLine();

            bool validFlag = false;
            validFlag = MatrixCardManager.Validate(arr, colIndex1, rowIndex1, colIndex2, rowIndex2, colIndex3, rowIndex3, userInput, validFlag);
            if (validFlag)
            {
                Console.WriteLine("All input are correct!");
            }
            else
            {
                Console.WriteLine("Sorry, your input were wrong!");
            }
            Console.ReadKey();
        }

效果:

C#仿密保卡功能的简单实现代码

标签:密保卡
0
投稿

猜你喜欢

  • C#实现SMTP邮件附件发送功能详解

    2022-08-14 10:09:38
  • 如何通过Android Stduio来编写一个完整的天气预报APP

    2023-10-11 17:45:01
  • idea企业开发之新建各类型项目的详细教程

    2023-02-28 05:27:11
  • Spring @CrossOrigin 注解原理实现

    2022-09-03 10:40:04
  • Android6.0获取动态权限代码示例

    2022-07-22 10:35:15
  • Android编程获取GPS数据的方法详解

    2023-09-20 16:37:34
  • C#调用和实现WebService,纯手工打造!

    2023-12-12 14:58:30
  • C#中怎样从指定字符串中查找并替换字符串?

    2023-09-30 14:26:24
  • C#双缓冲技术实例详解

    2023-02-02 15:56:37
  • android上的一个网络接口和图片缓存框架enif简析

    2022-10-06 07:37:56
  • c#生成随机数示例分享

    2023-10-04 06:54:50
  • SpringBoot中实现分布式的Session共享的详细教程

    2023-08-23 18:23:43
  • Java 可视化垃圾回收_动力节点Java学院整理

    2023-02-19 07:03:55
  • 简单讲解Android开发中触摸和点击事件的相关编程方法

    2023-03-30 12:29:10
  • C#用匿名方法定义委托的实现方法

    2021-07-08 22:49:07
  • MybatisPlus实现对象嵌套关联查询一对多List集合查询

    2022-04-27 16:00:12
  • JAVA WSIMPORT生成WEBSERVICE客户端401认证过程图解

    2023-11-14 00:27:55
  • C#如何用ThoughtWorks生成二维码

    2022-09-28 20:44:53
  • C#的TimeSpan案例详解

    2023-11-20 11:34:52
  • c++如何实现跳表(skiplist)

    2022-02-10 18:05:58
  • asp之家 软件编程 m.aspxhome.com