#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cstring>
#include <fstream>
int JUDGE=0;
const int SIZE = 100; // 栈的初始容量
const int ADDSIZE = 5; // 栈的单位增加容量
using namespace std;// 命名空间
const int INF = -0xfffffff;// 用于空栈时防止尴尬,返回一个不能读取的地址,让读取的人尴尬;
int JUDGE1(int a,int b,int c)
{
int i=1,sum,flag=0;
switch(i){
case 1:
sum=a+b;
sum=sum+c;
if(sum==24||sum==-24){
break;
}
case 2:
sum=a+b;
sum=sum-c;
if(sum==24||sum==-24){
break;
}
case 3:
sum=a+b;
sum=sum*c;
if(sum==24||sum==-24){
break;
}
case 4:
sum=a+b;
sum=sum/c;
if(sum==24||sum==-24){
break;
}
case 5:
sum=a-b;
sum=sum+c;
if(sum==24||sum==-24){
break;
}
case 6:
sum=a-b;
sum=sum-c;
if(sum==24||sum==-24){
break;
}
case 7:
sum=a-b;
sum=sum*c;
if(sum==24||sum==-24){
break;
}
case 8:
sum=a-b/c;
sum=sum/c;
if(sum==24||sum==-24){
break;
}
case 9:
sum=a*b;
sum=sum+c;
if(sum==24||sum==-24){
break;
}
case 10:
sum=a*b;
sum=sum-c;
if(sum==24||sum==-24){
break;
}
case 11:
sum=a*b;
sum=sum*c;
if(sum==24||sum==-24){
break;
}
case 12:
sum=a*b;
sum=sum/c;
if(sum==24||sum==-24){
break;
}
case 13:
sum=a/b;
sum=sum+c;
if(sum==24||sum==-24){
break;
}
case 14:
sum=a/b;
sum=sum-c;
if(sum==24||sum==-24){
break;
}
case 15:
sum=a/b;
sum=sum*c;
if(sum==24||sum==-24){
break;
}
case 16:
sum=a/b;
sum=sum/c;
if(sum==24||sum==-24){
break;
}
case 17:
flag=1;
break;
}
if(flag) {
return 0;
}
else {
return 1;
}
}
int JUDGE2(int a,int b,int c)
{
int i=1,sum,flag=0;
switch(i){
case 1:
sum=a+(b+c);
if(sum==24||sum==-24){
break;
}
case 2:
sum=a+(b-c);
if(sum==24||sum==-24){
break;
}
case 3:
sum=a+b*c;
if(sum==24||sum==-24){
break;
}
case 4:
sum=a+b/c;
if(sum==24||sum==-24){
break;
}
case 5:
sum=a-b+c;
if(sum==24||sum==-24){
break;
}
case 6:
sum=a-b-c;
if(sum==24||sum==-24){
break;
}
case 7:
sum=a-b*c;
if(sum==24||sum==-24){
break;
}
case 8:
sum=a-b/c;
if(sum==24||sum==-24){
break;
}
case 9:
sum=a*(b+c);
if(sum==24||sum==-24){
break;
}
case 10:
sum=a*(b-c);
if(sum==24||sum==-24){
break;
}
case 11:
sum=a*(b*c);
if(sum==24||sum==-24){
break;
}
case 12:
sum=a*(b/c);
if(sum==24||sum==-24){
break;
}
case 13:
sum=a/(b+c);
if(sum==24||sum==-24){
break;
}
case 14:
if(b-c)
sum=a/(b-c);
if(sum==24||sum==-24){
break;
}
case 15:
sum=a/b*c;
if(sum==24||sum==-24){
break;
}
case 16:
if(b/c)
sum=a/b/c;
if(sum==24||sum==-24){
break;
}
case 17:
flag=1;
break;
}
if(flag) {
return 0;
}
else {
return 1;
}
}
int Judge(int a,int b,int c,int d)
{
int judge;
judge=JUDGE1(a+b,c,d);
if(!judge){
judge=JUDGE1(a-b,c,d);
}
if(!judge){
judge=JUDGE1(a*b,c,d);
}
if(!judge){
judge=JUDGE1(a/b,c,d);
}
if(!judge){
judge=JUDGE2(a+b,c,d);
}
if(!judge){
judge=JUDGE2(a-b,c,d);
}
if(!judge){
judge=JUDGE2(a*b,c,d);
}
if(!judge){
judge=JUDGE2(a/b,c,d);
}
return judge;
}
void swap(int a[],int i,int j)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void perm(int a[],int p,int q)
{ int i;
if(p==q){
if(Judge(a[0],a[1],a[2],a[3])){
JUDGE=1;
return ;
}
}
else{
for(i=p;i<q+1;i++){
swap(a,p,i);
perm(a,p+1,q);
swap(a,p,i);
}
}
}
void CardTaking(int a[])
{
int i;
srand((unsigned)time(NULL));
for(i = 0; i < 4; i++){
a[i]=(rand() % 13)+1;
}
}
template <class T>
struct Strack
{
T *top;// 栈顶的指针
T *base;// 栈底的指针
int strackSize;//栈容量
void init()//栈的初始化
{
base = (T *)malloc(SIZE * sizeof(T));//分配内存
top = base;
strackSize = SIZE;
}
T Top()
{// 返回栈顶元素
if (top == base)
return INF;// 返回尴尬地址
return *(top - 1);
}
bool pop()
{// 删除栈顶元素
if (top == base)
return false;
top--;
return true;
}
void push(T x)
{//栈顶插入元素
if (isPull()) {//如果内存不够重新分配内存
base = (T *)realloc(base, (strackSize + ADDSIZE)*(sizeof(T)));
top = base + strackSize;
strackSize += ADDSIZE;
}
*top++ = x;
}
bool isEmpty()
{//判断栈是否为空
if (top == base)
return true;
return false;
}
bool isPull()//判满函数
{
if (top - base == strackSize)// 一次push一个所以当top - base = strackSize时满
return true;
else
return false;
}
};
int compareNumber(char x)//用来比较符号的优先级
{
if (x == '+' || x == '-')
return 0;
if (x == '*' || x == '/')
return 1;
return -1;
}
void change(char *mid, char *suxfix)//中缀表达式转换为后缀表达式
{
int i = 0, len = strlen(mid), mid_index = 0;
char c;
Strack<char >intElements;// int数据集
intElements.init();
intElements.push('#');// 特殊符号用来区分元素
while (i < len)
{
if (mid[i] == '(')
{
intElements.push(mid[i]);
i++;
}
else if (mid[i] == ')')
{
while (intElements.Top() != '(')
{
suxfix[mid_index++] = intElements.Top();
suxfix[mid_index++] = ' ';
intElements.pop();
}
intElements.pop();
i++;
}
else if (mid[i] == '+' || mid[i] == '-' || mid[i] == '/' || mid[i] == '*')
{
while (compareNumber(intElements.Top()) >= compareNumber(mid[i]))
{
suxfix[mid_index++] = intElements.Top();
suxfix[mid_index++] = ' ';
intElements.pop();
}
intElements.push(mid[i]);
i++;
}
else if (mid[i] >= '0'&&mid[i] <= '9')
{
while (mid[i] >= '0'&&mid[i] <= '9')
{
suxfix[mid_index++] = mid[i];
i++;
}
suxfix[mid_index++] = ' ';
}
else
{
}
}
while (intElements.Top() != '#') {
suxfix[mid_index++] = intElements.Top();
suxfix[mid_index++] = ' ';
intElements.pop();
}
suxfix[mid_index] = 0;
}
double countSuxfix(char *suxfix)//计算后缀表达式 suxfix后缀表达式
{
int len = strlen(suxfix);
double x;
Strack <int >intElements;
intElements.init();
int i = 0;
while (i<len) {
if (suxfix[i] == ' ')// 用于表达式有效字符集用完跳出循环
{
i++;
continue;
}
switch (suxfix[i])
{
case '+':
x = intElements.Top();//根据原理图看
intElements.pop();
x = intElements.Top() + x;
intElements.pop(); i++;
break;
case '-':
x = intElements.Top();
intElements.pop();
x = intElements.Top() - x;
intElements.pop();
i++;
break;
case '*':
x = intElements.Top();
intElements.pop();
x = intElements.Top()*x;
intElements.pop();
i++;
break;
case '/':
x = intElements.Top();
intElements.pop();
x = intElements.Top() / x;
intElements.pop();
i++;
break;
default:
{
x = 0;
while (suxfix[i] >= '0'&&suxfix[i] <= '9') {
x = x * 10 + suxfix[i] - '0';// 连续2个数,第一个*10,以此类推
i++;
}
}
}
intElements.push(x);
}
return intElements.Top();
}
void Newterm( int a[])
{
}
int main(void)
{
int i,poker[4],result,live=3,score=0,int BestScore;
FILE * fpreserve;
fpreserve=fopen("result.txt","rt+ ");
fscanf(fpreserve,"%d",&BestScore);
char str[1000];
char newStr[1000];
while(live){
while(!JUDGE){
CardTaking(poker);
perm(poker,0,3);
}
for(i=0;i<4;i++){
printf("%5d",poker[i]);
}
printf("\n");
scanf("%s", str);
change(str, newStr);
if(countSuxfix(newStr)==24){
printf("you get it!\n");
score++;
printf("lives:%-10dscore:%-10dBestScore:%-10d\n",live,score,BestScore);
printf("-----------------------------------------------------\n");
}
else{
live--;
printf("you answered wrong!\n");
printf("lives:%-10dscore:%-10dBestScore:%-10d\n",live,score,BestScore);
printf("-----------------------------------------------------\n");
}
JUDGE=0;
}
if(score>BestScore){
printf("New Record!\n");
rewind(fpreserve);
fprintf(fpreserve,"%d",score);
}
printf("defect!please play again!\n");
fclose(fpreserve);
return 0;
}
24点游戏
最新推荐文章于 2019-09-25 18:07:55 发布