Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
2.1 kB
2
Indexable
Never
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SIZE 501

struct Stack {
    char arr[SIZE];
    int topIndex;
};

struct Stack* initStack()
{
    struct Stack *new_stack = (struct Stack*)malloc(sizeof(struct Stack));
    new_stack->topIndex = -1;
    return new_stack;
}

char pop(struct Stack *stack)
{
    int ind = stack->topIndex;
    if (ind == -1)
        return 0;
    char elem = stack->arr[ind];
    (stack->topIndex)--;
    return elem;
}

char top(struct Stack *stack)
{
    int ind = stack->topIndex;
    if (ind == -1)
        return 0;
    return stack->arr[ind];
}

void push(struct Stack *stack, char value)
{
    int ind = ++(stack->topIndex);
    stack->arr[ind] = value;
}

int isEmpty(struct Stack *stack)
{
    if (stack->topIndex == -1)
        return 1;
    return 0;
}

int count(struct Stack *stack)
{
    return stack->topIndex + 1;
}

int main()
{   
    struct Stack *sym_stack = initStack();
    char input_str[SIZE];
    char prev_brack;
    fgets(input_str, SIZE, stdin);
    for (int i = 0; input_str[i]; i++) {
        char cur_sym = input_str[i];
        if (cur_sym == '(' || cur_sym == '{' || cur_sym == '[' || cur_sym == '<') {
            push(sym_stack, cur_sym);
        } else if (cur_sym == ')' || cur_sym == '}' || cur_sym == ']' || cur_sym == '>') {
            prev_brack = pop(sym_stack);
            if (prev_brack) {
                int first_case = cur_sym == ')' && prev_brack == '(';
                int sec_case = cur_sym == '}' && prev_brack == '{';
                int third_case = cur_sym == ']' && prev_brack == '[';
                int fourth_case = cur_sym == '>' && prev_brack == '<';
                if (!(first_case || sec_case || third_case || fourth_case)) {
                    printf("wrong");
                    return 0;
                }
            } else {
                printf("wrong");
                return 0;
            }
        }
    }
    if (isEmpty(sym_stack)) {
        printf("correct");
    } else {
        printf("wrong");
    }
    return 0;
}