document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.util.*;
  2. public class acmD {
  3.     public static void main(String [] args) {
  4.         Scanner scn = new Scanner(System.in);
  5.         String ls = System.getProperty("line.separator");
  6.         scn.useDelimiter(ls);
  7.         int n = scn.nextInt();
  8.         for(int x=1;x<=n;x++) {
  9.             String s = scn.next();
  10.             StringTokenizer st = new StringTokenizer(s, " ");
  11.             int count = 1;
  12.             ArrayList <Integer> num = new ArrayList<Integer>();
  13.             ArrayList <String> operator = new ArrayList<String>();
  14.             ArrayList <String> operatorStack = new ArrayList<String>();
  15.             while(st.hasMoreTokens()) {
  16.                 if((count%2) != 0) {
  17.                     String ss = st.nextToken();
  18.                     int no = Integer.parseInt(ss);
  19.                     num.add(no);
  20.                 }
  21.                 else {
  22.                     String ss = st.nextToken();
  23.                     operator.add(ss);
  24.                 }
  25.                 count++;
  26.             }
  27.            
  28.             for(int y =0;y< operator.size();y++) {
  29.                 String compare = operator.get(y);
  30.                 char comp = compare.charAt(0);
  31.                 if(comp == \'/\' || comp == \'*\') {
  32.                     operatorStack.add(compare);
  33.                 }
  34.             }
  35.             for(int y =0;y< operator.size();y++) {
  36.                 String compare = operator.get(y);
  37.                 char comp = compare.charAt(0);
  38.                 if(comp == \'+\' || comp == \'-\') {
  39.                     operatorStack.add(compare);
  40.                 }
  41.             }
  42.             System.out.print("Case #"+x+": ");
  43.             for(int y =0;y< operatorStack.size();y++) {
  44.                 System.out.print(operatorStack.get(y)+" ");
  45.             }
  46.  
  47.             for(int y=0;y<operator.size();y++) {
  48.                 String compare = operator.get(y);
  49.                 char comp = compare.charAt(0);
  50.                 int total = 0;
  51.                 if(comp == \'/\') {
  52.                     int no1 = num.get(y);
  53.                     int no2 = num.get(y+1);
  54.                     total = no1/no2;
  55.                     num.remove(y);
  56.                     num.remove(y);
  57.                     num.add(y, total);
  58.                     operator.remove(y);
  59.                     y--;
  60.                 } else
  61.                 if(comp == \'*\') {
  62.                     int no1 = num.get(y);
  63.                     int no2 = num.get(y+1);
  64.                     total = no1 * no2;
  65.                     num.remove(y);
  66.                     num.remove(y);
  67.                     num.add(y, total);
  68.                     operator.remove(y);
  69.                     y--;
  70.                 }
  71.             }
  72.             for(int y=0;y<operator.size();y++) {
  73.                 String compare = operator.get(y);
  74.                 char comp = compare.charAt(0);
  75.                 int total = 0;
  76.                 if(comp == \'+\') {
  77.                     int no1 = num.get(y);
  78.                     int no2 = num.get(y+1);
  79.                     total = no1 + no2;
  80.                     num.remove(y);
  81.                     num.remove(y);
  82.                     num.add(y, total);
  83.                     operator.remove(y);
  84.                     y--;
  85.                 } else
  86.                 if(comp == \'-\') {
  87.                     int no1 = num.get(y);
  88.                     int no2 = num.get(y+1);
  89.                     total = no1 - no2;
  90.                     num.remove(y);
  91.                     num.remove(y);
  92.                     num.add(y, total);
  93.                     operator.remove(y);
  94.                     y--;
  95.                 }
  96.             }
  97.             System.out.print(num.get(0));
  98.             System.out.println();
  99.         }
  100.     }
  101. }
');