Untitled

 avatar
unknown
plain_text
2 years ago
2.1 kB
8
Indexable
#include <stdio.h>
#include <stdlib.h>

struct Stack{
    char* braces;
    int last_index;
};


int push(char brace, struct Stack* stack){
    if (brace == '{' || brace == '[' || brace == '(' || brace == '<'){
        *(stack->braces+stack->last_index) = brace;
        stack->last_index++;
    }
    else {
        if(*(stack->braces+stack->last_index-1) == brace-2 || (*(stack->braces+stack->last_index-1) == brace-1 && brace == ')')){
            stack->last_index--;
        }
        else {
            return 0;
        }
    }
    return 1;
}


int main() {
    struct Stack* stack = malloc(sizeof(struct Stack));
    stack->last_index = 0;
    stack->braces = malloc(500*sizeof(char));
    char c = '1';
    while (c != '\n'){
        scanf("%c", &c);
        if ((c == '{' || c == '[' || c == '(' || c == '<') || (c == '}' || c == ']' || c == ')' || c == '>')){
            if (!push(c, stack)){
                printf("wrong");
                return 0;
            }
        }
    }
    printf("correct");
    return 0;
}















#include <stdio.h>
#include <stdlib.h>

struct Stack{
    char* braces;
    int last_index;
};


int push(char brace, struct Stack* stack){
    if (brace == '{' || brace == '[' || brace == '(' || brace == '<'){
        *(stack->braces+stack->last_index) = brace;
        stack->last_index++;
    }
    else {
        if(*(stack->braces+stack->last_index-1) == brace-2 || (*(stack->braces+stack->last_index-1) == brace-1 && brace == ')')){
            stack->last_index--;
        }
        else {
            return 0;
        }
    }
    return 1;
}


int main() {
    struct Stack* stack = malloc(sizeof(struct Stack));
    stack->last_index = 0;
    stack->braces = malloc(500*sizeof(char));
    char c = '1';
    while (c != '\n'){
        scanf("%c", &c);
        if ((c == '{' || c == '[' || c == '(' || c == '<') || (c == '}' || c == ']' || c == ')' || c == '>')){
            if (!push(c, stack)){
                printf("wrong");
                return 0;
            }
        }
    }
    printf("correct");
    return 0;
}
Editor is loading...