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 托福答案