import javax.swing.*;
import java.util.*;
public class GameCount24{
public static void main
(String args
[]){
int[] arr1 = new int[4];
int i;
int out;
for(i=0;i<arr1.length;i++){
arr1[i] = ran.nextInt(13)+1;
switch(arr1[i]){
case 1: pai = "A";break;
case 11: pai = "J";break;
case 12: pai = "Q";break;
case 13: pai = "K";break;
default : pai
= String.
valueOf(arr1
[i
]);
}
result += pai + " ";
}
//input = "3+2*(4-2)=";
char[] in = new char[input.length()];
for(i=0;i<input.length();i++){
in[i] = input.charAt(i);
}
Caclulate ca = new Caclulate();
//JOptionPane.showMessageDialog(null,n);
out = ca.calculator(in,arr1);
if (out == 24)
else
//System.out.println( "结果 :"+ca.calculator(in));
}
}
class Caclulate{
int n0=30;
int[] s1 = new int[n0+1]; //操作数栈
char[] s2 = new char[n0+1]; //运算符栈
int t1,t2;
int PaiCnt;
void calcu() //一次计算
{
int x1,x2,x;
char p;
//弹出一个运算符
p=s2[t2--];
//弹出两个操作数
x2=s1[t1--];
x1=s1[t1--];
//进行一次运算
switch(p) {
case '+':x=x1+x2;break;
case '-':x=x1-x2;break;
case '*':x=x1*x2;break;
default:x=x1/x2;
}
//结果压入操作数栈
s1[++t1]=x;
System.
out.
println("结果压入操作数栈s1["+t1
+"]="+x
);
}
void CheckPai(int[]pai)
{
int i=0,j=0;
/*if(t1!=4){
System.out.println("牌数不为4");
System.exit(0);
}*/
for(i=0;i<t1;i++){
for(j=0;j<4;j++)
if(s1[i]==pai[j])break;
if(j>=4){
System.
out.
println("牌"+s1
[i
]+"非法");
}
}
}
int calculator(char[]f,int[]pai)
{
int v,i=0,j=0;
char[] p = f;
t1=t2=0; //设置空栈
PaiCnt=0;
if(p[f.length-1]!='='){
System.
out.
println("字符串以'='结束");
}
while (p[i]!='=')
switch(p[i]) {
case '+': case '-':
while (t2!=0&&(s2[t2]!='('))
//执行先遇到的加、减、乘、除运算
calcu();
//当前运算符进栈
s2[++t2]=p[i];
//读下一个字符
System.
out.
println("运算符进栈s2["+t2
+"]="+s2
[t2
]);
i++;
break;
case '*': case '/':
if (t2!=0&&(s2[t2]=='*')||(s2[t2]=='/'))
//执行先遇到的乘、除运算
calcu();
//当前运算符进栈
s2[++t2]=p[i];
//读下一个字符
i++;
System.
out.
println("运算符进栈s2["+t2
+"]="+s2
[t2
]);
break;
case '(':
//左括号进栈
s2[++t2]=p[i];
//读下一个字符
i++;
System.
out.
println("运算符进栈s2["+t2
+"]="+s2
[t2
]);
break;
case ')':
while (s2[t2]!='(')
//执行括号内的加、减、乘、除运算
calcu();
//弹出左括号
t2--;
//读下一个字符
i++;
break;
default:
//把字符串转换成整数值
v=0;
do {
v = 10*v+p[i]-'0'; //将一个数字字符或连续的数字字符转换为数值类型的量
i++;
} while((p[i]>='0')&&(p[i]<='9'));
//操作数进栈
s1[++t1]=v;
CheckPai(pai);
PaiCnt++;
System.
out.
println("操作数进栈s1["+t1
+"]="+s1
[t1
]);
//num[j++]=v;
};
//执行先遇到的加、减、乘、除运算
while (t2!=0) calcu();
//返回结果
return s1[t1];
}
}