Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.1 kB
5
Indexable
Never
#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;
}