Добавь в класс Tree возможность инициализации без входного файла: #include <iostream>…
- User
Добавь в класс Tree возможность инициализации без входного файла: #include #include #include #include #include #include using namespace std;
struct VendorCode { int vendorcode;
VendorCode(int vc) : vendorcode(vc) {}}; struct ProductName { std::string productname;
ProductName(std::string pn) : productname(pn) {}}; struct TIN { long long int tin;
TIN(long long int t) { long long int n = t; int c = 0; while (n != 0) { c++; n /= 10; } if (c == 12) { tin = t; } else { std::cerr << "Invalid TIN. Setting default TIN to 100000000000" << std::endl; tin = 100000000000; } }}; struct Date { int day; int month; int year;
Date(int d, int m, int y) { bool correct = true; if (y > 2024 || y < 1900) correct = false; else { switch (m) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (d < 1 || d > 31) correct = false; break; case 4: case 6: case 9: case 11: if (d < 1 || d > 30) correct = false; break; case 2: if (d < 1 || d > 28) correct = false; break; default: correct = false; break; } } if (correct) { day = d; month = m; year = y; } else { std::cerr << "Invalid date. Setting default date to 1.1.2000." << std::endl; day = 1; month = 1; year = 2000; } }}; struct Sorts { VendorCode vendorcodes; ProductName productnames; TIN tins; Date dates; int firstHash; Sorts(const VendorCode& vc, const ProductName& pn, const TIN& t, const Date& d) : vendorcodes(vc), productnames(pn), tins(t), dates(d), firstHash(0) {} }; struct Cell { Sorts sorts; int flag; int firstHash; Cell() : sorts(VendorCode(0), ProductName(""), TIN(100000000000), Date(1, 1, 2000)), flag(0), firstHash(0) {} }; class HashTable { private: std::vector table; std::vector readFromFile(const std::string& filename) { std::vector sorts; std::ifstream file(filename);
if (file.is_open()) { int vc; std::string pn; int t; int day; int month; int year; while (file >> vc >> pn >> t >> day >> month >> year) { VendorCode vendorcode(vc); ProductName productname(pn); TIN tin(t); Date date(day, month, year); sorts.push_back(Sorts(vendorcode, productname, tin, date)); } file.close(); } else { std::cerr << "Enable open file" << filename << std::endl; } return sorts; } int digitsCount(int n) { int c = 0; while (n != 0) { c++; n /= 10; } return c; } int hash1(int key1, int key2, int key3, int n) { int d = digitsCount(n - 1); int l = digitsCount(key1); int e = (2 * l - d) / 2; int r = pow(10, e); int h1 = (key1 * key1) / r; int mod = pow(10, d); h1 %= mod; l = digitsCount(key2); e = (2 * l - d) / 2; r = pow(10, e); int h2 = (key2 * key2) / r; mod = pow(10, d); h2 %= mod; l = digitsCount(key3); e = (2 * l - d) / 2; r = pow(10, e); int h3 = (key3 * key3) / r; mod = pow(10, d); h3 %= mod; int h = (h1 + h2 + h3) % n; return h; } int hash2(int key1, int key2, int key3, int j, int k, int n) { int hash = (hash1(key1, key2, key3, n) + j * k) % n; return hash; } int collisionInsert(int key1, int key2, int key3, int n, int hash) { int h = hash; int j = 0; int k = 3; do { if (table[h].sorts.dates.day == key1 && table[h].sorts.dates.month == key2 && table[h].sorts.dates.year == key3) return -2; if (table[h].flag != 1) { return h; } else { h = hash2(key1, key2, key3, j, k, n); ++j; } } while (j != n); return -1; } int collisionSearch(int key1, int key2, int key3, int n, int hash) { int h = hash; int j = 0; int k = 3; do { if (table[h].sorts.dates.day == key1 && table[h].sorts.dates.month == key2 && table[h].sorts.dates.year == key3) { return h; } else { h = hash2(key1, key2, key3, j, k, n); ++j; } } while (j != n); return -1; } - Assistant
Для добавления возможности инициализации класса Tree без входного файла, необходимо добавить новый конструктор без параметров, который будет инициализировать пустое дерево. Вот как можно это сделать:
Tree() { root = NULL; }Теперь у вас есть конструктор без параметров, который инициализирует пустое дерево. Вы можете использовать его для создания объектов класса Tree без указания входного файла.
- User
А если при объявлении root сразу указать Node* root = NULL;, то можно ли сделать конструктор Tree() {
}?
- Assistant
Да, если вы объявите
Node* rootвнутри классаTreeи проинициализируете его значениемNULL, то вы можете определить конструкторTree()без тела, как вы указали. В этом случае конструкторTree()будет использовать инициализацию по умолчанию дляroot, которая уже равнаNULL.Вот как это может выглядеть:
class Tree { private: Node* root = NULL; public: Tree() = default; };С использованием инициализации по умолчанию (
= default) для конструктораTree(), вы сможете создавать объекты классаTreeбез явного указания тела конструктора.