Games 24

From , 3 Years ago, written in Java, viewed 164 times.
URL https://pastebin.vip/view/bcb41ccd
  1. import javax.swing.*;
  2. import java.util.*;
  3.  
  4. public class GameCount24{
  5.         public static void main(String args[]){
  6.                 int[] arr1 = new int[4];
  7.                 String result = "";
  8.                 String pai;
  9.                 String input;
  10.                 int i;
  11.                 int out;
  12.                 Random ran = new Random();
  13.                 for(i=0;i<arr1.length;i++){
  14.                         arr1[i] =  ran.nextInt(13)+1;
  15.                        
  16.                         switch(arr1[i]){
  17.                         case 1: pai = "A";break;
  18.                         case 11: pai = "J";break;
  19.                         case 12: pai = "Q";break;
  20.                         case 13: pai = "K";break;
  21.                         default : pai = String.valueOf(arr1[i]);
  22.                         }
  23.                         result += pai + " ";
  24.                        
  25.                 }
  26.                 input = JOptionPane.showInputDialog("取出的牌为:"+result);
  27.                 //input = "3+2*(4-2)=";
  28.                
  29.                 char[] in = new char[input.length()];
  30.                 for(i=0;i<input.length();i++){
  31.                         in[i] = input.charAt(i);
  32.                 }
  33.                
  34.                 Caclulate ca = new Caclulate();
  35.                 //JOptionPane.showMessageDialog(null,n);
  36.                 out =  ca.calculator(in,arr1);
  37.                 if (out == 24)
  38.                         JOptionPane.showMessageDialog(null,input +out+"胜利!");
  39.                 else
  40.                         JOptionPane.showMessageDialog(null,input +out+"失败!");
  41.                 //System.out.println( "结果 :"+ca.calculator(in));
  42.         }      
  43. }
  44.  
  45. class Caclulate{       
  46.        
  47.         int n0=30;
  48.         int[] s1 = new int[n0+1]; //操作数栈
  49.         char[] s2 = new char[n0+1]; //运算符栈
  50.         int t1,t2;
  51.         int PaiCnt;
  52.  
  53.         void calcu() //一次计算
  54.         {
  55.                 int x1,x2,x;
  56.                 char p;
  57.                 //弹出一个运算符
  58.                 p=s2[t2--];
  59.                 //弹出两个操作数
  60.                 x2=s1[t1--];
  61.                 x1=s1[t1--];
  62.                 //进行一次运算
  63.                 switch(p) {
  64.                 case '+':x=x1+x2;break;
  65.                 case '-':x=x1-x2;break;
  66.                 case '*':x=x1*x2;break;
  67.                 default:x=x1/x2;
  68.                 }
  69.                 //结果压入操作数栈
  70.                 s1[++t1]=x;
  71.                 System.out.println("结果压入操作数栈s1["+t1+"]="+x);
  72.         }
  73.         void CheckPai(int[]pai)
  74.         {
  75.                 int i=0,j=0;
  76.                 /*if(t1!=4){
  77.                         System.out.println("牌数不为4");
  78.                         System.exit(0);
  79.                 }*/
  80.                 for(i=0;i<t1;i++){
  81.                         for(j=0;j<4;j++)
  82.                                 if(s1[i]==pai[j])break;
  83.                         if(j>=4){
  84.                                 System.out.println("牌"+s1[i]+"非法");
  85.                                 System.exit(0);
  86.                         }
  87.                                
  88.                 }
  89.         }
  90.  
  91.         int calculator(char[]f,int[]pai)
  92.         {
  93.                 int v,i=0,j=0;
  94.                 char[] p = f;
  95.                 t1=t2=0; //设置空栈
  96.                 PaiCnt=0;
  97.                 if(p[f.length-1]!='='){
  98.                         System.out.println("字符串以'='结束");
  99.                         System.exit(0);
  100.                 }
  101.                        
  102.                 while (p[i]!='=')
  103.                         switch(p[i]) {
  104.                         case '+': case '-':
  105.                                 while (t2!=0&&(s2[t2]!='('))
  106.                                 //执行先遇到的加、减、乘、除运算
  107.                                 calcu();
  108.                                 //当前运算符进栈
  109.                                 s2[++t2]=p[i];
  110.                                 //读下一个字符
  111.                                 System.out.println("运算符进栈s2["+t2+"]="+s2[t2]);
  112.                                 i++;
  113.                                 break;
  114.                         case '*': case '/':
  115.                                 if (t2!=0&&(s2[t2]=='*')||(s2[t2]=='/'))
  116.                                 //执行先遇到的乘、除运算
  117.                                 calcu();
  118.                                 //当前运算符进栈
  119.                                 s2[++t2]=p[i];
  120.                                 //读下一个字符
  121.                                 i++;
  122.                                 System.out.println("运算符进栈s2["+t2+"]="+s2[t2]);
  123.                                 break;
  124.                         case '(':
  125.                                 //左括号进栈
  126.                                 s2[++t2]=p[i];
  127.                                 //读下一个字符
  128.                                 i++;
  129.                                 System.out.println("运算符进栈s2["+t2+"]="+s2[t2]);
  130.                                 break;
  131.                         case ')':
  132.                                 while (s2[t2]!='(')
  133.                                 //执行括号内的加、减、乘、除运算
  134.                                 calcu();
  135.                                 //弹出左括号
  136.                                 t2--;
  137.                                 //读下一个字符
  138.                                 System.out.println("弹出左括号");
  139.                                 i++;
  140.                                 break;
  141.                         default:
  142.                                 //把字符串转换成整数值
  143.                                 v=0;
  144.                                 do {
  145.                                         v = 10*v+p[i]-'0';  //将一个数字字符或连续的数字字符转换为数值类型的量
  146.                                         i++;
  147.                                 } while((p[i]>='0')&&(p[i]<='9'));
  148.                                 //操作数进栈
  149.                                 s1[++t1]=v;
  150.                                 CheckPai(pai);
  151.                                 PaiCnt++;
  152.                                 System.out.println("操作数进栈s1["+t1+"]="+s1[t1]);
  153.                                 //num[j++]=v;
  154.                         };
  155.                
  156.                 //执行先遇到的加、减、乘、除运算
  157.                 while (t2!=0) calcu();
  158.                
  159.                 //返回结果
  160.                 return s1[t1];
  161.         }
  162.        
  163. }

Reply to "Games 24"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website