zoukankan      html  css  js  c++  java
  • 简单算术验证码

        /// <summary>
            /// 随机获取运算符
            /// </summary>
            /// <returns></returns>
            private string GetOperator()
            {
                string OperatorArray = "+,-,×,÷";
                String[] VcArray = OperatorArray.Split(',');
                Random random = new Random();
                int iNum = random.Next(VcArray.Length);
                return VcArray[iNum];
            }

      /// <summary>
            /// 随机获取数并添加到数组
            /// </summary>
            /// <returns></returns>
            private int[] GetNumber(string oper)
            {
                int[] numList = new int[2];
                string OperatorArray = "1,2,3,4,5,6,7,8,9,10,12,15,18,20";
                String[] VcArray = OperatorArray.Split(',');
                Random random = new Random();
                int iNum = random.Next(VcArray.Length);
                int iNum1 = random.Next(VcArray.Length);
                if (oper == "-" || oper == "÷")
                {
                    numList[0] = Convert.ToInt32(VcArray[iNum]) >= Convert.ToInt32(VcArray[iNum1]) ? Convert.ToInt32(VcArray[iNum]) : Convert.ToInt32(VcArray[iNum1]);
                    numList[1] = Convert.ToInt32(VcArray[iNum]) < Convert.ToInt32(VcArray[iNum1]) ? Convert.ToInt32(VcArray[iNum]) : Convert.ToInt32(VcArray[iNum1]);
                    if (oper == "÷")
                    {
                        //必须整除
                        while (numList[0] % numList[1] > 0)
                        {
                            iNum = random.Next(VcArray.Length);
                            iNum1 = random.Next(VcArray.Length);
                            numList[0] = Convert.ToInt32(VcArray[iNum]) >= Convert.ToInt32(VcArray[iNum1]) ? Convert.ToInt32(VcArray[iNum]) : Convert.ToInt32(VcArray[iNum1]);
                            numList[1] = Convert.ToInt32(VcArray[iNum]) < Convert.ToInt32(VcArray[iNum1]) ? Convert.ToInt32(VcArray[iNum]) : Convert.ToInt32(VcArray[iNum1]);
                        }
                    }
                }
                else
                {
                    numList[0] = Convert.ToInt32(VcArray[iNum]);
                    numList[1] = Convert.ToInt32(VcArray[iNum1]);
                }
                return numList;
            }

    /// <summary>
            /// 获取运算结果
            /// </summary>
            /// <param name="firstNum"></param>
            /// <param name="secondNum"></param>
            /// <param name="oper"></param>
            /// <returns></returns>
            private int GetResult(int firstNum, int secondNum, string oper)
            {
                int result = 0;
                switch (oper)
                {
                    case "+":
                        result = firstNum + secondNum;
                        break;
                    case "-":
                        result = firstNum - secondNum;
                        break;
                    case "×":
                        result = firstNum * secondNum;
                        break;
                    case "÷":
                        result = firstNum / secondNum;
                        break;
                }
                return result;
            }

     /// <summary>
            /// 根据产生的随机数生成图象
            /// </summary>
            /// <param name="strCode"></param>
            private void CreateImg(string strCode, HttpContext context)
            {
                //也可以不用指定的图片,而是画一个背景图片
                Bitmap bitMapImage = new Bitmap(60, 25);
                Graphics graphicImage = Graphics.FromImage(bitMapImage);
                graphicImage.FillRectangle(new SolidBrush(Color.White), 0, 0, 60, 25);

                //设置画笔的输出模式
                graphicImage.SmoothingMode = SmoothingMode.HighSpeed;

                //添加文本字符串
                graphicImage.DrawString(strCode, new Font("Arial", 15, FontStyle.Italic), SystemBrushes.WindowText, new Point(0, 0));

                //画图片的前景噪音点
                Random randomPixel = new Random();
                for (int i = 0; i < 220; i++)
                {
                    int x = randomPixel.Next(bitMapImage.Width);
                    int y = randomPixel.Next(bitMapImage.Height);

                    bitMapImage.SetPixel(x, y, Color.FromArgb(randomPixel.Next(0, 255), randomPixel.Next(0, 255), randomPixel.Next(0, 255)));
                }

                //设置图像输出的格式
                context.Response.ContentType = "image/jpeg";

                //保存数据流
                bitMapImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);

                //释放占用的资源
                graphicImage.Dispose();
                bitMapImage.Dispose();

            }

       /// <summary>
            /// 调用
            /// </summary>

     public void ProcessRequest(HttpContext context)
            {
                try
                {
                    string strCode = string.Empty;
                    var oper = GetOperator();
                    var firstNum = GetNumber(oper)[0];
                    var secondNum = GetNumber(oper)[1];
                    var resultNum = GetResult(firstNum, secondNum, oper);
                    strCode = firstNum.ToString() + oper + secondNum.ToString() + "=";
                    CreateImg(strCode, context);
                }
                catch (Exception)
                {
                    throw;
                }
            }

  • 相关阅读:
    风口之下,猪都能飞。当今中国股市牛市,真可谓“错过等七年”。 给你一个回顾历史的机会,已知一支股票连续n天的价格走势,以长度为n的整数数组表示,数组中第i个元素(prices[i])代表该股票第i天的股价。 假设你一开始没有股票,但有至多两次买入1股而后卖出1股的机会,并且买入前一定要先保证手上没有股票。若两次交易机会都放弃,收益为0。 设计算法,计算你能获得的最大收益。 输入数值范围:2<=n<
    世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么?
    图——克鲁斯算法——构建最小生成树(采用邻接矩阵的方式存储)
    [liu yanling]黑盒测试用例设计方法
    [Stephen]Export from Excel to ALM
    [liu yanling]测试小结
    [liu yanling]测试用例作用
    [liu yanling]测试流程
    [liu yanling]软件测试技巧
    [liu yanling]软件测试用例的基本要素包括哪些?
  • 原文地址:https://www.cnblogs.com/hpp3501/p/4331843.html
Copyright © 2011-2022 走看看