From e43c97e4d667a2006aa384b35f1c79dc467e90d8 Mon Sep 17 00:00:00 2001 From: stuffacc Date: Wed, 15 Oct 2025 17:30:50 +0300 Subject: [PATCH 1/3] Stack 1.0 --- 5/stack.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 5/stack.h | 18 ++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 5/stack.c create mode 100644 5/stack.h diff --git a/5/stack.c b/5/stack.c new file mode 100644 index 0000000..eaae60e --- /dev/null +++ b/5/stack.c @@ -0,0 +1,46 @@ +#include "stack.h" + + +void init(Stack* stack) { + (*stack).top = NULL; + (*stack).size = 0; +} + +void push(Stack* stack, char value) { + Node* new_node = malloc(sizeof(Node)); + + (*new_node).value = value; + (*new_node).next = (*stack).top; + + (*stack).top = new_node; + (*stack).size++; +} + + + +Node* pop(Stack* stack) { + if ((*stack).top == NULL) { + return NULL; + } + + Node* out = (*stack).top; + (*stack).top = (*out).next; + + (*stack).size--; + + return out; +} + +void printStack(Stack* stack) { + if ((*stack).size == 0) { + printf("\n"); + return; + } + Node* current = (*stack).top; + do { + printf("%c ", (*current).value); + current = (*current).next; + } + while (current != NULL); + printf("\n"); +} diff --git a/5/stack.h b/5/stack.h new file mode 100644 index 0000000..b2d5857 --- /dev/null +++ b/5/stack.h @@ -0,0 +1,18 @@ +#include +#include + +typedef struct Node { + char value; + struct Node* next; +} Node; + +typedef struct Stack { + Node* top; + int size; +} Stack; + + +void init(Stack* stack); +void push(Stack* stack, char value); +Node* pop(Stack* stack); +void printStack(Stack* stack); From 9cb289df99bb112fa7895915227fb128fcca14b0 Mon Sep 17 00:00:00 2001 From: stuffacc Date: Wed, 15 Oct 2025 17:39:25 +0300 Subject: [PATCH 2/3] shunting yard --- 5/shuntingYard.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 5/shuntingYard.c diff --git a/5/shuntingYard.c b/5/shuntingYard.c new file mode 100644 index 0000000..b24e856 --- /dev/null +++ b/5/shuntingYard.c @@ -0,0 +1,88 @@ +#include "stack.h" + +int getPriority(char cur); + + +void main() { + char string[200] = {' '}; + + Stack stack; + init(&stack); + fgets(string, sizeof(string), stdin); + + for (int i = 0; i < 200; i++) { + char cur = string[i]; + if (cur == ' ') { + continue; + } + else if (cur == '\n') { + break; + } + + else if (cur == '(') { + push(&stack, cur); + } + + else if (cur == ')') { + while (stack.size > 0) { + Node* node = pop(&stack); + char value = (*node).value; + if (value == '(') { + break; + } + + printf("%c ", value); + } + } + else if (cur == '+' || cur == '-' || cur == '*' || cur == '/') { + int curPriority = getPriority(cur); + while (stack.size > 0) { + Node* node = pop(&stack); + char value = (*node).value; + + int valuePriority = getPriority(value); + if (valuePriority >= curPriority) { + printf("%c ", value); + } + else { + push(&stack, value); + free(node); + break; + } + } + push(&stack, cur); + } + + else { + printf("%c ", cur); + } + + } + + + while (stack.size > 0) { + Node* node = pop(&stack); + char value = (*node).value; + if (value == '(') { + break; + } + + printf("%c ", value); + } + + printf("\n"); + + + +} + +int getPriority(char cur) { + if (cur == '+' || cur == '-') { + return 1; + } + else if (cur == '*' || cur == '/') { + return 2; + } + + return -1000; +} From 7c6a7a73e953a64827f1c97be98de9477b7d3e10 Mon Sep 17 00:00:00 2001 From: stuffacc Date: Wed, 15 Oct 2025 18:19:41 +0300 Subject: [PATCH 3/3] Print FIX --- 5/shuntingYard.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/5/shuntingYard.c b/5/shuntingYard.c index b24e856..3b91d31 100644 --- a/5/shuntingYard.c +++ b/5/shuntingYard.c @@ -10,12 +10,11 @@ void main() { init(&stack); fgets(string, sizeof(string), stdin); + int countSpace = 0; + for (int i = 0; i < 200; i++) { char cur = string[i]; - if (cur == ' ') { - continue; - } - else if (cur == '\n') { + if (cur == '\n') { break; } @@ -31,7 +30,7 @@ void main() { break; } - printf("%c ", value); + printf(" %c", value); } } else if (cur == '+' || cur == '-' || cur == '*' || cur == '/') { @@ -42,7 +41,7 @@ void main() { int valuePriority = getPriority(value); if (valuePriority >= curPriority) { - printf("%c ", value); + printf(" %c", value); } else { push(&stack, value); @@ -54,7 +53,16 @@ void main() { } else { - printf("%c ", cur); + if (cur == ' ' && countSpace == 0) { + printf("%c", cur); + countSpace += 1; + } + else if (cur != ' ') { + printf("%c", cur); + countSpace = 0; + } + + } } @@ -67,7 +75,7 @@ void main() { break; } - printf("%c ", value); + printf(" %c", value); } printf("\n");