c++概率的运用,基于控制台程序
作者:网络转载 发布时间:[ 2014/2/10 9:58:56 ] 推荐标签: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 << "
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");
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11