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
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.25)
project(homeworks C)

add_compile_options(-Wall -Wextra -pedantic)

add_subdirectory(src/stack)
5 changes: 5 additions & 0 deletions src/stack/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_library(stack stack.c)

add_executable(task2 task2.c)

target_link_libraries(task2 PRIVATE stack)
9 changes: 9 additions & 0 deletions src/stack/instruction.txt
Copy link
Collaborator

Choose a reason for hiding this comment

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

Интересно, а здесь CMake файлов нет (да и не надо уже, наверное, сделаете отдельный PR для нужной домашки)

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Task1:
gcc -Wall -Wextra -pedantic -c stack.c
gcc -Wall -Wextra -pedantic -c task1.c
gcc -Wall -Wextra -pedantic stack.o task1.o -o steakTask1

Task2:
gcc -Wall -Wextra -pedantic -c stack.c
gcc -Wall -Wextra -pedantic -c task2.c
gcc -Wall -Wextra -pedantic stack.o task2.o -o steakTask2
54 changes: 54 additions & 0 deletions src/stack/stack.c
Copy link
Collaborator

Choose a reason for hiding this comment

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

Претензии к стеку такие же, как и в соседнем PR

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>

struct StackNode {
int value;
struct StackNode* next;
};

struct Stack {
struct StackNode* head;
};

Stack* newStack()
{
Stack* stack = (Stack*)malloc(sizeof(Stack));
if (!stack)
return NULL;
stack->head = NULL;
return stack;
}

void push(struct Stack* stack, int value)
{
struct StackNode* node = (struct StackNode*)malloc(sizeof(struct StackNode));

node->value = value;
node->next = stack->head;
stack->head = node;
}

int pop(Stack* stack)
{
struct StackNode* oldNode = stack->head;
int res = oldNode->value;

stack->head = oldNode->next;
free(oldNode);

return res;
}

int isEmpty(Stack* stack)
{
return stack->head == NULL;
}

void deleteStack(Stack* stack)
{
while (!isEmpty(stack)) {
pop(stack);
}
free(stack);
}
9 changes: 9 additions & 0 deletions src/stack/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

typedef struct Stack Stack;

Stack* newStack(); // Создание стека.
void push(Stack* stack, int value); // Добавляет элемент на вершину стека.
int pop(Stack* stack); // Убирает из стека элемент и возвращает его. Необходимо проверять isEmpty перед вызовом
int isEmpty(Stack* stack);
void deleteStack(Stack* stack); // Удаление стека.
105 changes: 105 additions & 0 deletions src/stack/task2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "stack.h"
#include <stdio.h>
#include <string.h>

int precedence(char op)
{
if (op == '*' || op == '/')
return 2;
if (op == '+' || op == '-')
return 1;
return 0;
}

int isOperator(char c)
{
return c == '+' || c == '-' || c == '*' || c == '/';
}

int isDigit(char c)
Copy link
Collaborator

Choose a reason for hiding this comment

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

{
return c >= '0' && c <= '9';
}

/*
Пользователь должен выделить минимум 2 * strlen(infix) символ.
return 1 — если преобразование прошло успешно,
return 0 — если ошибка (неправильные скобки или неподдерживаемый символ).
*/
int infixToPostfix(char* infix, char* postfix)
{
Stack* st = newStack();
int rpn = 0; // индекс для записи в reverse polish notation(постфиксная запись)

for (int i = 0; infix[i]; ++i) {
char c = infix[i];

if (isDigit(c)) {
postfix[rpn++] = c;
} else if (c == '(') {
push(st, c);
} else if (c == ')') {
int found = 0;
while (!isEmpty(st)) {
int top = pop(st);
if (top == '(') {
found = 1;
break;
}
postfix[rpn++] = (char)top;
}

if (!found) {
printf("лишняя закрывающая скобка ')'\n");
deleteStack(st);
return 0;
}
} else if (isOperator(c)) {
while (!isEmpty(st)) {
int top = pop(st);
if (precedence(top) < precedence(c)) {
push(st, top);
break;
}
postfix[rpn++] = (char)top;
}
push(st, c);
} else {
printf("неподдерживаемый символ '%c'\n", c);
deleteStack(st);
return 0;
}
}

// проверка не осталось ли незакрытых скобок
while (!isEmpty(st)) {
int top = pop(st);
if (top == '(') {
printf("лишняя открывающая скобка '('\n");
deleteStack(st);
return 0;
}
postfix[rpn++] = (char)top;
}

postfix[rpn] = '\0';
deleteStack(st);
return 1;
}

int main(void)
{
char input[100];
char output[2 * 100];

printf("инфиксное выражение:\n");
scanf("%99s", input);

if (!infixToPostfix(input, output)) {
printf("ошибка.\n");
return 1;
}
printf("постфиксное выражение: %s\n", output);

return 0;
}