diff --git a/05_stack_and_queue/src/bracketsChecker.c b/05_stack_and_queue/src/bracketsChecker.c new file mode 100644 index 0000000..0fef5ec --- /dev/null +++ b/05_stack_and_queue/src/bracketsChecker.c @@ -0,0 +1,44 @@ +#include "stack.h" + +#include +#include +#include +#include + +int bracketsChecker(char* str, bool* res) +{ + Stack* stack = newStack(); + for (unsigned long i = 0; i < strlen(str); i++) { + if ((str[i] == '(') || (str[i] == '[') || (str[i] == '{')) { + push(stack, str[i]); + } else if ((str[i] == ')') || (str[i] == ']') || (str[i] == '}')) { + if (!isEmpty(stack)) { + char popped = pop(stack); + if (((str[i] == ')') && (popped != '(')) || ((str[i] == ']') && (popped != '[')) || ((str[i] == '}') && (popped != '{'))) { + *res = false; + } + } else { + *res = false; + } + } + } + *res = *res && isEmpty(stack); + deleteStack(stack); + return 0; +} + +int main(void) +{ + int n = 0; + scanf("%d\n", &n); + + char* input = calloc(n + 1, sizeof(char)); + fgets(input, n + 1, stdin); + + bool res = true; + bracketsChecker(input, &res); + + printf("%d\n", res); + free(input); + return 0; +} \ No newline at end of file diff --git a/05_stack_and_queue/src/sortStation.c b/05_stack_and_queue/src/sortStation.c new file mode 100644 index 0000000..a423903 --- /dev/null +++ b/05_stack_and_queue/src/sortStation.c @@ -0,0 +1,119 @@ +#include "stack.h" + +#include +#include +#include +#include + +int getPriority(char operation) +{ + if (operation == '*' || operation == '/') { + return 2; + } else if (operation == '+' || operation == '-') { + return 1; + } + return 0; +} + +int sortStation(char* str, char* res) +{ + unsigned lastUsedResIndex = 0; + Stack* stack = newStack(); + unsigned long len = strlen(str); + for (unsigned long i = 0; i < len; i++) { + if (isdigit(str[i])) { + if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') { + res[lastUsedResIndex] = ' '; + lastUsedResIndex++; + } + while (i < len && isdigit(str[i])) { + res[lastUsedResIndex] = str[i]; + lastUsedResIndex++; + i++; + } + if (i < len) { + i--; + } + } else if ((str[i] == '+') || (str[i] == '-') || (str[i] == '*') || (str[i] == '/')) { + while (!isEmpty(stack)) { + char peeked = peek(stack); + if (peeked == '(') { + break; + } + if (getPriority(peeked) >= getPriority(str[i])) { + if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') { + res[lastUsedResIndex] = ' '; + lastUsedResIndex++; + } + res[lastUsedResIndex] = pop(stack); + lastUsedResIndex++; + } else { + break; + } + } + push(stack, str[i]); + } else if (str[i] == '(') { + push(stack, str[i]); + } else if (str[i] == ')') { + char peeked = 0; + if (isEmpty(stack)) { + deleteStack(stack); + return 1; + } + peeked = peek(stack); + while (peeked != '(') { + if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') { + res[lastUsedResIndex] = ' '; + lastUsedResIndex++; + } + res[lastUsedResIndex] = peeked; + lastUsedResIndex++; + pop(stack); + + if (isEmpty(stack)) { + deleteStack(stack); + return 1; + } + peeked = peek(stack); + } + pop(stack); + } + } + while (!isEmpty(stack)) { + char popped = pop(stack); + if (popped == '(') { + deleteStack(stack); + return 1; + } + if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') { + res[lastUsedResIndex] = ' '; + lastUsedResIndex++; + } + res[lastUsedResIndex] = popped; + lastUsedResIndex++; + } + res[lastUsedResIndex] = '\0'; + deleteStack(stack); + return 0; +} + +int main(void) +{ + int n = 0; + scanf("%d\n", &n); + + char* input = calloc(n + 1, sizeof(char)); + fgets(input, n + 1, stdin); + + char* res = calloc(n + 1, sizeof(char)); + + if (sortStation(input, res) == 0) { + printf("%s\n", res); + } else { + printf("Входная строка некорректна"); + } + + free(input); + free(res); + return 0; +} diff --git a/05_stack_and_queue/src/stack.c b/05_stack_and_queue/src/stack.c new file mode 100644 index 0000000..03c6cbf --- /dev/null +++ b/05_stack_and_queue/src/stack.c @@ -0,0 +1,64 @@ +#include "stack.h" +#include +#include + +typedef struct StackNode { + char data; + struct StackNode* next; +} StackNode; + +typedef struct Stack { + StackNode* head; +} Stack; + +Stack* newStack(void) +{ + Stack* stack = calloc(1, sizeof(Stack)); + return stack; +} + +bool isEmpty(Stack* stack) +{ + return stack->head == NULL; +} + +void push(Stack* stack, char data) +{ + StackNode* element = (StackNode*)calloc(1, sizeof(StackNode)); + + element->data = data; + element->next = stack->head; + stack->head = element; +} + +char pop(Stack* stack) +{ + if (isEmpty(stack)) { + return '\0'; + } + + StackNode* oldNode = stack->head; + char data = oldNode->data; + stack->head = stack->head->next; + + free(oldNode); + + return data; +} + +char peek(Stack* stack) +{ + if (isEmpty(stack)) { + return '\0'; + } + + return stack->head->data; +} + +void deleteStack(Stack* stack) +{ + while (!isEmpty(stack)) { + pop(stack); + } + free(stack); +} diff --git a/05_stack_and_queue/src/stack.h b/05_stack_and_queue/src/stack.h new file mode 100644 index 0000000..6d576ad --- /dev/null +++ b/05_stack_and_queue/src/stack.h @@ -0,0 +1,11 @@ +#pragma once +#include + +typedef struct Stack Stack; + +Stack* newStack(void); +bool isEmpty(Stack*); +void push(Stack* stack, char data); +char pop(Stack* stack); +char peek(Stack* stack); +void deleteStack(Stack* stack); \ No newline at end of file