24点游戏改进版---支持出题,解题运算
2011-10-17 14:29
330 查看
// test11.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "string"
#include "ctime"
#include "cstdlib"
#include "stack.h"
#include "vector"
#include "cctype"
using namespace std;
int const MAXSIZE=100;
using namespace std;
int i=0;
int num[4];
void back(float a,float b,float c,float d,int j);
void add(float a,float b,float c,float d,int j)
{
if ((a*b!=0)&&(j<4)&&(i!=1))
{
back(a+b,c,d,0,j+1);
//if (i==1)
//cout<<(a+b)<<"="<<a<<"+"<<b<<endl;
}
}
void sub(float a,float b,float c,float d,int j)
{
if ((a*b!=0)&&(j<4)&&(i!=1))
{
back(a-b,c,d,0,j+1);
back(b-a,c,d,0,j+1);
//if (i==1)
//cout<<(a-b)<<"="<<a<<"-"<<b<<endl;
}
}
void mul(float a,float b,float c,float d,int j)
{
if ((a*b!=0)&&(j<4)&&(i!=1))
{
back(a*b,c,d,0,j+1);
//if (i==1)
// cout<<(a*b)<<"="<<a<<"*"<<b<<endl;
}
}
void divi(float a,float b,float c,float d,int j)
{
if ((a*b!=0)&&(j<4)&&(i!=1))
{
back(a/b,c,d,0,j+1);
//if (i==1)
//cout<<(a/b)<<"="<<a<<"/"<<b<<endl;
}
}
void back(float a,float b,float c,float d,int j)
{
add(a,b,c,d,j);
mul(a,b,c,d,j);
sub(a,d,b,c,j);
divi(a,d,b,c,j);
add(a,c,b,d,j);
mul(a,c,b,d,j);
add(a,d,c,b,j);
mul(a,d,c,b,j);
sub(a,b,c,d,j);
divi(a,b,c,d,j);
sub(b,a,c,d,j);
divi(b,a,c,d,j);
sub(d,a,b,c,j);
divi(d,a,b,c,j);
sub(a,c,b,d,j);
divi(a,c,b,d,j);
if ((j==3)&&(a+b+c+d==24))
i=1;
}
void rand_num()
{
while(i==0)
{
srand(time(0));
for (int j=0;j<4;j++)
{
num[j]=rand()%10+1;
while(num[j]==10)
num[j]=rand()%10+1;
}
if (i==0)
back(num[0],num[1],num[2],num[3],0);
}
}
bool todouble(string s1)
{
string s=s1;
return false;
}
template<typename T> class stack
{
public:
stack(int size);
stack();
~stack();
void push(T);
T pop();
T getTop();
bool isEmpty();
bool isFull();
void clear();
private:
T* list;
int top;
int maxsize;
};
template<typename T> stack<T>::stack(int size)
{
list = new T[size];
maxsize = size;
top=0;
}
template<typename T> stack<T>::stack()
{
list = new T[MAXSIZE];
maxsize = MAXSIZE;
top=0;
}
template<typename T> stack<T>::~stack()
{
delete []list; //---------------------select the delete and delete[] different
//delete list;
}
template<typename T> void stack<T>::push(T elem)
{
if (isFull())
{
cerr<<"stack overflows!\n";
exit(0);
}
list[top]=elem;
top++;
}
template<typename T> T stack<T>::pop()
{
if (isEmpty())
{
cerr<<"stack is empty!\n";
exit(0);
}
top--;
return list[top];
}
template<typename T> T stack<T>::getTop()
{
if(isEmpty())
{
cerr<<"stack is empty!\n";
exit(0);
}
return list[top-1];
}
template<typename T> bool stack<T>::isEmpty()
{
if(top==0)
return true;
else
return false;
}
template<typename T> bool stack<T>::isFull()
{
if(top==maxsize)
return true;
return false;
}
template<typename T> void stack<T>::clear()
{
top=0;
}
string infixtosuffix(string infix)
{
stack<char> stack(infix.size());
string post;
for (string::iterator iter = infix.begin();iter!=infix.end();++iter)
{
if(isdigit(*iter))
//判断是否是数字
post.append(1,*iter);
//在post后面接上一个*iter
else
{
switch(*iter)
{
case '(':
stack.push(*iter);
break;
case ')':
if (stack.isEmpty())
{
cerr<<"format error\n";
exit(0);
}
while(stack.getTop()!='(')
{
if(!post.empty()&&isdigit(post.at(post.size()-1)))
post.append(" ");
post.append(1,stack.pop());
}
stack.pop();
break;
case '+':
case '-':
post.append(" ");
while(!stack.isEmpty()&&stack.getTop()!='(')
post.append(1,stack.pop());
stack.push(*iter);
break;
case '*':
case '/':
post.append(" ");
while (!stack.isEmpty() && stack.getTop()!='('
&& (stack.getTop()=='*'||stack.getTop()=='/'))
post.append(1,stack.pop());
stack.push(*iter);
break;
default:
cerr<<"format error!\n";
exit(0);
}
}
}
while (!stack.isEmpty())
{
if(stack.getTop()=='(')
{
cerr<<"format error!\n";
exit(0);
}
if(!post.empty()&&isdigit(post.at(post.size()-1)))
post.append(" ");
post.append(1,stack.pop());
}
return post;
}
double calculation(string post)
{
stack<double> stack(post.size());
string stroperation;
double doperation;
for (string::iterator iter = post.begin();iter!=post.end();++iter)
{
if(isdigit(*iter))
stroperation.append(1,*iter);
else if (*iter==' ')
{
if(!isdigit(*(iter-1)))
continue;
doperation = atoi(stroperation.c_str());
stack.push(doperation);
stroperation="";
//----------------------------------------
}
else
{
int op2=stack.pop();
int op1=stack.pop();
switch(*iter)
{
case '+':
stack.push(op1+op2);
break;
case '-':
stack.push(op1-op2);
break;
case '*':
stack.push(op1*op2);
break;
case '/':
stack.push(op1/op2);
break;
}
}
}
if (stack.isEmpty())
{
cout<<"format error\n";
exit(0);
}
return stack.pop();
}
void main()
{
char is_continue='y';
while(is_continue=='y')
{
string answers;
double results;
string post;
rand_num();
if(i==1)
cout<<"the num is: "<<num[0]<<" "<<num[1]<<" "<<num[2]<<" "<<num[3]<<endl;
i=0;
cout<<"your answer: ";
cin>>answers;
int n=0;
for(string::iterator it=answers.begin();it!=answers.end();++it)
{
if (isdigit(*it))
{
for (int j=0;j<4;j++)
{
if(num[j]==atoi(it))
{
num[j]=-1;
n++;
}
}
}
}
if (n==4)
{
post = infixtosuffix(answers);
results=calculation(post);
cout<<"the results is: "<<results<<" ";
if (results==24)
cout<<"you are right!"<<endl;
else
cout<<"you are worry"<<endl;
}
else
cout<<"you must use the num list!"<<endl;
cout<<"are you continue? y or n:";
cin>>is_continue;
}
}
//
#include "stdafx.h"
#include "iostream"
#include "string"
#include "ctime"
#include "cstdlib"
#include "stack.h"
#include "vector"
#include "cctype"
using namespace std;
int const MAXSIZE=100;
using namespace std;
int i=0;
int num[4];
void back(float a,float b,float c,float d,int j);
void add(float a,float b,float c,float d,int j)
{
if ((a*b!=0)&&(j<4)&&(i!=1))
{
back(a+b,c,d,0,j+1);
//if (i==1)
//cout<<(a+b)<<"="<<a<<"+"<<b<<endl;
}
}
void sub(float a,float b,float c,float d,int j)
{
if ((a*b!=0)&&(j<4)&&(i!=1))
{
back(a-b,c,d,0,j+1);
back(b-a,c,d,0,j+1);
//if (i==1)
//cout<<(a-b)<<"="<<a<<"-"<<b<<endl;
}
}
void mul(float a,float b,float c,float d,int j)
{
if ((a*b!=0)&&(j<4)&&(i!=1))
{
back(a*b,c,d,0,j+1);
//if (i==1)
// cout<<(a*b)<<"="<<a<<"*"<<b<<endl;
}
}
void divi(float a,float b,float c,float d,int j)
{
if ((a*b!=0)&&(j<4)&&(i!=1))
{
back(a/b,c,d,0,j+1);
//if (i==1)
//cout<<(a/b)<<"="<<a<<"/"<<b<<endl;
}
}
void back(float a,float b,float c,float d,int j)
{
add(a,b,c,d,j);
mul(a,b,c,d,j);
sub(a,d,b,c,j);
divi(a,d,b,c,j);
add(a,c,b,d,j);
mul(a,c,b,d,j);
add(a,d,c,b,j);
mul(a,d,c,b,j);
sub(a,b,c,d,j);
divi(a,b,c,d,j);
sub(b,a,c,d,j);
divi(b,a,c,d,j);
sub(d,a,b,c,j);
divi(d,a,b,c,j);
sub(a,c,b,d,j);
divi(a,c,b,d,j);
if ((j==3)&&(a+b+c+d==24))
i=1;
}
void rand_num()
{
while(i==0)
{
srand(time(0));
for (int j=0;j<4;j++)
{
num[j]=rand()%10+1;
while(num[j]==10)
num[j]=rand()%10+1;
}
if (i==0)
back(num[0],num[1],num[2],num[3],0);
}
}
bool todouble(string s1)
{
string s=s1;
return false;
}
template<typename T> class stack
{
public:
stack(int size);
stack();
~stack();
void push(T);
T pop();
T getTop();
bool isEmpty();
bool isFull();
void clear();
private:
T* list;
int top;
int maxsize;
};
template<typename T> stack<T>::stack(int size)
{
list = new T[size];
maxsize = size;
top=0;
}
template<typename T> stack<T>::stack()
{
list = new T[MAXSIZE];
maxsize = MAXSIZE;
top=0;
}
template<typename T> stack<T>::~stack()
{
delete []list; //---------------------select the delete and delete[] different
//delete list;
}
template<typename T> void stack<T>::push(T elem)
{
if (isFull())
{
cerr<<"stack overflows!\n";
exit(0);
}
list[top]=elem;
top++;
}
template<typename T> T stack<T>::pop()
{
if (isEmpty())
{
cerr<<"stack is empty!\n";
exit(0);
}
top--;
return list[top];
}
template<typename T> T stack<T>::getTop()
{
if(isEmpty())
{
cerr<<"stack is empty!\n";
exit(0);
}
return list[top-1];
}
template<typename T> bool stack<T>::isEmpty()
{
if(top==0)
return true;
else
return false;
}
template<typename T> bool stack<T>::isFull()
{
if(top==maxsize)
return true;
return false;
}
template<typename T> void stack<T>::clear()
{
top=0;
}
string infixtosuffix(string infix)
{
stack<char> stack(infix.size());
string post;
for (string::iterator iter = infix.begin();iter!=infix.end();++iter)
{
if(isdigit(*iter))
//判断是否是数字
post.append(1,*iter);
//在post后面接上一个*iter
else
{
switch(*iter)
{
case '(':
stack.push(*iter);
break;
case ')':
if (stack.isEmpty())
{
cerr<<"format error\n";
exit(0);
}
while(stack.getTop()!='(')
{
if(!post.empty()&&isdigit(post.at(post.size()-1)))
post.append(" ");
post.append(1,stack.pop());
}
stack.pop();
break;
case '+':
case '-':
post.append(" ");
while(!stack.isEmpty()&&stack.getTop()!='(')
post.append(1,stack.pop());
stack.push(*iter);
break;
case '*':
case '/':
post.append(" ");
while (!stack.isEmpty() && stack.getTop()!='('
&& (stack.getTop()=='*'||stack.getTop()=='/'))
post.append(1,stack.pop());
stack.push(*iter);
break;
default:
cerr<<"format error!\n";
exit(0);
}
}
}
while (!stack.isEmpty())
{
if(stack.getTop()=='(')
{
cerr<<"format error!\n";
exit(0);
}
if(!post.empty()&&isdigit(post.at(post.size()-1)))
post.append(" ");
post.append(1,stack.pop());
}
return post;
}
double calculation(string post)
{
stack<double> stack(post.size());
string stroperation;
double doperation;
for (string::iterator iter = post.begin();iter!=post.end();++iter)
{
if(isdigit(*iter))
stroperation.append(1,*iter);
else if (*iter==' ')
{
if(!isdigit(*(iter-1)))
continue;
doperation = atoi(stroperation.c_str());
stack.push(doperation);
stroperation="";
//----------------------------------------
}
else
{
int op2=stack.pop();
int op1=stack.pop();
switch(*iter)
{
case '+':
stack.push(op1+op2);
break;
case '-':
stack.push(op1-op2);
break;
case '*':
stack.push(op1*op2);
break;
case '/':
stack.push(op1/op2);
break;
}
}
}
if (stack.isEmpty())
{
cout<<"format error\n";
exit(0);
}
return stack.pop();
}
void main()
{
char is_continue='y';
while(is_continue=='y')
{
string answers;
double results;
string post;
rand_num();
if(i==1)
cout<<"the num is: "<<num[0]<<" "<<num[1]<<" "<<num[2]<<" "<<num[3]<<endl;
i=0;
cout<<"your answer: ";
cin>>answers;
int n=0;
for(string::iterator it=answers.begin();it!=answers.end();++it)
{
if (isdigit(*it))
{
for (int j=0;j<4;j++)
{
if(num[j]==atoi(it))
{
num[j]=-1;
n++;
}
}
}
}
if (n==4)
{
post = infixtosuffix(answers);
results=calculation(post);
cout<<"the results is: "<<results<<" ";
if (results==24)
cout<<"you are right!"<<endl;
else
cout<<"you are worry"<<endl;
}
else
cout<<"you must use the num list!"<<endl;
cout<<"are you continue? y or n:";
cin>>is_continue;
}
}
相关文章推荐
- 【转载】数学游戏(mathgame)解题方法 - N皇后位运算解法,14皇后亦在1秒中
- Cdoj 24点游戏之解题报告
- 有兴趣一起改进subsonic2吗?使其支持位运算与空间运算。
- 数学游戏(mathgame)解题方法 - N皇后位运算解法,14皇后亦在1秒中
- 【开源】猜数字游戏出题器及解题器的实现
- 【转】游戏开发高度图有关资料与Balder中的相关支持
- 华为OJ——24点游戏算法
- 24点游戏
- 24点游戏
- LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor
- win7环境下command命令行无法add ax,bx 不支持寄存器之间的运算
- 计算24点游戏精化算法剖析
- (转)Windows 支持 DirectX 和 OpenGL,为什么大多数 PC 游戏还是 DirectX 开发?
- 华为OJ24点游戏算法
- [游戏引擎中文版]Hot Soup Processor最新中文支持版
- python_如何让类支持比较运算?
- 24点纸牌游戏
- unity3d游戏开发,让TextAsset支持中文字符
- iPhone 6/6 Plus 出现后,如何改进工作流以实现一份设计稿支持多个尺寸?
- 四则运算生成程序——GUI支持和部分功能改进