C++分析研究  
C++
日历
<2011年3月>
272812345
6789101112
13141516171819
20212223242526
272829303112
3456789
统计
  • 随笔 - 92
  • 文章 - 4
  • 评论 - 4
  • 引用 - 0

导航

常用链接

留言簿

随笔档案

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

  c++概率的运用,基于控制台程序

  #include<iostream>

  #include<windows.h>

  #include<conio.h>

  #include<time.h>

  #include<stdlib.h>

  using namespace std;

  double Count_com=0; //电脑赢得次数

  double Count_you=0; //玩家赢得次数

  int stone = 1, cloth = 2, scissors = 3; //石头剪刀布中的三种状态

  /////////////////声明//////////////////////

  int randget(int start, int end);

  double getDoubleRand(int start, int end);

  void gameing();

  void start1();

  void start2();

  void start3();

  void tell(int com, int you);

  /////////////////定义randget///////////////

  int randget(int start, int end)

  {

  return rand() % (end - start + 1) + start; //从start到end开始取随机数;

  }

  /////////////////定义getDoubleRand/////////

  double getDoubleRand(int start, int end)

  {

  return rand() % (end - start + 1) + start;

  }

  ////////////////定义石头剪刀布////////////////

  void start1()

  {

  srand((unsigned int)time(NULL));

  char x;

  int com = 0, you = 0;

  cout 《 "\n Z:石头X:剪刀C:布 你出:" 《 endl;

  while (1)

  {

  if (_kbhit()) //_kbhit()判断是否有键盘操作

  {

  x = _getch();

  if ((x == 'Z' || x == 'z')) you = 1;

  if ((x == 'X' || x == 'x')) you = 3;

  if ((x == 'C' || x == 'c')) you = 2;

  while (_kbhit())

  _getch();

  com = randget(1, 3);

  tell(com,you);

  }

  }

  }

  ////////////////定义抛硬币///////////////

  void start2()

  {

  cout 《 "按任意键开始抛硬币" 《 endl;

  system("pause");

  srand((unsigned int)time(NULL));

  int coin = 0;

  long double rec = 0, times = 0, rec2 = 0;

  for (;;)

  {

  Sleep(50); //设置进程的速度。

  times += 1;

  coin = randget(0, 1); //随机取正反面

  if (coin == 1) //逻辑判断

  {

  rec++;

  cout 《 "正面 正面几率:" 《 rec / times 《 "%" 《 endl;

  }

  if (coin == 0)

  {

  rec2++;

  cout 《 "反面 反面几率:" 《 rec2 / times 《 "%" 《 endl;

  }

  }

  }

  ////////////////定义π的计算////////////

  void start3()

  {

  srand((unsigned int)time(NULL));

  cout 《 "现在讲进行π的计算,按任何键开始。(撒种子法)" 《 endl;

  double x = 0, y = 0; //随机取点定义x和y的坐标

  double pi = 0, times = 0, distance = 0, init = 0;

  for (;;)

  {

  Sleep(50);

  times += 1;

  x = getDoubleRand(0, 2);

  y = getDoubleRand(0, 2);

  distance = (x - 1)*(x - 1) + (y - 1)*(y - 1);

  if (distance <= 1)

  {

  init += 1;

  cout 《 "当前是第" 《 times 《 "次计算π的值,π当前测的值是:" 《 4 * ( init / times )/ 1 《 endl;

  }

  }

  }

  /////////////辨别输赢///////////////////

  void tell(int com, int you)

  {

  double rate, temp;

  temp = Count_you + Count_com;

  rate = Count_you / temp * 100;

  if (com == you)

  {

  cout 《 "打平了!" 《 "****************……你的胜率:" 《 rate 《 "%" 《 endl;

  }

  if ((com == 1) && (you == 2))

  {

  cout 《 "你赢了!****" 《 "电脑>>>石头****你>>>布" 《 "……你的胜率:" 《 rate 《 "%" 《 endl;

  Count_you++;

  }

  if ((com == 1) && (you == 3))

  {

  cout 《 "你输了!****" 《 "电脑>>>石头****你>>>剪刀" 《 "……你的胜率:" 《 rate 《 "%" 《 endl;

  Count_com++;

  }

  if ((com == 2) && (you == 1))

  {

  cout 《 "你输了!****" 《 "电脑>>>布 ****你>>>石头" 《 "……你的胜率:" 《 rate 《 "%" 《 endl;

  Count_com++;

  }

  if ((com == 2) && (you == 3))

  {

  cout 《 "你赢了!****" 《 "电脑>>>布 ****你>>>剪刀" 《 "……你的胜率:" 《 rate 《 "%" 《 endl;

  Count_you++;

  }

  if ((com == 3) && (you == 1))

  {

  cout 《 "你赢了!****" 《 "电脑>>>剪刀****你>>>石头" 《 "……你的胜率:" 《 rate 《 "%" 《 endl;

  Count_you++;

  }

  if ((com == 3) && (you == 2))

  {

  cout 《 "你输了!****" 《 "电脑>>>剪刀****你>>>布 " 《 "……你的胜率:" 《 rate 《 "%" 《 endl;

  Count_com++;

  }

  }

  /////////////主函数//////////////////////

  int main()

  {

  int choice=0;

  again: cout 《 "________________________概率测试_____________________" 《 endl;

  cout 《 "1.石头剪刀布。" 《 endl;

  cout 《 "2.抛硬币。" 《 endl;

  cout 《 "3.π的计算。" 《 endl;

  cout 《 "_____________________________________________________" 《 endl;

  cin 》 choice;

  switch (choice)

  {

  case 1:start1();

  case 2:start2();

  case 3:start3();

  [cpp] view plaincopy在CODE上查看代码片派生到我的代码片

  default:goto again;

  }

  system("pause"); www.qcwyo68.com  托福答案

 

posted on 2014-02-12 18:28 HAOSOLA 阅读(331) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


 
Copyright © HAOSOLA Powered by: 博客园 模板提供:沪江博客
PK10开奖 PK10开奖