If else vs switches with stack logic

I learn stacks by writing a program that checks the syntax of brackets. If I introduce (Baller), it should give me a positive result. If I have (Baller(), this should give me a negative result. Essentially, the application verifies the correct use of the user (), {} and [].

  • If I come across a character (, {, or [], I will add it to the character on the stack.
  • If I come across the a)}] sign, I will remove the character from the stack.
  • If the text contains an odd number of brackets or parentheses (for example, (and] is not continuous), an error message is printed.

So, I did this half in the if else statement, but I thought it should be easier to do in the switch statement, and also be a good learning experience.

So what I did in the switch statement:

public class Input {
    public static void main(String[] args) {
        Stack stack = new Stack();
        String str;
        str = JOptionPane.showInputDialog("Text to parse: ");
        char arr[] = str.toCharArray();
        System.out.print(str);
        System.out.println();
        System.out.println();

        for(char c : arr) {
            switch(c) {

            case '{':
                stack.Push(c);
                System.out.print(stack.firstNode.getData());
                break;
            case '(':
                stack.Push(c);
                System.out.print(stack.firstNode.getData());
                break;
            case '[':
                stack.Push(c);
                System.out.print(stack.firstNode.getData());
                break;

            case '}':
                c = (Character) stack.Peek(); //<-- Edited for @Jimmy
                if( c != '{') {
                    System.out.println("  Syntax ERROR");
                }
            case ']':
                if( c != '[') {
                    System.out.println("  Syntax ERROR");
                }   
            case ')':
                if( c != '(') {
                    System.out.println("  Syntax ERROR");
                }
            }
        }
    }
}

: , , . if-else, if-, :

if(first == '(' && (current == '}' || current == ']')) {
if first == '{' && (current == ']' || current == ')')) {
//and so on

? ?

, , .

:

import javax.swing.JOptionPane;



public class Input {
    public static void main(String[] args) {
        Stack stack = new Stack();
        String str;
        str = JOptionPane.showInputDialog("Text to parse: ");
        char arr[] = str.toCharArray();
        System.out.print(str);
        System.out.println();
        System.out.println();


        for(char c : arr) {

            switch(c) {

            case '{':
                stack.Push(c);
                break;
            case '(':
                stack.Push(c);
                break;
            case '[':
                stack.Push(c);
                break;

            case '}':
                if(stack.isEmpty() || (Character) stack.Pop() != '{') {
                    System.out.println("  Syntax ERROR");
                }
                break;
            case ']':
                if(stack.isEmpty() || (Character) stack.Pop() != '[') {
                    System.out.println("  Syntax ERROR");
                }
                break;
            case ')':
                if(stack.isEmpty() || (Character) stack.Pop() != '(') {
                    System.out.println("  Syntax ERROR");
                }
                break;
            }
        } if(!stack.isEmpty()) {
            System.out.println("  Syntax ERROR");
        }
    }
}
+4
3

! , .

switch, , , . , stack.isEmpty() || . , , .

. , , java.util.Stack.

for(char c : arr) {

    switch(c) {

    case '{':
        stack.push(c);
        System.out.print(stack.peek());
        break;
    case '(':
        stack.push(c);
        System.out.print(stack.peek());
        break;
    case '[':
        stack.push(c);
        System.out.print(stack.peek());
        break;

    case '}':
        if(stack.isEmpty() || (Character) stack.pop() != '{') {
            System.out.println("  Syntax ERROR");
        }
        break;
    case ']':
        if(stack.isEmpty() || (Character) stack.pop() != '[') {
            System.out.println("  Syntax ERROR");
        }
        break;
    case ')':
        if(stack.isEmpty() || (Character) stack.pop() != '(') {
            System.out.println("  Syntax ERROR");
        }
        break;
    }
}

EDIT: break;. case: , "" .

2: . , , . - :

if(!stack.isEmpty()) {
    System.out.println("  Syntax ERROR");
}

3: stack.firstElement() stack.peek().

+4

, , :

  • . , , .

  • . .

1, , :

case '}':
    if (stack.empty()) {
        System.out.println("  Syntax ERROR");
    }
    else {
        c = (Character) stack.Pop();
        if( c != '{') {
            System.out.println("  Syntax ERROR");
        }
    }
    break;

,

case '}':
    if (stack.empty() || ((Character)stack.Pop()) != '{') {
        System.out.println("  Syntax ERROR");
    }
    break;

2, , . (bal)ler), .

if (!stack.empty()) 
    System.out.println("  Syntax ERROR");
}
+2

You said you want to learn how to use the operator switch. There is a neat trick you can use called failing:

switch (c) {
    case '{':
    case '(':
    case '[':
        stack.Push(c);
        System.out.print(stack.firstNode.getData());
        break;

    case '}':
    case ']':
    case ')':
        if (stack.isEmpty() || stack.pop() != c) {
            System.out.println("Syntax ERROR");
        }
        break;
}

You can also use a pre-implemented class Stack, which also generic.

+2
source

Source: https://habr.com/ru/post/1606855/


All Articles