Arkanium77

M(m...)

Nov 20th, 2016
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. package stackz;
  2.  
  3. import java.util.*;
  4.  
  5. public class StackZ {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.         Stack m = new Stack();
  10.         System.out.print("Введите строку: ");
  11.         String s="";
  12.         int sch = 99;
  13.         while (sch == 99) {
  14.             s = sc.nextLine();
  15.             sch = 0;
  16.             for (int i = 0; i < s.length(); i++) {
  17.                 if (s.charAt(i) == '(') {
  18.                     sch++;
  19.                 } else if (s.charAt(i) == ')') {
  20.                     sch--;
  21.                 }
  22.                 if (sch < 0) {
  23.                     sch = 99;
  24.                     break;
  25.                 }
  26.             }
  27.             if (sch != 0) {
  28.                 System.out.println("Строка введена некорректно. Повторите ввод.");
  29.                 sch = 99;
  30.             }
  31.         }
  32.         int y;
  33.         char[] mas = s.toCharArray();
  34.         int a, b = 0;
  35.         for (int i = s.length() - 1; i > -1; i--) {
  36.             y = s.charAt(i);
  37.             if (y != (int) '(' || y != (int) ')' || y != (int) ',') {
  38.                 if (y >= (int) '0' && y <= (int) '9') {
  39.                     m.push(mas[i]);
  40.                 } else if (y == (int) 'M' || y == (int) 'm') {
  41.                     try {
  42.                         a = (int) (char) m.peek();
  43.                        
  44.                     } catch (ClassCastException e) {
  45.                         a = (int) m.peek();
  46.                     }
  47.                     //System.out.println((char)a);
  48.                     if (!m.empty()) {
  49.                         m.pop();
  50.                     }
  51.                     try {
  52.                         b = (int) (char) m.peek();
  53.                     } catch (ClassCastException e) {
  54.                         b = (int) m.peek();
  55.                     } catch (EmptyStackException e) {
  56.                         b = a;
  57.                     }
  58.                     //System.out.println((char)b);
  59.                     if (!m.empty()) {
  60.                         m.pop();
  61.                     }
  62.                     /*if (!m.empty()) {
  63.                         m.pop();
  64.                     }*/
  65.                     if (y == (int) 'M') {
  66.                         if (a > b) {
  67.                             m.push(a);
  68.                             //System.out.println("="+(char)a);
  69.                         } else {
  70.                             m.push(b);
  71.                             //System.out.println("="+(char)b);
  72.                         }
  73.                     }
  74.                     if (y == (int) 'm') {
  75.                         if (a < b) {
  76.                             m.push(a);
  77.                             //System.out.println("="+(char)a);
  78.                         } else {
  79.                             m.push(b);
  80.                             //System.out.println("="+(char)b);
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.         System.out.println(((char) (int) m.peek()));
  87.     }
  88.     // M(3,m(2,3))
  89.     //Введите строку: m(m(m(3,M(3,4)),2),4)
  90. }
Advertisement
Add Comment
Please, Sign In to add comment