#include
#include
#include
Char sign [4]={' + ', '-', '*', '/'};//define a global variable, symbols used to store addition, subtraction, multiplication, and division, convenient the following symbol to judge
The class game24point//24 point card game
{
Private:
Double a, [4].//in order to realize precise calculation, prevent the calculation results for decimal was forced into an integer, adopts the double variable
Public:
Game24point ();//the constructor
Double c (double, double, char);//calculation function, a symbol of two sequential number, the results
Void the find ();//search function, used to find the right solution
Void shuzhi ();//get four CARDS numerical
};
Game24point: : game24point ()
{
Char ch [2];//because the CARDS, in addition to the digital and AJQK characters in reason to use character array
Int x, y;
Srand (time (0));
X=rand () % + 1;
Y=rand () % + 1;
A [2]=x;
A [3]=y;
cout<" # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # "& lt;
{
Cin> Ch [I];
If (ch [I]=='J') a [I]=11;
Else if (ch [I]=='Q') a [I]=12;
Else if (ch [I]=='K') a [I]=13;
Else if (ch [I]=='A') A [I]=1;
Else if (ch [I]=='2') a [I]=2;
Else if (ch [I]=='3') a [I]=3;
Else if (ch [I]=='4') a [I]=4;
Else if (ch [I]=='5') a [I]=5;
Else if (ch [I]=='6') a [I]=6;
Else if (ch [I]=='7') a [I]=7.
Else if (ch [I]=='8') a [I]=8;
Else if (ch [I]=='9') a [I]=9.
Else if (ch [I]=='10') a [I]=10;
The else
{
cout<" # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # "& lt;
}//card character converted to numerical
cout<" Computer card points: "& lt;
Double game24point: : c (double a, double b, char c)
{
If (c=='+')
Return a + b;//for +, it returns the corresponding results
Else if (c=='-')
Return a - b;
Else if (c=='*')
Return a * b;
Else if (c=='/')
If (b!=0)
Return a/b;//only when the denominator is not zero, return the result
}
Void game24point: : find ()
{
Double pl [3], lw [2];//the first symbol is placed, after a is equal to the number of the three remaining after calculation, the array is used to store the three number
Double sum;//get the final result of
Int judge=0;//determine whether to find a reasonable solution
cout<" # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # "& lt;
{
for(int j=0; j<4. J++)//second placed symbol
{
For (int k=0; k<4. K++)//third placed symbol
{
for(int m=0; M<3; M++)//first calculation of two adjacent Numbers, there are three kinds of circumstances, is equivalent to the role of the brackets
{
If (a==0 [m + 1] & amp; & Sign [I]=='/') break;
Pl [m]=c (a [m], a [m + 1], sign [I]);
Pl [3] (m + 1) %=a/(m + 2) % 4.
Pl/(m + 2) % 3=a/(m + 3) % 4.//make sure the first two Numbers, is equal to the number of the remaining three calculation is complete, in order to store in the temp array
For (int n=0; N<2; N++)//three Numbers to select the calculation of two adjacent Numbers, first two cases, equivalent to a second brackets
{
If (pl [n + 1)==0 & amp; & Sign [j]=='/') break;
Lw [n]=c (pl [n], pl (n + 1), sign [j]);
Lw [2] (n + 1) %=pl/(n + 2) % 3;//make sure the first two Numbers, is equal to the number of the remaining two calculation is complete, in order to store in the temp array
If (lw [1]==0 & amp; & Sign [k]=='/') break;
Sum=c (lw [0], lw [1], sign [k]);//calculation and
If (sum==24) if//and 24
{
Judge=1;//judgment operator is 1, said it had obtained solution
If (m==0 & amp; & N==0)
cout<" ((" & lt; Else if (m==0 & amp; & N==1)
cout<" (" & lt; Else if (m==1 & amp; & N==0)
cout<" (" & lt; Else if (m==1 & amp; & N==1)
coutElse if (m==2 & amp; & N==0)
cout