Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package stackz;
- import java.util.*;
- public class StackZ {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Stack m = new Stack();
- System.out.print("Введите строку: ");
- String s="";
- int sch = 99;
- while (sch == 99) {
- s = sc.nextLine();
- sch = 0;
- for (int i = 0; i < s.length(); i++) {
- if (s.charAt(i) == '(') {
- sch++;
- } else if (s.charAt(i) == ')') {
- sch--;
- }
- if (sch < 0) {
- sch = 99;
- break;
- }
- }
- if (sch != 0) {
- System.out.println("Строка введена некорректно. Повторите ввод.");
- sch = 99;
- }
- }
- int y;
- char[] mas = s.toCharArray();
- int a, b = 0;
- for (int i = s.length() - 1; i > -1; i--) {
- y = s.charAt(i);
- if (y != (int) '(' || y != (int) ')' || y != (int) ',') {
- if (y >= (int) '0' && y <= (int) '9') {
- m.push(mas[i]);
- } else if (y == (int) 'M' || y == (int) 'm') {
- try {
- a = (int) (char) m.peek();
- } catch (ClassCastException e) {
- a = (int) m.peek();
- }
- //System.out.println((char)a);
- if (!m.empty()) {
- m.pop();
- }
- try {
- b = (int) (char) m.peek();
- } catch (ClassCastException e) {
- b = (int) m.peek();
- } catch (EmptyStackException e) {
- b = a;
- }
- //System.out.println((char)b);
- if (!m.empty()) {
- m.pop();
- }
- /*if (!m.empty()) {
- m.pop();
- }*/
- if (y == (int) 'M') {
- if (a > b) {
- m.push(a);
- //System.out.println("="+(char)a);
- } else {
- m.push(b);
- //System.out.println("="+(char)b);
- }
- }
- if (y == (int) 'm') {
- if (a < b) {
- m.push(a);
- //System.out.println("="+(char)a);
- } else {
- m.push(b);
- //System.out.println("="+(char)b);
- }
- }
- }
- }
- }
- System.out.println(((char) (int) m.peek()));
- }
- // M(3,m(2,3))
- //Введите строку: m(m(m(3,M(3,4)),2),4)
- }
Advertisement
Add Comment
Please, Sign In to add comment