欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-18 08:32:46
不过我写的比较草率,代码结构不是很好,也没有体现oop的思想,这几天有空会重构一下。 先把代码发出来: 复制代码 代码如下:public class matrixcar...

不过我写的比较草率,代码结构不是很好,也没有体现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#仿密保卡功能的简单实现代码

上一篇:

下一篇: