欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

C#实现微信红包功能

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

本文实例为大家分享了C#仿微信红包功能的具体代码,供大家参考,具体内容如下

Program.cs代码:

class Program
  {
    static void Main(string[] args)
    {
      //初始化要发起的红包基础数据
      double total = 100;
      int num = 50;
      double min = 0.01;
      string temp;
      bool flag = false;
      Console.WriteLine(string.Format("是否需要自定义红包金额和数量(默认{0}元/{1}人)Y/N:", total, num));
      temp = Console.ReadLine();
      if (temp.Trim().ToLower().Equals("y") || temp.Trim().ToLower().Equals("yes"))
      {
        Console.WriteLine("请输入你要发起的红包金额:");
        do
        {
          temp = Console.ReadLine();
          flag = double.TryParse(temp, out total);
          if (!flag)
          {
            Console.WriteLine("金额必须为整数或小数,请重新输入:");
          }
        } while (!flag);
        Console.WriteLine("请输入你要发起的红包个数:");
        do
        {
          temp = Console.ReadLine();
          flag = int.TryParse(temp, out num);
          if (!flag)
          {
            Console.WriteLine("红包个数必须为整数,请重新输入:");
          }
        } while (!flag);
      }

      total -= min * num;
      if (total < 0)
      {
        Console.WriteLine("抱歉,你的金额不足!");
        return;
      }

      //产生正态分布的随机红包金额,并计算相关的金额和数量保证数据的准确性
      double average = total / num;
      double variance = 1;
      Random u1 = new Random();
      Random u2 = new Random();
      double[] nums = new double[num];

      for (int i = 0; i < num; i++)
      {
        double? result = total;
        if (i < num - 1 && total > 0)
        {
          do
          {
            result = Round((double)Normal(u1.NextDouble(), u2.NextDouble(), average, variance), 2);
          } while (result == null || result < 0);
          if (total > result)
          {
            total = (double)Round((total - (double)result), 2);
          }
          else
          {
            result = total;
            total = 0;
          }
        }
        else if (i == num - 1)
        {
          total = 0;
        }
        nums[i] = Math.Round(min + (double)result, 2); //浮点运算问题,这里需要四舍五入数据才正确

        Console.WriteLine(string.Format("第{0}个红包金额:{1}", i + 1, (min + result)));
        Console.WriteLine("剩余金额:" + ((i != num - 1 && total == 0) ? min * (num - i - 1) : total + (min * (num - i - 1))));
      }
      Console.WriteLine("最大金额:" + nums.Max());
      Console.WriteLine("最小金额:" + nums.Min());
      Console.WriteLine("总额:" + Round(nums.Sum(), 2));
      Console.WriteLine("初始方差:" + variance);
      Console.WriteLine("结果方差:" + Variance(nums));
      Console.WriteLine("按任意键退出!");

      Console.ReadKey();
    }

    /// <summary>
    /// 产生符合正态分布的随机数
    /// </summary>
    /// <param name="u1">正态分布第一个随机数</param>
    /// <param name="u2">正态分布第二个随机数</param>
    /// <param name="averageValue">正态期望(平均值)</param>
    /// <param name="variance">正态标准差(Math.Sqrt(方差))</param>
    /// <returns></returns>
    public static double? Normal(double u1, double u2, double averageValue, double variance)
    {
      double? result = null;
      try
      {
        result = averageValue + Math.Sqrt(variance) * Math.Sqrt((-2) * Math.Log(u1)) * Math.Sin(2 * Math.PI * u2);
      }
      catch (Exception)
      {
        result = null;
      }

      return result;
    }

    /// <summary>
    /// 求一组数据的方差
    /// </summary>
    /// <param name="list">要求的数组</param>
    /// <returns></returns>
    public static double Variance(double[] nums)
    {
      double average = nums.Sum() / nums.Length;
      double sum = 0;
      double variance = 0;
      foreach (double num in nums)
      {
        sum += Math.Pow((num - average), 2);
      }
      variance = sum / nums.Length;

      return variance;
    }

    /// <summary>
    /// 截取小数指定小数位,且不四舍五入
    /// </summary>
    /// <param name="originNum">要截取的小数</param>
    /// <param name="lastNum">截取小数后位数</param>
    /// <returns></returns>
    public static double? Round(double originNum, int lastNum)
    {
      double? result = null;
      int index = originNum.ToString().IndexOf('.');
      if (index != -1)
      {
        string temp = originNum.ToString();
        result = Convert.ToDouble(temp.Substring(0, index + 1) + temp.Substring(index + 1, Math.Min(temp.Length - index - 1, lastNum)));
      }
      if (result == 0)
      {
        result = null;
      }
      else if (index == -1)
      {
        result = originNum;
      }

      return result;
    }
  }

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

上一篇:C# winform实现右下角弹出窗口结果的方法

栏    目:C#教程

下一篇:详解C# 中的正则表达式运用

本文标题:C#实现微信红包功能

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5653.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有