【C++运用】(控制台)-概率的运用,石头剪刀布,抛硬币,圆周率的计算---ShinePans

来源:互联网 发布:淘宝如何清洗订单 编辑:程序博客网 时间:2024/04/28 22:09
/*程序运用了随机数,kbhit()函数,getch函数,以及一些设计的算法*/#include<iostream>#include<windows.h>#include<conio.h>#include<time.h>#include<stdlib.h>#include<math.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);void gameing();void start1();   //石头剪刀布void start2();   //抛硬币void start3();  //π的计算void menu();void tell(int com, int you);/////////////////定义randget///////////////int randGet(int start, int end){return rand() % (end - start + 1) + start; //从start到end开始取随机数;}////////////////定义石头剪刀布////////////////void start1(){srand((unsigned int)time(NULL));char x;int com = 0, you = 0;cout << "\n      Z:石头X:剪刀C:布  你出:(按q/Q结束游戏)" << endl;for (;;){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;if ((x != 'z'&&x != 'Z'&&x != 'x'&&x != 'X'&&x != 'c'&&x != 'C'&&x!='q'&&x!='Q'))   //屏蔽其他的按键{cout << "输入无效,请重新开始" << endl; start1();}if ((x == 'q' || x == 'Q'))  menu();         //退出按qcom = 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 *100<< "%" << endl;}if (coin == 0){rec2++;cout << "反面 反面几率:" << rec2 / times *100<< "%" << endl;}if (_kbhit()){system("pause");  menu();}}}////////////////定义π的计算////////////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 =double( randGet(0, 2000))/1000.0;y =double( randGet(0, 2000))/1000.0;distance =sqrt( (x - 1)*(x - 1) + (y - 1)*(y - 1));if (distance <= 1.0){init += 1;}cout << x << "当前是第" << times << "次计算π的值,π当前测的值是:" << 4 * (init / times) / 1 << endl;if (_kbhit()){system("pause");menu();}}}/////////////辨别输赢///////////////////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++;}}/////////////主函数//////////////////////void menu(){int choice = 0; 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();default:menu();}system("pause");}int main(){menu();}

此处的π运算不精确,会找到更加精确的计算方法
                                             
4 0
原创粉丝点击