24点游戏的代码,支持界面,排行榜查询,答错纠错,no命令,有隐藏bug哦(算是小彩蛋)看不懂评论区问吧,有时间会解答,急就私戳吧
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include<ctime>
#include <string>
#include <stack>
#include <fstream>
#include <cassert>
using namespace std;
int allnumber[10] = { 1,2,3,4,5,6,7,8,9,10};
int score = 0;
double result;
char card[] = { '1','2','3','4','5','6','7','8','9','0' };
char pai[4] = { '@','@','@','@' };//对牌初始化
double nums[4];
char ope[4] = { '+','-','*','/' };
void GetCards(int a[])
{
for (int i = 0; i < 4; i++) {
pai[i] = '@';
}
while (pai[0] == '@' || pai[1] == '@' || pai[2] == '@' || pai[3] == '@')//若牌没有输入,则重新输入
{
int i = 0;
int j;
for (i = 0; i < 4; i++)
{
j = a[i];
pai[i] = card[j];
}
for (i = 0; i < 4; i++)
{
if (pai[i] == '1') nums[i] = 1;
if (pai[i] == '2') nums[i] = 2;
if (pai[i] == '3') nums[i] = 3;
if (pai[i] == '4') nums[i] = 4;
if (pai[i] == '5') nums[i] = 5;
if (pai[i] == '6') nums[i] = 6;
if (pai[i] == '7') nums[i] = 7;
if (pai[i] == '8') nums[i] = 8;
if (pai[i] == '9') nums[i] = 9;
if (pai[i] == '9') nums[i] = 9;
if (pai[i] == '0') nums[i] = 10;
}
}
cout << " 随机获得的4张牌为" << nums[0] << "," << nums[1] << "," << nums[2] << "," << nums[3] << "," << endl;
}
double calculate(double a, double b, char op)
{
if (op == '+') return a + b;
if (op == '-') return a - b;
if (op == '*') return a * b;
if ((op == '/') && (b != 0)) return a / b;
}
int qiongju()
{
double temp[3], tem[2];//第一个符号放置后,经过计算后相当于剩下三个数,这个数组用于存储这三个数
double sum;//求得的和
int judge = 0; //判断是否找到一个合理的解
for (int i = 0; i < 4; i++)//第一次放置的符号
{
for (int j = 0; j < 4; j++) //第二次放置的符号
{
for (int k = 0; k < 4; k++) //第三次放置的符号
{
for (int m = 0; m < 3; m++) //首先计算的两个相邻数字,共有3种情况,相当于括号的作用
{
if (nums[m + 1] == 0 && ope[i] == '/') break;
temp[m] = calculate(nums[m], nums[m + 1], ope[i]);
temp[(m + 1) % 3] = nums[(m + 2) % 4];
temp[(m + 2) % 3] = nums[(m + 3) % 4]; //先确定首先计算的两个数字,计算完成相当于剩下三个数,按顺序储存在temp数组中
for (int n = 0; n < 2; n++) //三个数字选出先计算的两个相邻数字,两种情况,相当于第二个括号
{
if (temp[n + 1] == 0 && ope[j] == '/') break;
tem[n] = calculate(temp[n], temp[n + 1], ope[j]);
tem[(n + 1) % 2] = temp[(n + 2) % 3]; //先确定首先计算的两个数字,计算完成相当于剩下两个数,按顺序储存在temp数组中
if (tem[1] == 0 && ope[k] == '/') break;
sum = calculate(tem[0], tem[1], ope[k]); //计算和 oo
if (sum == 24) //若和为24
{
judge = 1; //判断符为1,表示已求得解
if (m == 0 && n == 0)
cout << "其中一个正确解为:" << "((" << nums[0] << ope[i] << nums[1] << ")" << ope[j] << nums[2] << ")" << ope[k] << nums[3] << "=" << 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 == 2 && n == 0)
cout << "其中一个正确解为:" << "(" << nums[0] << ope[j] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[i] << nums[3] << ")=" << sum << endl;
else if (m == 2 && n == 0)
cout << "其中一个正确解为:" << nums[0] << ope[k] << "(" << nums[1] << ope[j] << "(" << nums[2] << ope[i] << nums[3] << "))=" << sum << endl; //m=0,1,2 n=0,1表示六种括号放置可能,并按照这六种可能输出相应的格式的计算式
}
}
}
}
}
}
return judge;
}
bool isone(char c) {
return (c == '+' || c == '-');
}
bool istwo(char c) {
return (c == '*' || c == '/');
}
string shorten(string m) {
stack<char> s;
string sur;
int i;
char w;
sur;
for (i = 0; i < m.size(); i++) {
if (isdigit(m[i]) || m[i] == '.') {
while (isdigit(m[i]) || m[i] == '.') sur += m[i++];
i--;
sur += '$';
}
else if (isone(m[i])) {
while (s.size() && (isone(s.top()) || istwo(s.top()))) {
sur += s.top();
s.pop();
}
s.push(m[i]);
}
else if (m[i] == ')') {
while (s.top() != '(') {
sur += s.top();
s.pop();
}
s.pop();
}
else if (istwo(m[i])) {
while (s.size() && istwo(s.top())) {
sur += s.top();
s.pop();
}
s.push(m[i]);
}
else s.push(m[i]);
}
while (s.size()) {
sur += s.top();
s.pop();
}
return sur;
}
double tentimes(int n) {
double res = 1;
for (int i = 0; i < n; i++) {
res *= 10;
}
return res;
}
double str2double(string s) {
double res = 0;
char c;
int dec = 0;
for (int i = 1; i <= s.size(); i++) {
c = s[i - 1];
if (c == '.') dec = i;
else if (!dec) res = res * 10 + c - '0';
else res += (c - '0') / tentimes(i - dec);
}
return res;
}
//计算输入的表达式
double calculate(string s) {
double res, t;
stack<double> num;
string temp;
int i;
for (i = 0; i < s.size(); i++) {
temp = "";
if (isdigit(s[i]) || s[i] == '.') {
while (isdigit(s[i]) || s[i] == '.') temp += s[i++]; //如果最后一位是数字,这样做会出错
num.push(str2double(temp));
}
else {
switch (s[i]) {
case '+': t = num.top(); num.pop(); t += num.top(); num.pop(); num.push(t); break;
case '-': t = num.top(); num.pop(); t = num.top() - t; num.pop(); num.push(t); break;
case '*': t = num.top(); num.pop(); t *= num.top(); num.pop(); num.push(t); break;
case '/': t = num.top(); num.pop(); t = num.top() / t; num.pop(); num.push(t); break;
default: cerr << "输入表达书有误!" << endl; system("pause"); break;
}
}
}
res = num.top();
return res;
}
void jisuan()
{
string mid, sur;
cin >> mid;
if (mid.compare("NO") == 0 || mid.compare("No") == 0 || mid.compare("no") == 0) {
if (qiongju() == 0) result = 24;
else result=0;
return;
}
sur = shorten(mid);
result = calculate(sur);
}
void test1()
{
ofstream ofs("ranking1.txt", ios::out / ios::binary);
ofs.open("ranking1.txt", ios::out);
ofs.close();
}
void test2()
{
ofstream ofs("ranking2.txt", ios::out / ios::binary);
ofs.open("ranking2.txt", ios::out);
ofs.close();
}
void test3()
{
ofstream ofs("ranking3.txt", ios::out / ios::binary);
ofs.open("ranking3.txt", ios::out);
ofs.close();
}
void display1()
{
fstream myFile;
myFile.open("ranking1.txt", ios::in);
if (myFile.is_open())
{
string line;
while (getline(myFile, line))
{
cout << line << endl;
}
myFile.close();
}
}
void display2()
{
fstream myFile;
myFile.open("ranking2.txt", ios::in);
if (myFile.is_open())
{
string line;
while (getline(myFile, line))
{
cout << line << endl;
}
myFile.close();
}
}
void display3()
{
fstream myFile;
myFile.open("ranking3.txt", ios::in);
if (myFile.is_open())
{
string line;
while (getline(myFile, line))
{
cout << line << endl;
}
myFile.close();
}
}
int main()
{
srand((unsigned)time(0));//获取当前时间作为参数,生可以随时间变化的随机数
time_t begin, end;
double ret;
string name;
int choose,cho;
int flag = 1;
int life = 3;
int arr[4];
cout << " 24点游戏" << endl;
cout << " *---------------------*" << endl;
cout << " 1.开始游戏" << endl;
cout << " 2.结束游戏" << endl;
cout << " 3.查看排行" << endl;
cout << " *---------------------*" << endl;
cout << " 请输入编号选择功能:";
cin >> choose;
switch (choose)
{
case 1:
{
cout << " 难度选择" << endl;
cout << " *---------------------*" << endl;
cout << " 1.简单" << endl;
cout << " 2.中等" << endl;
cout << " 3.困难" << endl;
cout << " *---------------------*" << endl;
cout << " 请输入编号选择功能:";
cin >> cho;
cout << " 您游戏世间的名讳:" << endl;
cout << " > " ;
cin >> name;
//test1();
//test2();
//test3();
while (flag)
{
cout << " **剩余生命值为:" << life << endl;
cout << " **当前所得分数为:" << score << endl;
for (int i = 0; i < 4; i++)
{
arr[i] = rand() % 10;
}
begin = clock();//开始计时
GetCards(arr);
jisuan();
end = clock();//结束计时
ret = double(end - begin) / CLOCKS_PER_SEC;
if (ret > 300)
{
cout << " 你已经超过300s了!" << endl;
life = life - 1;
}
else
{
if (result == 24)
{
cout << " 计算成功!" << endl;
score = score + 10;
}
else
{
cout << " 计算出错!" << endl;
life = life - 1;
}
}
if (life == 0)
{
flag = 0;
cout << " 你已经用完了所有次数!" << endl;
}
}
cout << " 共计获得分数为:" << score << endl;
ofstream out1("./ranking1.txt", ios::app);
ofstream out2("./ranking2.txt", ios::app);
ofstream out3("./ranking3.txt", ios::app);
if (out1.fail()&&out2.fail()&&out3.fail())
{
cout << "error\n";
}
switch (cho)
{
case 1:
{
out1 << " " "name:" << name << " " << "score:" << score << endl;;
out1.close();
break;
}
case 2:
{
out2 << " ""name:" << name << " " << "score:" << score << endl;;
out2.close();
break;
}
case 3:
{
out3 <<" " "name:" << name << " " << "score:" << score << endl;;
out3.close();
break;
}
default:
break;
}
break;
}
case 2:
{
break;
}
case 3:
{
display1();
display2();
display3();
break;
}
default:
{
cout << "请输入1~3的数字!";
}
}
cout << "游玩愉快!";
return 0;
}