commit 9c1fece35b6e70ccaeba493ce727bde3a08602e0 Author: Arity-T Date: Sun Mar 9 11:12:41 2025 +0300 Добавил программы по тестированию 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