Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions 5/shuntingYard.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "stack.h"

int getPriority(char cur);


void main() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нужно выделить алгоритм в отдельную функцию.

char string[200] = {' '};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем здесь пробел?


Stack stack;
init(&stack);
fgets(string, sizeof(string), stdin);

int countSpace = 0;

for (int i = 0; i < 200; i++) {
char cur = string[i];
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 {
if (cur == ' ' && countSpace == 0) {
printf("%c", cur);
countSpace += 1;
}
else if (cur != ' ') {
printf("%c", cur);
countSpace = 0;
}


}

}


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;
}
46 changes: 46 additions & 0 deletions 5/stack.c
Original file line number Diff line number Diff line change
@@ -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);
Comment on lines +40 to +44

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это легко можно переписать в виде обычного цикла while.

printf("\n");
}
18 changes: 18 additions & 0 deletions 5/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdlib.h>
#include <stdio.h>

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);