From 9c1fece35b6e70ccaeba493ce727bde3a08602e0 Mon Sep 17 00:00:00 2001 From: Arity-T Date: Sun, 9 Mar 2025 11:12:41 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC=D1=8B=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D1=82=D0=B5=D1=81=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 + programs/1_bubble_sort.cpp | 65 ++++++++++++++ programs/2_exponentiation.cpp | 31 +++++++ programs/3_bst.cpp | 165 ++++++++++++++++++++++++++++++++++ 4 files changed, 265 insertions(+) create mode 100644 .gitignore create mode 100644 programs/1_bubble_sort.cpp create mode 100644 programs/2_exponentiation.cpp create mode 100644 programs/3_bst.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..75dca29 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.docx +*.pdf +*.exe +stuff \ No newline at end of file diff --git a/programs/1_bubble_sort.cpp b/programs/1_bubble_sort.cpp new file mode 100644 index 0000000..34ad431 --- /dev/null +++ b/programs/1_bubble_sort.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; + +void swap(int &a, int &b) +{ + int tmp = a; + a = b; + b = tmp; +} + +void bubbleSort(int arr[], int n) +{ + int nPairs = n; +A: + nPairs = nPairs - 1; + bool hasSwapped = false; + int i = 0; + while (i < nPairs) + { + if (arr[i] > arr[i + 1]) + { + swap(arr[i], arr[i + 1]); + hasSwapped = true; + } + i++; + } + if (hasSwapped) + { + goto A; + } +} + +int main() +{ + int n; + cin >> n; + + if (n < 1 || n > 10000) { + return 1; + } + + + int *arr = new int[n]; + + for (int i = 0; i < n; i++) + { + int tmp; + cin >> tmp; + if (tmp > 10000 || tmp < -10000) return 1; + arr[i] = tmp; + } + + bubbleSort(arr, n); + + for (int i = 0; i < n; i++) + { + cout << arr[i]; + if (i < n - 1) + cout << " "; + } + cout << endl; + + delete[] arr; + return 0; +} diff --git a/programs/2_exponentiation.cpp b/programs/2_exponentiation.cpp new file mode 100644 index 0000000..eb22c31 --- /dev/null +++ b/programs/2_exponentiation.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int main() +{ + unsigned long long n, k; + + cin >> n >> k; + + if (n < 1 || n > 15 || k < 1 || k > 15) + { + return 1; + } + + unsigned long long r = 1; + + while (k > 0) + { + if (k % 2 == 1) + { + r *= n; + } + n *= n; + k /= 2; + } + + cout << r << endl; + + return 0; +} diff --git a/programs/3_bst.cpp b/programs/3_bst.cpp new file mode 100644 index 0000000..8c638e2 --- /dev/null +++ b/programs/3_bst.cpp @@ -0,0 +1,165 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct BSTNode { + string word; + BSTNode* left; + BSTNode* right; + BSTNode(const string& w) : word(w), left(nullptr), right(nullptr) {} +}; + +class BST { + BSTNode* root; + + bool insertNode(BSTNode*& node, const string& w) { + if (!node) { + node = new BSTNode(w); + return true; + } + if (w == node->word) return false; + if (w < node->word) return insertNode(node->left, w); + return insertNode(node->right, w); + } + + bool removeNode(BSTNode*& node, const string& w) { + if (!node) return false; + if (w < node->word) return removeNode(node->left, w); + if (w > node->word) return removeNode(node->right, w); + if (!node->left) { + BSTNode* temp = node->right; + delete node; + node = temp; + } else if (!node->right) { + BSTNode* temp = node->left; + delete node; + node = temp; + } else { + BSTNode* minNode = findMin(node->right); + node->word = minNode->word; + removeNode(node->right, minNode->word); + } + return true; + } + + BSTNode* findMin(BSTNode* node) { + if (!node) return nullptr; + while (node->left) node = node->left; + return node; + } + + void clearTree(BSTNode*& node) { + if (!node) return; + clearTree(node->left); + clearTree(node->right); + delete node; + node = nullptr; + } + + void inorder(BSTNode* node, vector& result) const { + if (!node) return; + inorder(node->left, result); + result.push_back(node->word); + inorder(node->right, result); + } + +public: + BST() : root(nullptr) {} + bool insert(const string& w) { return insertNode(root, w); } + bool remove(const string& w) { return removeNode(root, w); } + void clear() { clearTree(root); } + vector getAllWords() const { + vector result; + inorder(root, result); + return result; + } +}; + +int main() { + SetConsoleCP(1251); + SetConsoleOutputCP(1251); + setlocale(LC_ALL, "Russian"); + BST dictionary; + + cout << "Команды:\n" + << "0 - очистить словарь;\n" + << "1,<строка> - добавить строку;\n" + << "2,<строка> - удалить строку;\n" + << "3 - вывести все слова;\n" + << "4 - завершить работу.\n\n"; + + while (true) { + string input; + if (!getline(cin, input)) break; + if (input.empty()) { + cout << "Некорректный ввод!\n"; + continue; + } + int commaPos = input.find(','); + int command; + string cmdStr, word; + if (commaPos == (int)string::npos) { + cmdStr = input; + } else { + cmdStr = input.substr(0, commaPos); + if (commaPos + 1 < (int)input.size()) { + word = input.substr(commaPos + 1); + } + } + try { command = stoi(cmdStr); } + catch (...) { + cout << "Некорректный ввод!\n"; + continue; + } + + switch (command) { + case 0: + dictionary.clear(); + cout << "Словарь очищен\n"; + break; + case 1: + if (word.empty()) { + cout << "Некорректный ввод!\n"; + break; + } + if (dictionary.insert(word)) + cout << "Строка \"" << word << "\" добавлена в словарь\n"; + else + cout << "Строка \"" << word << "\" уже есть в словаре\n"; + break; + case 2: + if (word.empty()) { + cout << "Некорректный ввод!\n"; + break; + } + if (dictionary.remove(word)) + cout << "Строка \"" << word << "\" удалена из словаря\n"; + else + cout << "Строка \"" << word << "\" не найдена в словаре\n"; + break; + case 3: { + vector allWords = dictionary.getAllWords(); + if (allWords.empty()) { + cout << "Словарь пуст\n"; + break; + } + for (int i = 0; i < (int)allWords.size(); i++) { + cout << allWords[i]; + if (i + 1 < (int)allWords.size()) cout << ", "; + } + cout << "\n"; + break; + } + case 4: + return 0; + default: + cout << "Некорректный ввод!\n"; + break; + } + } + return 0; +} \ No newline at end of file