扑克牌24点小游戏

题目要求

基本要求

    随机生成4个代表扑克牌牌面的数字字母,程序自动列出所有可能算出24的表达式,用擅长的语言(C/C++/Java或其他均可)实现程序解决问题。
      1.程序风格良好(使用自定义注释模板)
      2.列出表达式无重复。

提高要求

   用户初始生命值为一给定值(比如3),初始分数为0。随机生成4个代表扑克牌牌面的数字或字母,由用户输入包含这4个数字或字母的运算表达式(可包含括号),如果表达式计算结果为24则代表用户赢了此局。
     1. 程序风格良好(使用自定义注释模板)
     2.使用计时器要求用户在规定时间内输入表达式,如果规定时间内运算正确则加分,超时或运算错误则进入下一题并减少生命值(不扣分)。
     3.所有成绩均可记录在TopList.txt文件中。

算法设计

扑克牌24点小游戏_第1张图片

程序代码

/*************************************************

Copyright:XUST

Author:   高雪峰

email:    [email protected]

Date:     2019-04-14

Description:实现24点游戏(提高)。

**************************************************/
#include 
#include 
#include 
#include 
#include 
#include
#include

using namespace std;
class TFpoint {
protected:
	int num[1][4];//存放随机数
	string str;//存放用户输入的表达式
	string answer;//记录所有能算出24的算法
	//string oc[24];
	int n = 0;//记录有多少组算法

public:
	TFpoint() {        //初始化对象并向用户提供四个随机数,获取用户的表达式
		int temp=0;
		//srand((unsigned)time(NULL));
		while (temp == 0) {
			num[n][0]= (rand() % (14 - 1)) + 1;
			num[n][1] = (rand() % (14 - 1)) + 1;
			num[n][2] = (rand() % (14 - 1)) + 1;
			num[n][3] = (rand() % (14 - 1)) + 1;
			if (cal(num[n][0], num[n][1], num[n][2], num[n][3]))
				temp++;
		}
		cout << num[0][0] << " " << num[0][1] << " " << num[0][2] << " " << num[0][3] << endl;
		//print();
		cout << "请输入:";
		cin >> str;
	}
	/*void print() {
		for(int i=n-1;i>=0;i--)
		cout << num[i][0] << " " << num[i][1] << " " << num[i][2] << " " << num[i][3]<
	int cal(int x, int y, int z, int w) //运算表达式的所有情况
	{
		//cout << "a";
		//int m = 0;
		if (x + y + z + w == 24) { 
			answer += "x + y + z + w == 24#";
			//oc[n] += x; oc[n] += '+'; oc[n] += y; oc[n] += '+'; oc[n] += z; oc[n] += '+'; oc[n] += w;
		    n++;
			//cout << x << "+" << y << "+" << z << "+" << w << "=24" << endl;
		} 
		 if (x + y + z - w == 24) {
			 answer += "x + y + z - w == 24#";
			//oc[n] += x; oc[n] += '+'; oc[n] += y; oc[n] += '+'; oc[n] += z; oc[n] += '-'; oc[n] += w;
			n++;
			//cout << x << "+" << y << "+" << z << "-" << w << "=24" << endl;
		} 
		 if ((x + y)*(z + w) == 24) {
			 answer += "(x + y)*(z + w) == 24#";
			//oc[n] += '('; oc[n] += x; oc[n] += '+'; oc[n] += y; oc[n] = ')'; oc[n] = '*'; oc[n] += '('; oc[n] += z; oc[n] += '+'; oc[n] += w; oc[n] += ')';
			n++;
			//cout << "(" << x << "+" << y << ")*(" << z << "+" << w << ")=24" << endl;
		}
		 if ((x - y)*(z + w) == 24) {
			 answer += "(x - y)*(z + w) == 24#";
			//oc[n] += '('; oc[n] += x; oc[n] += '-'; oc[n] += y; oc[n] = ')'; oc[n] = '*'; oc[n] += '('; oc[n] += z; oc[n] += '+'; oc[n] += w; oc[n] += ')';
			n++;
			//cout << "(" << x << "-" << y << ")*(" << z << "+" << w << ")=24" << endl;
		} 
		 if ((x - y)*(z - w) == 24) {
			 answer += "(x - y)*(z - w) == 24#";
			//oc[n] += '('; oc[n] += x; oc[n] += '-'; oc[n] += y; oc[n] = ')'; oc[n] = '*'; oc[n] += '('; oc[n] += z; oc[n] += '-'; oc[n] += w; oc[n] += ')';
			n++;
			//cout << "(" << x << "-" << y << ")*(" << z << "-" << w << ")=24" << endl;
		}
		 if ((x + y + z)*w == 24) {
			 answer += "(x + y + z)*w == 24#";
			//oc[n] += '('; oc[n] += x; oc[n] += '+'; oc[n] += y; oc[n] += '+';oc[n] += z; oc[n] = ')'; oc[n] = '*';  oc[n] += w; 
			n++;
			//cout << "(" << x << "+" << y << "+" << z << ")*" << w << "=24" << endl;
		}
		 if ((x - y - z)*w == 24) {
			 answer += "(x - y - z)*w == 24#";
			//oc[n] += '('; oc[n] += x; oc[n] += '-'; oc[n] += y; oc[n] += '-'; oc[n] += z; oc[n] = ')'; oc[n] = '*';  oc[n] += w; 
			n++;
			//cout << "(" << x << "-" << y << "-" << z << ")*" << w << "=24" << endl;
		}
		 if ((x + y - z)*w == 24) {
			 answer += "(x + y - z)*w == 24#";
			//oc[n] += '('; oc[n] += x; oc[n] += '+'; oc[n] += y; oc[n] += '-'; oc[n] += z; oc[n] = ')'; oc[n] = '*';  oc[n] += w; 
			n++;
			//cout << "(" << x << "+" << y << "-" << z << ")*" << w << "=24" << endl;
		} 
		 if ((x*y*z) / w == 24) {
			 answer += "(x*y*z) / w == 24#";
			//oc[n] += '('; oc[n] += x; oc[n] += '*'; oc[n] += y; oc[n] += '*'; oc[n] += z; oc[n] = ')'; oc[n] = '/';  oc[n] += w; 
			n++;
			//cout << "(" << x << "*" << y << "*" << z << ")/" << w << "=24" << endl;
		} 
		 if ((x*y)*(z + w) == 24) {
			 answer += "(x*y)*(z + w) == 24#";
			//oc[n] += '('; oc[n] += x; oc[n] += '*'; oc[n] += y; oc[n] = ')';oc[n] += '*'; oc[n] += '('; oc[n] += z;  oc[n] = '+';  oc[n] += w; oc[n] = ')';
			n++;
			//cout << "(" << x << "*" << y << ")*(" << z << "+" << w << ")=24" << endl;
		} 
		 if ((x*y)*(z - w) == 24) {
			 answer += "(x*y)*(z - w) == 24#";
			//oc[n] += '('; oc[n] += x; oc[n] += '*'; oc[n] += y; oc[n] = ')'; oc[n] += '*'; oc[n] += '('; oc[n] += z;  oc[n] = '-';  oc[n] += w; oc[n] = ')';
			n++;
			//cout << "(" << x << "*" << y << ")*(" << z << "-" << w << ")=24" << endl;
		} 
		 if ((x*y)*z - w == 24) {
			 answer += "(x*y)*z - w == 24#";
			//oc[n] += '('; oc[n] += x; oc[n] += '*'; oc[n] += y; oc[n] = ')'; oc[n] += '*';  oc[n] += z;  oc[n] = '-';  oc[n] += w; 
			n++;
			//cout << "(" << x << "*" << y << ")*" << z << "-" << w << "=24" << endl;
		} 
		 if ((x*y)*z + w == 24) {
			 answer += "(x*y)*z + w == 24#";
			 //oc[n] += '('; oc[n] += x; oc[n] += '*'; oc[n] += y; oc[n] = ')'; oc[n] += '*';  oc[n] += z;  oc[n] = '+';  oc[n] += w;
			 n++;
			// cout << "(" << x << "*" << y << ")*" << z << "+" << w << "=24" << endl;
		 }
		 if (x*y*z*w == 24) {
			 answer += "x*y*z*w == 24#";
			 //oc[n] += x; oc[n] += '*'; oc[n] += y; oc[n] += '*'; oc[n] += z; oc[n] += '*'; oc[n] += w;
			 n++;
			// cout << x << "*" << y << "*" << z << "*" << w << "=24" << endl;
		 }
		 if ((x + y) + (z / w) == 24) {
			 answer += "(x + y) + (z / w) == 24#";
			 //oc[n] += '('; oc[n] += x; oc[n] += '+'; oc[n] += y; oc[n] = ')'; oc[n] += '+'; oc[n] += '('; oc[n] += z;  oc[n] = '/';  oc[n] += w; oc[n] = ')';
			 n++;
			// cout << "(" << x << "+" << y << ")+(" << z << "/" << w << ")" << "=24" << endl;
		 }
		 if ((x + y)*(z / w) == 24) {
			 answer += "(x + y)*(z / w) == 24#";
			 //oc[n] += '('; oc[n] += x; oc[n] += '+'; oc[n] += y; oc[n] = ')'; oc[n] += '*'; oc[n] += '('; oc[n] += z;  oc[n] = '/';  oc[n] += w; oc[n] = ')';
			 n++;
			// cout << "(" << x << "+" << y << ")*(" << z << "/" << w << ")" << "=24" << endl;
		 }
		 if ((x*y) + z + w == 24) {
			 answer += "(x*y) + z + w == 24#";
			 //oc[n] += '('; oc[n] += x; oc[n] += '*'; oc[n] += y; oc[n] = ')'; oc[n] += '+';  oc[n] += z;  oc[n] = '+';  oc[n] += w;
			 n++;
			 //cout << "(" << x << "*" << y << ")+" << z << "+" << w << "=24" << endl;
		 }
		 if ((x*y) + z - w == 24) {
			 answer += "(x*y) + z - w == 24#";
			 //oc[n] += '('; oc[n] += x; oc[n] += '*'; oc[n] += y; oc[n] = ')'; oc[n] += '+';  oc[n] += z;  oc[n] = '-';  oc[n] += w;
			 n++;
			 //cout << "(" << x << "*" << y << ")+" << z << "-" << w << "=24" << endl;
		 }
		 if ((x*y) - (z / w) == 24) {
			 answer += "(x*y) - (z / w) == 24#";
			 //oc[n] += '('; oc[n] += x; oc[n] += '*'; oc[n] += y; oc[n] = ')'; oc[n] += '-'; oc[n] += '('; oc[n] += z;  oc[n] = '/';  oc[n] += w; oc[n] = ')';
			 n++;
			// cout << "(" << x << "*" << y << ")-(" << z << "/" << w << ")" << "=24" << endl;
		 }
		 if ((x*y) + (z / w) == 24) {
			 answer += "(x*y) + (z / w) == 24#";
			 //oc[n] += '('; oc[n] += x; oc[n] += '*'; oc[n] += y; oc[n] = ')'; oc[n] += '+'; oc[n] += '('; oc[n] += z;  oc[n] = '/';  oc[n] += w; oc[n] = ')';
			 n++;
			 //cout << "(" << x << "*" << y << ")+(" << z << "/" << w << ")" << "=24" << endl;
		 }
		 if ((x*y) - z - w == 24) {
			 answer += "(x*y) - z - w == 24#";
			 //oc[n] += '('; oc[n] += x; oc[n] += '*'; oc[n] += y; oc[n] = ')'; oc[n] += '-';  oc[n] += z;  oc[n] = '-';  oc[n] += w;
			 n++;
			// cout << "(" << x << "*" << y << ")-" << z << "-" << w << "=24" << endl;
		 }
		 if ((x*y) + (z*w) == 24) {
			 answer += "(x*y) + (z*w) == 24#";
			 //oc[n] += '('; oc[n] += x; oc[n] += '*'; oc[n] += y; oc[n] = ')'; oc[n] += '+'; oc[n] += '('; oc[n] += z;  oc[n] = '*';  oc[n] += w; oc[n] = ')';
			 n++;
			 //cout << "(" << x << "*" << y << ")+(" << z << "*" << w << ")" << "=24" << endl;
		 }
		 if ((x*y) - (z*w) == 24) {
			 answer += "(x*y) - (z*w) == 24#";
			 n++;
			 //cout << "(" << x << "*" << y << ")-(" << z << "*" << w << ")" << "=24" << endl;
		 }
		 if ((x*y) / (z*w) == 24) {
			 answer += "(x*y) / (z*w) == 24#";
			 n++;
			 //cout << "(" << x << "*" << y << ")/(" << z << "*" << w << ")" << "=24" << endl;
		 }
		 if ((x*y) / (z - w) == 24) {
			 answer += "(x*y) / (z - w) == 24#";
			 n++;
			 //cout << "(" << x << "*" << y << ")/(" << z << "-" << w << ")" << "=24" << endl;
		 }
		 if ((x*y) / (z + w) == 24) {
			 answer += "(x*y) / (z + w) == 24#";
			 n++;
			 
			 //cout << "(" << x << "*" << y << ")/(" << z << "+" << w << ")" << "=24" << endl;
		 }
		 if(n==0)
		return 0;
		 else
		return 1;
	}
};
class Calexpression: public TFpoint{//继承TFpoint类,实现计算表达式操作
public:
	Calexpression() {
	}
	/*
	 *比较两个操作符的优先级,op1比op2高返回1,一样返回0,低返回-1
	*/
	int compareOpe(char op1, char op2) {
		switch (op1) {
		case '+': case '-':
			return (op2 == '*' || op2 == '/' ? -1 : 0);
			break;
		case '*': case '/':
			return (op2 == '-' || op2 == '+' ? 1 : 0);
		}
		return -1;
	}
	/*
	 *判断是否为数字
	 */
	bool isNum(char c) {
		int a = c - '0';
		if (a >= 0 && a <= 9) {
			return true;
		}
		else {
			return false;
		}
	}
	/*
	 *判断是否为操作符
	 */
	bool isOperator(char c) {
		if (c == '+' || c == '-' || c == '*' || c == '/') {
			return true;
		}
		else {
			return false;
		}
	}
	/*
	 *得到后缀表达式
	*/
	string getSuffixExpression() {
		stack<char> numAndOpe;
		stack<char> ope;
		//开始遍历字符串
		for (int i = 0; i <(int) str.length(); i++) {
			if (isNum(str[i])) {
				if (isNum(str[i + 1]) ){
					numAndOpe.push('#');
					numAndOpe.push(str[i]);
					numAndOpe.push(str[i+1]);
					numAndOpe.push('#');
						i++;
				}
				else
				numAndOpe.push(str[i]);//如果是数字,则直接进入栈numAndOpe
			}
			else if (isOperator(str[i])) {//如果是操作符
				if (ope.empty() || ope.top() == '(') {//若栈空或栈顶为(
					ope.push(str[i]);
				}
				else if (compareOpe(str[i], ope.top()) > 0) {//若操作符比栈顶操作符优先级高
					ope.push(str[i]);
				}
				else {
					while (true) {
						numAndOpe.push(ope.top());
						ope.pop();
						if (ope.empty() || ope.top() == '(') {
							ope.push(str[i]);
							break;
						}
						else if (compareOpe(str[i], ope.top()) > 0) {
							ope.push(str[i]);
							break;
						}
					}
				}
			}
			else if (str[i] == '(') {
				ope.push(str[i]);
			}
			else if (str[i] == ')') {
				while (ope.top() != '(') {
					numAndOpe.push(ope.top());
					ope.pop();
				}
				ope.pop();//丢掉'('
			}
			else {
				cout << "错误输入" << endl;
				exit(-1);
			}
		}

		while (!ope.empty()) {
			numAndOpe.push(ope.top());
			ope.pop();
		}

		string suffix = string(numAndOpe.size(), 'a');
		int i = 0;
		while (!numAndOpe.empty()) {
			suffix[i] = numAndOpe.top();
			numAndOpe.pop();
			i++;
		}
		reverse(begin(suffix), end(suffix));
		return suffix;
	}
	/*
	*根据后缀表达式计算
	*/
	int calculate(string str) {
		stack<int> numbers;
		for (int i = 0; i <(int) str.length(); i++) {
			if (isNum(str[i])||str[i]=='#') {
				if (str[i] == '#') {
					i++;
					numbers.push((str[i]-'0')*10+(str[i+1]-'0'));
					i += 2;

				}
				else
				numbers.push(str[i] - '0');
			}
			else if (isOperator(str[i])) {//遇到操作符,取出栈顶2个数字,并计算,计算结果压入栈

				int a = numbers.top();
				numbers.pop();
				int b = numbers.top();
				numbers.pop();
				int tempResult;
				if (str[i] == '+') {
					tempResult = b + a;
				}
				else if (str[i] == '-') {
					tempResult = b - a; 
				}
				else if (str[i] == '*') {
					tempResult = b * a;
				}
				else if (str[i] == '/') {
					tempResult = b / a;
				}
				numbers.push(tempResult);
			}
		}
		return numbers.top();
	}
};
int main() {
	int i = 0, temp = 3,a,score=0;
	clock_t start, finish;//设置计时器
	//double duration;
	while (1) {
		int n=1;
		cout << "你现在的生命值:" << temp << "(答题超过60s算作失败扣除一点,生命值少于1点游戏结束!)" << endl;
		cout << "1、开始游戏" << endl << "2、结束" << endl << "请选择:";
		cin >> a;
		if (a == 1) {
			for (;;) {//进行循环游戏
				start = clock();
				cout << endl;
				cout <<endl<< "生命值:"<<temp<< "分数为:"<<score<<endl;
				cout << n << "、:";
				Calexpression c1;
				
				cout <<"结果:"<< c1.calculate(c1.getSuffixExpression())<<endl;
				finish = clock();
				n++;
				if (c1.calculate(c1.getSuffixExpression()) != 24) {
					cout << "答案错误!";
                    temp--;
					//cout << temp << "分数为:"<
				}	
				else if ((double)(finish - start) / CLOCKS_PER_SEC > 60) {//输入表达式正确后若时间大于60s则超时扣除生命
					cout << "超时!";
					temp--;
					//cout << temp << "分数为:" << score << endl;
				}
				else {
					score += 1000;
				}
				if (temp == 0) {
					FILE *fp;
					fp = fopen("TopList.txt", "at");
					fprintf(fp, "结果分数:%d\n", score);
					fclose(fp);
					break;
				}
			}
		}
		else if (a == 2)
			return 0;
		else
			cout << "请在1,2中选择!" << endl;
		
		if (temp == 0) {
			cout << "游戏结束!重玩请输入1" << endl;
			int x;
			cin >> x;
			if (x != 1)
				break;
		}
		
	}
	
	return 0;
	system("pause");
}

测试

扑克牌24点小游戏_第2张图片
扑克牌24点小游戏_第3张图片

你可能感兴趣的:(程序优化设计作业)