Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <fstream>
- #include <windows.h>
- #define FALSE 0
- #define TRUE 1
- using namespace std;
- int min1(int first, int second)
- {
- if (first < second)
- return first;
- if (first > second)
- return second;
- return first;
- }
- class String
- {
- char* string;
- public:
- String() { string = NULL; }
- String(const char *str)
- {
- string = new char[strlen(str) + 1];
- strcpy(string, str);
- string[strlen(str)] = '\0';
- }
- ~String() { if (string) delete[] string; }
- void print() { cout << string << endl; }
- const char* getString()
- {
- if (string)
- return string;
- else
- return NULL;
- }
- bool operator==(String &s2)
- {
- bool Bool = 0;
- if (strlen(string) == strlen(s2.getString()))
- {
- Bool = 1;
- for (int i = 0; i < strlen(string); i++)
- if (string[i] != s2.getString()[i])
- Bool = 0;
- }
- return Bool;
- }
- bool operator<(String &s2)
- {
- bool Bool = 0;
- for (int i = 0; i < min1(strlen(this->string), strlen(s2.getString())); i++)
- {
- if (this->string[i] == s2.getString()[i])
- {
- Bool = 1;
- continue;
- }
- if (this->string[i] < s2.getString()[i])
- {
- Bool = 0;
- return TRUE;
- }
- if (this->string[i] > s2.getString()[i])
- {
- Bool = 0;
- return FALSE;
- }
- }
- if (Bool)
- if (strlen(string) < strlen(s2.getString()))
- return TRUE;
- else
- return FALSE;
- return FALSE;
- }
- bool operator>(String &s2)
- {
- bool Bool = 0;
- for (int i = 0; i < min1(strlen(this->string), strlen(s2.getString())); i++)
- {
- if (this->string[i] == s2.getString()[i])
- {
- Bool = 1;
- continue;
- }
- if (this->string[i] > s2.getString()[i])
- {
- Bool = 0;
- return TRUE;
- }
- if (this->string[i] < s2.getString()[i])
- {
- Bool = 0;
- return FALSE;
- }
- }
- if (Bool)
- if (strlen(string) > strlen(s2.getString()))
- return TRUE;
- else
- return FALSE;
- return FALSE;
- }
- String& operator=(String &s2)
- {
- if (!s2.getString())
- {
- delete[] string;
- return *this;
- }
- if (string) delete[] string;
- string = new char[strlen(s2.getString())+1];
- strcpy(string, s2.getString());
- string[strlen(s2.getString())] = '\0';
- return *this;
- }
- String& operator=(const char *str2)
- {
- if (!str2)
- {
- delete[] string;
- return *this;
- }
- if (string) delete[] string;
- string = new char[strlen(str2) + 1];
- strcpy(string, str2);
- string[strlen(str2)] = '\0';
- return *this;
- }
- String operator+(String &s2)
- {
- char buf[255];
- strcpy(buf, string);
- return String(strcat(buf, s2.getString()));
- }
- friend ostream& operator<<(ostream& c0ut, String &s1);
- friend istream& operator >> (istream& c1n, String &s1);
- };
- ostream& operator<<(ostream& c0ut, String &s1)
- {
- if (!s1.getString())
- {
- c0ut << "Элемент пуст";
- return c0ut;
- }
- c0ut << s1.getString();
- return c0ut;
- }
- istream& operator>>(istream& c1n, String &s1)
- {
- char buf[256];
- c1n.getline(buf, 256);
- s1 = buf;
- return c1n;
- }
- class Elem
- {
- public:
- String name;
- String number;
- String birthdate;
- public:
- Elem()
- {
- name = String();
- number = String();
- birthdate = String();
- }
- Elem(String name, String number, String birthdate)
- {
- this->name = name;
- this->number = number;
- this->birthdate = birthdate;
- }
- ~Elem() {}
- const char* getName() { return name.getString(); }
- const char* getNumber() { return number.getString(); }
- const char* getBirthDate() { return birthdate.getString(); }
- String getNameS() { return name; }
- String getNumberS() { return number; }
- String getBirthDateS() { return birthdate; }
- void print()
- {
- cout << "Name: " << name << endl << "Number: " << number << endl << "Birth date: " << birthdate << endl;
- }
- friend istream& operator>>(istream &c1n, Elem &n1);
- friend ostream& operator<<(ostream &c0ut, Elem &n1);
- friend ofstream& operator<<(ofstream &ofstr, Elem &n1);
- };
- istream& operator>> (istream &c1n, Elem &n1)
- {
- cout << "Enter name: ";
- c1n >> n1.name;
- cout << "Enter number: ";
- c1n >> n1.number;
- cout << "Enter birth date: ";
- c1n >> n1.birthdate;
- return c1n;
- }
- ostream& operator<<(ostream &c0ut, Elem &n1)
- {
- if (!n1.name.getString())
- {
- c0ut << "Элемент пуст.";
- return c0ut;
- }
- c0ut << "Name: " << n1.name << endl << "Number: " << n1.number << endl << "Birth date: " << n1.birthdate << endl;
- return c0ut;
- }
- ofstream& operator<<(ofstream &ofstr, Elem &n1)
- {
- ofstr << n1.name << endl;
- ofstr << n1.number << endl;
- ofstr << n1.birthdate << endl;
- return ofstr;
- }
- template <class Tkey, class Tvalue>
- class Dictionary
- {
- struct KeyValue
- {
- Tkey key;
- Tvalue value;
- };
- KeyValue *arr;
- int current;
- int size;
- public:
- Dictionary()
- {
- size = 1;
- current = 0;
- arr = new KeyValue[size];
- }
- Dictionary(int size)
- {
- arr = new KeyValue[size];
- this->size = size;
- current = 0;
- }
- ~Dictionary() { if (arr) delete[] arr; }
- bool add(Tkey &key, Tvalue &value)
- {
- for (int i = 0; i < current; i++)
- if (arr[i].key == key)
- return FALSE;
- if (current == size)
- {
- KeyValue *temp = new KeyValue[size + 1];
- for (int i = 0; i < size; i++)
- temp[i] = arr[i];
- delete[] arr;
- arr = temp;
- arr[current].key = key;
- arr[current].value = value;
- size++;
- current++;
- return TRUE;
- }
- arr[current].key = key;
- arr[current].value = value;
- current++;
- return 1;
- }
- Tvalue& getValue(int i){return arr[i].value;}
- void sort()
- {
- for (int i = 0; i<current; i++)
- for (int j = 1; j<current; j++)
- if (arr[j - 1].key > arr[j].key)
- {
- KeyValue tmp;
- tmp = arr[j - 1];
- arr[j - 1] = arr[j];
- arr[j] = tmp;
- }
- }
- bool deleteEl(Tkey &key)
- {
- int pos = -1;
- if (!current)
- return FALSE;
- for (int i = 0; i<size; i++)
- if (arr[i].key == key)
- {
- pos = i;
- break;
- }
- if (pos < 0)
- return FALSE;
- for (int i = pos+1; i < size; i++)
- arr[i - 1] = arr[i];
- size--;
- current--;
- KeyValue *tmp = new KeyValue[size];
- for (int i = 0; i < size; i++)
- tmp[i] = arr[i];
- delete[] arr;
- arr = tmp;
- return TRUE;
- }
- bool deleteALL()
- {
- if (!arr)
- return FALSE;
- if (arr) delete[] arr;
- current = 0;
- size = 1;
- arr = new KeyValue[size];
- return TRUE;
- }
- void print()
- {
- if (!current)
- {
- cout << "Записей не найденно!" << endl;
- return;
- }
- for (short i = 0; i < current; i++)
- cout << arr[i].value << endl;
- }
- void save(ofstream& stream)
- {
- for (int i = 0; i < current; i++)
- stream << arr[i].value;
- }
- Tvalue& operator[](Tkey &keyfind)
- {
- if (!current)
- {
- Tvalue *tmp = new Tvalue();
- Tvalue &empty = *tmp;
- return empty;
- }
- for (int i = 0; i < size; i++)
- if (arr[i].key == keyfind)
- return arr[i].value;
- Tvalue *tmp = new Tvalue();
- Tvalue &empty = *tmp;
- return empty;
- }
- };
- class Notebook
- {
- int count;
- Dictionary<String, Elem> dictionary;
- public:
- Notebook() { count = 0; }
- bool add(String &s1, Elem &e2) { if (dictionary.add(s1, e2)) return TRUE; else return FALSE; }
- bool deleteEl(String &s1) { if (dictionary.deleteEl(s1)) return TRUE; else return FALSE; }
- bool deleteALL() { if (dictionary.deleteALL()) return TRUE; else return FALSE; }
- void sort() { dictionary.sort(); }
- void menu()
- {
- bool sortM;
- cout << "Выберите метод поиска или сортировки: " << endl << "0)По ФИО" << endl << "1)По номеру" << endl;
- cin >> sortM;
- ifstream fileRead("Notebook.txt");
- if (fileRead.is_open())
- {
- char filecount[7];
- fileRead.getline(filecount, 7);
- count = atoi(filecount);
- for (int i = 0; i < count; i++)
- {
- char bufname[255];
- char bufnumber[50];
- char bufbirthdate[50];
- fileRead.getline(bufname, 255);
- fileRead.getline(bufnumber, 50);
- fileRead.getline(bufbirthdate, 50);
- Elem tmp(bufname, bufnumber, bufbirthdate);
- String name;
- if (!sortM)
- name = bufname;
- else
- name = bufnumber;
- add(name, tmp);
- }
- }
- fileRead.close();
- bool ex1t = 1;
- String name;
- Elem note;
- short menuSelection;
- while (ex1t)
- {
- system("cls");
- cout << "Записная книжка" << endl << "1)Вывести все записи" << endl << "2)Добавить запись" << endl << "3)Удалить запись" << endl;
- if (!sortM)
- cout << "4)Поиск по ФИО" << endl << "5)Сортировать по ФИО" << endl;
- else
- cout << "4)Поиск по номеру" << endl << "5)Сортировать по номеру" << endl;
- cout << "6)Удалить все записи" << endl << "7)Сохранение и выход из программы" << endl;
- char *buftmp = new char[50];
- cin >> buftmp;
- menuSelection = atoi(buftmp);
- delete[] buftmp;
- system("cls");
- switch (menuSelection)
- {
- case 1:
- print();
- cout << "1)Вернуться в меню" << endl;
- buftmp = new char[50];
- cin >> buftmp;
- menuSelection = atoi(buftmp);
- delete[] buftmp;
- break;
- case 2:
- cin.get();
- cin >> note;
- if (!sortM)
- name = note.getName();
- else
- name = note.getNumber();
- if (add(name, note))
- count++;
- break;
- case 3:
- cin.get();
- if (!sortM)
- cout << "Введите ФИО элемента, который хотите удалить: ";
- else
- cout << "Введите номер элемента, который хотите удалить: ";
- cin >> name;
- if (deleteEl(name))
- count--;
- break;
- case 4:
- cin.get();
- if (!sortM)
- cout << "Введите ФИО искомого элемента: ";
- else
- cout << "Введите номер искомого элемента: ";
- cin >> name;
- cout << dictionary[name] << endl;
- cout << "1)Вернуться в меню" << endl;
- buftmp = new char[50];
- cin >> buftmp;
- menuSelection = atoi(buftmp);
- delete[] buftmp;
- break;
- case 5:
- sort();
- break;
- case 6:
- bool deleteallQuestion;
- cout << "Вы уверенны, что хотите удалить все записи?" << endl << "0)Нет" << endl << "1)Да" << endl;
- buftmp = new char[50];
- cin >> buftmp;
- deleteallQuestion = atoi(buftmp);
- delete[] buftmp;
- if (deleteallQuestion)
- if (deleteALL())
- count = 0;
- break;
- case 7:
- {
- ex1t = 0;
- ofstream fileSave;
- fileSave.open("Notebook.txt");
- fileSave << count << endl;
- dictionary.save(fileSave);
- fileSave.close();
- break;
- }
- default:
- cout << "Неверное действие." << endl << "1)Вернуться в меню" << endl;
- buftmp = new char[50];
- cin >> buftmp;
- menuSelection = atoi(buftmp);
- delete[] buftmp;
- break;
- }
- }
- }
- void print() { dictionary.print(); }
- };
- int main()
- {
- setlocale(LC_ALL, "rus");
- Notebook test;
- test.menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement