您的位置:首页 > 其它

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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: