查看: 346|回复: 0

[.NET源码] C#实现微信红包功能

发表于 2018-2-8 08:00:02

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

Program.cs代码:

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. //初始化要发起的红包基础数据
  6. double total = 100;
  7. int num = 50;
  8. double min = 0.01;
  9. string temp;
  10. bool flag = false;
  11. Console.WriteLine(string.Format("是否需要自定义红包金额和数量(默认{0}元/小贝人)Y/N:", total, num));
  12. temp = Console.ReadLine();
  13. if (temp.Trim().ToLower().Equals("y") || temp.Trim().ToLower().Equals("yes"))
  14. {
  15. Console.WriteLine("请输入你要发起的红包金额:");
  16. do
  17. {
  18. temp = Console.ReadLine();
  19. flag = double.TryParse(temp, out total);
  20. if (!flag)
  21. {
  22. Console.WriteLine("金额必须为整数或小数,请重新输入:");
  23. }
  24. } while (!flag);
  25. Console.WriteLine("请输入你要发起的红包个数:");
  26. do
  27. {
  28. temp = Console.ReadLine();
  29. flag = int.TryParse(temp, out num);
  30. if (!flag)
  31. {
  32. Console.WriteLine("红包个数必须为整数,请重新输入:");
  33. }
  34. } while (!flag);
  35. }
  36. total -= min * num;
  37. if (total < 0)
  38. {
  39. Console.WriteLine("抱歉,你的金额不足!");
  40. return;
  41. }
  42. //产生正态分布的随机红包金额,并计算相关的金额和数量保证数据的准确性
  43. double average = total / num;
  44. double variance = 1;
  45. Random u1 = new Random();
  46. Random u2 = new Random();
  47. double[] nums = new double[num];
  48. for (int i = 0; i < num; i++)
  49. {
  50. double? result = total;
  51. if (i < num - 1 && total > 0)
  52. {
  53. do
  54. {
  55. result = Round((double)Normal(u1.NextDouble(), u2.NextDouble(), average, variance), 2);
  56. } while (result == null || result < 0);
  57. if (total > result)
  58. {
  59. total = (double)Round((total - (double)result), 2);
  60. }
  61. else
  62. {
  63. result = total;
  64. total = 0;
  65. }
  66. }
  67. else if (i == num - 1)
  68. {
  69. total = 0;
  70. }
  71. nums[i] = Math.Round(min + (double)result, 2); //浮点运算问题,这里需要四舍五入数据才正确
  72. Console.WriteLine(string.Format("第{0}个红包金额:小贝", i + 1, (min + result)));
  73. Console.WriteLine("剩余金额:" + ((i != num - 1 && total == 0) ? min * (num - i - 1) : total + (min * (num - i - 1))));
  74. }
  75. Console.WriteLine("最大金额:" + nums.Max());
  76. Console.WriteLine("最小金额:" + nums.Min());
  77. Console.WriteLine("总额:" + Round(nums.Sum(), 2));
  78. Console.WriteLine("初始方差:" + variance);
  79. Console.WriteLine("结果方差:" + Variance(nums));
  80. Console.WriteLine("按任意键退出!");
  81. Console.ReadKey();
  82. }
  83. /// <summary>
  84. /// 产生符合正态分布的随机数
  85. /// </summary>
  86. /// <param name="u1">正态分布第一个随机数</param>
  87. /// <param name="u2">正态分布第二个随机数</param>
  88. /// <param name="averageValue">正态期望(平均值)</param>
  89. /// <param name="variance">正态标准差(Math.Sqrt(方差))</param>
  90. /// <returns></returns>
  91. public static double? Normal(double u1, double u2, double averageValue, double variance)
  92. {
  93. double? result = null;
  94. try
  95. {
  96. result = averageValue + Math.Sqrt(variance) * Math.Sqrt((-2) * Math.Log(u1)) * Math.Sin(2 * Math.PI * u2);
  97. }
  98. catch (Exception)
  99. {
  100. result = null;
  101. }
  102. return result;
  103. }
  104. /// <summary>
  105. /// 求一组数据的方差
  106. /// </summary>
  107. /// <param name="list">要求的数组</param>
  108. /// <returns></returns>
  109. public static double Variance(double[] nums)
  110. {
  111. double average = nums.Sum() / nums.Length;
  112. double sum = 0;
  113. double variance = 0;
  114. foreach (double num in nums)
  115. {
  116. sum += Math.Pow((num - average), 2);
  117. }
  118. variance = sum / nums.Length;
  119. return variance;
  120. }
  121. /// <summary>
  122. /// 截取小数指定小数位,且不四舍五入
  123. /// </summary>
  124. /// <param name="originNum">要截取的小数</param>
  125. /// <param name="lastNum">截取小数后位数</param>
  126. /// <returns></returns>
  127. public static double? Round(double originNum, int lastNum)
  128. {
  129. double? result = null;
  130. int index = originNum.ToString().IndexOf('.');
  131. if (index != -1)
  132. {
  133. string temp = originNum.ToString();
  134. result = Convert.ToDouble(temp.Substring(0, index + 1) + temp.Substring(index + 1, Math.Min(temp.Length - index - 1, lastNum)));
  135. }
  136. if (result == 0)
  137. {
  138. result = null;
  139. }
  140. else if (index == -1)
  141. {
  142. result = originNum;
  143. }
  144. return result;
  145. }
  146. }
复制代码

运行结果:

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

您可能感兴趣的文章:

  • c#版在pc端发起微信扫码支付的实例
  • C# MVC 微信支付教程系列之公众号支付代码
  • C# MVC 微信支付教程系列之扫码支付代码实例


回复

使用道具 举报