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
1 change: 0 additions & 1 deletion HomeworksAccept/homework21.09/read.me

This file was deleted.

1 change: 0 additions & 1 deletion HomeworksAccept/homework28.09/read.me

This file was deleted.

1 change: 0 additions & 1 deletion HomeworksAccept/read.me

This file was deleted.

121 changes: 121 additions & 0 deletions parsingTree/parsingTree/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include "tree.h"
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <stdbool.h>

bool isDigit(char symbol) {
return symbol >= '0' && symbol <= '9';
}

bool readFromFileToTree(char fileName[], Tree* tree) {
int errorCode = 0;
FILE* file = fopen(fileName, "rt");
if (file == NULL) {
printf("���� �� ������!");
return false;
}
char letter = 0;

while (!feof(file)) {
fscanf(file, "%c", &letter);
if (letter == '+' || letter == '*' || letter == '/') {
addToTreeSymbol(tree, letter, &errorCode);
if (errorCode != 0) {
return false;
}
}
if (letter == '-') {
int symbol = getc(file);
if (symbol == ' ') {
addToTreeSymbol(tree, letter, &errorCode);
if (errorCode != 0) {
return false;
}
ungetc(symbol, file);
} else {
ungetc(symbol, file);
fscanf(file, "%c", &letter);
int number = -letter - '0';
fscanf(file, "%c", &letter);
while (isDigit(letter)) {
number *= 10;
number += letter - '0';
fscanf(file, "%c", &letter);
}
addToTreeNumber(tree, number, &errorCode);
if (errorCode != 0) {
return false;
}
}
}
if (isDigit(letter)) {
int number = (int)letter - 48;
fscanf(file, "%c", &letter);
while (isDigit(letter)) {
number *= 10;
number += (int)letter - 48;
fscanf(file, "%c", &letter);
}
addToTreeNumber(tree, number, &errorCode);
if (errorCode != 0) {
return false;
}
}
}
return true;
}

bool connectingFunction(char fileName[]) {
Tree* tree = createTree();
if (!readFromFileToTree(fileName, tree)) {
return false;
}
printTree(tree);
int errorCode = 0;
int number = postorderCount(tree, &errorCode);
if (errorCode != 0) {
return -1;
}
printf("%d\n", number);
clearTree(tree);
return true;
}

bool test() {
Tree* tree = createTree();
if (!readFromFileToTree("test.txt", tree)) {
return false;
}
int errorCode = 0;
int number = postorderCount(tree, &errorCode);
if (errorCode != 0) {
return -1;
}
clearTree(tree);
return number == -4;
}

int main() {
setlocale(LC_ALL, "RUS");
if (test()) {
printf("����� ������ �������!\n");
} else {
printf("������...\n");
return -1;
}
printf("������� ��� ����� � ��� �����������, ������ �� ������ ��������� 100 ��������\n");
char fileName[100] = { '\0' };
int checkScan = scanf("%s", &fileName);
while (checkScan != 1) {
while (getchar() != '\n') {
}

printf("������...");
checkScan = scanf("%s", &fileName);
}
if (!connectingFunction(fileName)) {
printf("������...\n");
return -1;
}
}
84 changes: 84 additions & 0 deletions parsingTree/parsingTree/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "stack.h"
#include <stdlib.h>
#include <stdio.h>

typedef struct Value {
int number;
char symbol;
} Value;

typedef struct Node {
Value value;
Node* right;
Node* left;
} Node;

typedef struct Unit {
Node* value;
Unit* next;
}Unit;

typedef struct Stack {
Unit* head;
}Stack;

Stack* createStack(void) {
Stack* stack = calloc(1, sizeof(stack));
return stack;
}

int push(Stack* stack, Node* value) {
Unit* temp = calloc(1, sizeof(Unit));
if (temp == NULL) {
printf("�������� � �������...");
return -1;
}
temp->value = value;
temp->next = stack->head;

stack->head = temp;
return 0;
}

Node* pop(Stack* stack, int* errorCode) {
if (stack->head == NULL) {
if (errorCode != NULL) {
*errorCode = -1;
}
return NULL;
}
if (errorCode != NULL) {
*errorCode = 0;
}

Node* value = stack->head->value;

Unit* next = stack->head->next;
free(stack->head);
stack->head = next;

return value;
}

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

void customDelete(Stack* stack) {
while (!isEmpty(stack)) {
int errorCode = 0;
Node* element = pop(stack, &errorCode);
free(element);
}
free(stack);
stack = NULL;
}

void deleteStack(Stack* stack) {
while (!isEmpty(stack)) {
int errorCode = 0;
pop(stack, &errorCode);
}
free(stack);
stack = NULL;
}
30 changes: 30 additions & 0 deletions parsingTree/parsingTree/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef STACK_H
#define STACK_H
#include <stdbool.h>

typedef struct Stack Stack;

typedef struct Node Node;

typedef struct Value Value;

typedef struct Unit Unit;

//�������� stack'a
Stack* createStack(void);

//���������� �������� � ������ �����
int push(Stack* stack, Node* value);

//�������� �������� �� �����(�� ������ �����)
Node* pop(Stack* stack, int* errorCode);

//�������� �� �������
bool isEmpty(Stack* stack);

//������� �����
void deleteStack(Stack* stack);

//������� ����� + ������������� �������� ������ ����
void customDelete(Stack* stack);
#endif
1 change: 1 addition & 0 deletions parsingTree/parsingTree/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(- (* (+ 2 2) 2) (* (+ 2 2) 3))
Loading