Created
November 1, 2025 09:57
-
-
Save vlaleli/9724b24253c031e4910de8f2c4f3faa6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iomanip> | |
| #include <iostream> | |
| #include "BST.h" | |
| BST::BST() : root(nullptr) {} | |
| BST::~BST() { destroy(root); } | |
| void BST::destroy(Node* n) { | |
| if (!n) return; | |
| destroy(n->left); | |
| destroy(n->right); | |
| delete n; | |
| } | |
| BST::Node* BST::insert(Node* n, int key, const Violation& v) { | |
| if (!n) return new Node(key, v); | |
| if (key < n->key) n->left = insert(n->left, key, v); | |
| else if (key > n->key) n->right = insert(n->right, key, v); | |
| else n->list.push_back(v); | |
| return n; | |
| } | |
| void BST::insert(int plate, const Violation& v) { root = insert(root, plate, v); } | |
| const std::vector<Violation>* BST::find(Node* n, int key) { | |
| if (!n) return nullptr; | |
| if (key < n->key) return find(n->left, key); | |
| if (key > n->key) return find(n->right, key); | |
| return &n->list; | |
| } | |
| const std::vector<Violation>* BST::find(int plate) const { return find(root, plate); } | |
| static void printList(const std::vector<Violation>& list, std::ostream& out) { | |
| for (const auto& v : list) { | |
| out << " - " << v.date << ", " << v.summary | |
| << ", штраф: " << std::fixed << std::setprecision(2) | |
| << v.amount << "\n"; | |
| } | |
| } | |
| void BST::inorder(Node* n, std::ostream& out) { | |
| if (!n) return; | |
| inorder(n->left, out); | |
| out << n->key << ":\n"; | |
| printList(n->list, out); | |
| inorder(n->right, out); | |
| } | |
| void BST::printAll(std::ostream& out) const { inorder(root, out); } | |
| void BST::printByNumber(int plate, std::ostream& out) const { | |
| const auto* p = find(plate); | |
| if (!p) { | |
| out << "Номер не найден\n"; | |
| return; | |
| } | |
| out << plate << ":\n"; | |
| printList(*p, out); | |
| } | |
| void BST::printRange(Node* n, int L, int R, std::ostream& out) { | |
| if (!n) return; | |
| if (L < n->key) printRange(n->left, L, R, out); | |
| if (L <= n->key && n->key <= R) { | |
| out << n->key << ":\n"; | |
| printList(n->list, out); | |
| } | |
| if (n->key < R) printRange(n->right, L, R, out); | |
| } | |
| void BST::printByRange(int left, int right, std::ostream& out) const { | |
| printRange(root, left, right, out); | |
| } | |
| void BST::clear() { | |
| destroy(root); | |
| root = nullptr; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <vector> | |
| #include <ostream> | |
| #include "Violation.h" | |
| class BST { | |
| public: | |
| BST(); | |
| ~BST(); | |
| void insert(int plate, const Violation& v); | |
| const std::vector<Violation>* find(int plate) const; | |
| void printAll(std::ostream& out) const; | |
| void printByNumber(int plate, std::ostream& out) const; | |
| void printByRange(int left, int right, std::ostream& out) const; | |
| void clear(); | |
| private: | |
| struct Node { | |
| int key; | |
| std::vector<Violation> list; | |
| Node* left; | |
| Node* right; | |
| Node(int k, const Violation& v) : key(k), list{v}, left(nullptr), right(nullptr) {} | |
| }; | |
| Node* root; | |
| static void destroy(Node* n); | |
| static Node* insert(Node* n, int key, const Violation& v); | |
| static const std::vector<Violation>* find(Node* n, int key); | |
| static void inorder(Node* n, std::ostream& out); | |
| static void printRange(Node* n, int L, int R, std::ostream& out); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <sstream> | |
| #include "BST.h" | |
| static Violation makeViolation(const std::string& date, const std::string& summary, double amount) { | |
| Violation v{date, summary, amount}; | |
| return v; | |
| } | |
| static void printHelp() { | |
| std::cout << "Команды:\n"; | |
| std::cout << "ADD <plate:int> <date> <amount:double> <summary>\n"; | |
| std::cout << "PRINT_ALL\n"; | |
| std::cout << "PRINT <plate:int>\n"; | |
| std::cout << "PRINT_RANGE <L:int> <R:int>\n"; | |
| std::cout << "CLEAR\n"; | |
| std::cout << "HELP\n"; | |
| std::cout << "EXIT\n"; | |
| } | |
| int main() { | |
| std::ios::sync_with_stdio(false); | |
| std::cin.tie(nullptr); | |
| BST db; | |
| printHelp(); | |
| std::string line; | |
| while (true) { | |
| std::cout << "> "; | |
| if (!std::getline(std::cin, line)) break; | |
| if (line.empty()) continue; | |
| std::istringstream iss(line); | |
| std::string cmd; | |
| iss >> cmd; | |
| if (cmd == "ADD") { | |
| int plate; | |
| std::string date; | |
| double amount; | |
| if (!(iss >> plate >> date >> amount)) { | |
| std::cout << "Ошибка формата\n"; | |
| continue; | |
| } | |
| std::string rest; | |
| std::getline(iss, rest); | |
| if (!rest.empty() && rest[0] == ' ') rest.erase(0, 1); | |
| db.insert(plate, makeViolation(date, rest, amount)); | |
| std::cout << "OK\n"; | |
| } else if (cmd == "PRINT_ALL") { | |
| db.printAll(std::cout); | |
| } else if (cmd == "PRINT") { | |
| int plate; | |
| if (!(iss >> plate)) { | |
| std::cout << "Ошибка формата\n"; | |
| continue; | |
| } | |
| db.printByNumber(plate, std::cout); | |
| } else if (cmd == "PRINT_RANGE") { | |
| int L, R; | |
| if (!(iss >> L >> R)) { | |
| std::cout << "Ошибка формата\n"; | |
| continue; | |
| } | |
| if (L > R) std::swap(L, R); | |
| db.printByRange(L, R, std::cout); | |
| } else if (cmd == "CLEAR") { | |
| db.clear(); | |
| std::cout << "OK\n"; | |
| } else if (cmd == "HELP") { | |
| printHelp(); | |
| } else if (cmd == "EXIT") { | |
| break; | |
| } else { | |
| std::cout << "Неизвестная команда. Введите HELP\n"; | |
| } | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <string> | |
| struct Violation { | |
| std::string date; | |
| std::string summary; | |
| double amount; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment