博客
关于我
24点小游戏(c++)
阅读量:594 次
发布时间:2019-03-12

本文共 3450 字,大约阅读时间需要 11 分钟。

#include 
#include
#include
#include
#include
#include
using namespace std;char card[] = { 'A','2','3','4','5','6','7','8','9','10','J','Q','K' };char buf[4];double nums[4];char ope[4] = { '+','-','*','/' };void cre(){ srand(time(0)); for(int i = 0; i < 4; i++) { int j = rand() % 13; buf[i] = card[j]; //随机生成四张牌面为: } for(int i = 0; i < 4; i++) { if(buf[i] == 'A' ) nums[i] = 1; else if(buf[i] == '2') nums[i] = 2; else if(buf[i] == '3') nums[i] = 3; else if(buf[i] == '4') nums[i] = 4; else if(buf[i] == '5') nums[i] = 5; else if(buf[i] == '6') nums[i] = 6; else if(buf[i] == '7') nums[i] = 7; else if(buf[i] == '8') nums[i] = 8; else if(buf[i] == '9') nums[i] = 9; else if(buf[i] == '10') nums[i] = 10; else if(buf[i] == 'J') nums[i] = 11; else if(buf[i] == 'Q') nums[i] = 12; else if(buf[i] == 'K') nums[i] = 13; }}double calcute(double a, double b, char index){ if(index == '+') return a + b; else if(index == '-') return a - b; else if(index == '*') return a * b; else if(index == '/') { if(b != 0) return a / b; //分母不能为0 }}void exh(){ double temp[3]; for(int m = 0; m < 3; m++) { if(nums[m+1] == 0 && ope[m] == '/') break; temp[m] = calcute(nums[m], nums[m+1], ope[m]); idx = (m+2)%3; temp[idx] = nums[idx+1]; //初始化剩下的部分 idx = (m+3)%3; temp[idx] = nums[idx+1]; } for(int n = 0; n < 2; n++) { if(temp[n+1] == 0 && ope[n] == '/') break; tem[n] = calcute(temp[n], temp[n+1], ope[n]); idx = (n+2) % 2; tem[idx] = temp[idx+1]; } if(tem[1] == 0 && ope[k] == '/') break; sum = calcute(tem[0], tem[1], ope[k]); if(sum == 24) { if(m == 0 && n == 0) { cout << "((" << nums[0] << ope[i] << nums[1] << ")" << ope[j] << nums[2] << ")=" << sum << endl; } else if(m == 0 && n == 1) { cout << "(" << nums[0] << ope[i] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[j] << nums[3] << ")=" << sum << endl; } else if(m == 1 && n == 0) { cout << "(" << nums[0] << ope[j] << "(" << nums[1] << ope[i] << nums[2] << ")" << ope[k] << nums[3] << )=" << sum << endl; } else if(m == 1 && n == 1) { cout << (nums[0] << ope[k] << "((" << nums[1] << ope[i] << nums[2] << "))" << ope[j] << nums[3] << )=" << sum << endl; } else if(m == 0 && n == 0) { cout << "((" << nums[0] << ope[i] << nums[1] << ")" << ope[j] << nums[2] << ")=" << sum << endl; } else if(m == 0 && n == 1) { cout << "(" << nums[0] << ope[i] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[j] << nums[3] << ")=" << sum << endl; } else if(m == 1 && n == 0) { cout << "(" << nums[0] << ope[j] << "(" << nums[1] << ope[i] << nums[2] << ")" << ope[k] << nums[3] << )=" << sum << endl; } else if(m == 1 && n == 1) { cout << (nums[0] << ope[k] << "((" << nums[1] << ope[i] << nums[2] << "))" << ope[j] << nums[3] << )=" << sum << endl; } }}int main(){ int i; int select = 1; cout << " ################################################" << endl << "#" << endl << " # 欢迎进入24点游戏 #" << endl << " ################################################" << endl; while(select) { //-clear screen- Sleep(1000); system("cls"); cout << " ################################################" << endl << "#" << endl << " # 请选择游戏模式 #" << endl << " # 0. 开始游戏 1. 退出 #" << endl << " # consortium#" << endl << " ################################################" << endl; cout << " 请输入你的选择(0或1):"; cin >> i; Sleep(1000); system("cls"); switch(i) { case 0: cre(); exh(); break; case 1: select = 0; break; default: cout << "请在0和1之间选择!" << endl; } } return 0;}

转载地址:http://nzjxz.baihongyu.com/

你可能感兴趣的文章
python关于sorted排序函数详细解析 附实战代码(全)
查看>>
python3.8调用m文件,调用matlab
查看>>
python3.9新特性_Python 3.9 新特性速览
查看>>
Python3.x:遍历select下拉框获取value值
查看>>
python360百科_11月编程排行榜:Python超过Java成第二
查看>>
Python关于Paginator分页函数详细解析 附实战代码
查看>>
python3、ipython3、setup-tools、pip等环境搭建详细总结
查看>>
python3下使用cv2.imwrite存储带有中文路径图片或者绝对路径图片
查看>>
python3与django中@property详解
查看>>
python3中如何表示无穷大
查看>>
Python3中提示:no module named 'wxpy'
查看>>
python3中选择文件对话框的格式打开和保存图片
查看>>
python3为什么print永远都是地址_Python3 print 函数用法总结
查看>>
Python3之uuid模块
查看>>
Python3之写入Excel文档(xlsxwriter)
查看>>
Python3之打印操作打印流重定向(下)
查看>>
python3判断字典、列表、元组为空以及字典是否存在某个key的方法
查看>>
Python3利用pymysql操作 MySQL
查看>>
python3处理json文件中含有中文dumps的应用
查看>>
Python3子进程输出
查看>>