Advertisement
Guest User

Untitled

a guest
May 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4. #include <windows.h>
  5. #define FALSE 0
  6. #define TRUE 1
  7. using namespace std;
  8.  
  9. int min1(int first, int second)
  10. {
  11. if (first < second)
  12. return first;
  13. if (first > second)
  14. return second;
  15. return first;
  16. }
  17.  
  18. class String
  19. {
  20. char* string;
  21. public:
  22. String() { string = NULL; }
  23. String(const char *str)
  24. {
  25. string = new char[strlen(str) + 1];
  26. strcpy(string, str);
  27. string[strlen(str)] = '\0';
  28. }
  29. ~String() { if (string) delete[] string; }
  30. void print() { cout << string << endl; }
  31. const char* getString()
  32. {
  33. if (string)
  34. return string;
  35. else
  36. return NULL;
  37. }
  38. bool operator==(String &s2)
  39. {
  40. bool Bool = 0;
  41. if (strlen(string) == strlen(s2.getString()))
  42. {
  43. Bool = 1;
  44. for (int i = 0; i < strlen(string); i++)
  45. if (string[i] != s2.getString()[i])
  46. Bool = 0;
  47. }
  48. return Bool;
  49. }
  50. bool operator<(String &s2)
  51. {
  52. bool Bool = 0;
  53. for (int i = 0; i < min1(strlen(this->string), strlen(s2.getString())); i++)
  54. {
  55. if (this->string[i] == s2.getString()[i])
  56. {
  57. Bool = 1;
  58. continue;
  59. }
  60. if (this->string[i] < s2.getString()[i])
  61. {
  62. Bool = 0;
  63. return TRUE;
  64. }
  65. if (this->string[i] > s2.getString()[i])
  66. {
  67. Bool = 0;
  68. return FALSE;
  69. }
  70. }
  71. if (Bool)
  72. if (strlen(string) < strlen(s2.getString()))
  73. return TRUE;
  74. else
  75. return FALSE;
  76. return FALSE;
  77. }
  78. bool operator>(String &s2)
  79. {
  80. bool Bool = 0;
  81. for (int i = 0; i < min1(strlen(this->string), strlen(s2.getString())); i++)
  82. {
  83. if (this->string[i] == s2.getString()[i])
  84. {
  85. Bool = 1;
  86. continue;
  87. }
  88. if (this->string[i] > s2.getString()[i])
  89. {
  90. Bool = 0;
  91. return TRUE;
  92. }
  93. if (this->string[i] < s2.getString()[i])
  94. {
  95. Bool = 0;
  96. return FALSE;
  97. }
  98. }
  99. if (Bool)
  100. if (strlen(string) > strlen(s2.getString()))
  101. return TRUE;
  102. else
  103. return FALSE;
  104. return FALSE;
  105. }
  106. String& operator=(String &s2)
  107. {
  108. if (!s2.getString())
  109. {
  110. delete[] string;
  111. return *this;
  112. }
  113. if (string) delete[] string;
  114. string = new char[strlen(s2.getString())+1];
  115. strcpy(string, s2.getString());
  116. string[strlen(s2.getString())] = '\0';
  117. return *this;
  118. }
  119. String& operator=(const char *str2)
  120. {
  121. if (!str2)
  122. {
  123. delete[] string;
  124. return *this;
  125. }
  126. if (string) delete[] string;
  127. string = new char[strlen(str2) + 1];
  128. strcpy(string, str2);
  129. string[strlen(str2)] = '\0';
  130. return *this;
  131. }
  132.  
  133. String operator+(String &s2)
  134. {
  135. char buf[255];
  136. strcpy(buf, string);
  137. return String(strcat(buf, s2.getString()));
  138. }
  139. friend ostream& operator<<(ostream& c0ut, String &s1);
  140. friend istream& operator >> (istream& c1n, String &s1);
  141. };
  142. ostream& operator<<(ostream& c0ut, String &s1)
  143. {
  144. if (!s1.getString())
  145. {
  146. c0ut << "Элемент пуст";
  147. return c0ut;
  148. }
  149. c0ut << s1.getString();
  150. return c0ut;
  151. }
  152. istream& operator>>(istream& c1n, String &s1)
  153. {
  154. char buf[256];
  155. c1n.getline(buf, 256);
  156. s1 = buf;
  157. return c1n;
  158. }
  159.  
  160. class Elem
  161. {
  162. public:
  163. String name;
  164. String number;
  165. String birthdate;
  166. public:
  167. Elem()
  168. {
  169. name = String();
  170. number = String();
  171. birthdate = String();
  172. }
  173. Elem(String name, String number, String birthdate)
  174. {
  175. this->name = name;
  176. this->number = number;
  177. this->birthdate = birthdate;
  178. }
  179. ~Elem() {}
  180. const char* getName() { return name.getString(); }
  181. const char* getNumber() { return number.getString(); }
  182. const char* getBirthDate() { return birthdate.getString(); }
  183. String getNameS() { return name; }
  184. String getNumberS() { return number; }
  185. String getBirthDateS() { return birthdate; }
  186. void print()
  187. {
  188. cout << "Name: " << name << endl << "Number: " << number << endl << "Birth date: " << birthdate << endl;
  189. }
  190. friend istream& operator>>(istream &c1n, Elem &n1);
  191. friend ostream& operator<<(ostream &c0ut, Elem &n1);
  192. friend ofstream& operator<<(ofstream &ofstr, Elem &n1);
  193. };
  194. istream& operator>> (istream &c1n, Elem &n1)
  195. {
  196. cout << "Enter name: ";
  197. c1n >> n1.name;
  198. cout << "Enter number: ";
  199. c1n >> n1.number;
  200. cout << "Enter birth date: ";
  201. c1n >> n1.birthdate;
  202. return c1n;
  203. }
  204. ostream& operator<<(ostream &c0ut, Elem &n1)
  205. {
  206. if (!n1.name.getString())
  207. {
  208. c0ut << "Элемент пуст.";
  209. return c0ut;
  210. }
  211. c0ut << "Name: " << n1.name << endl << "Number: " << n1.number << endl << "Birth date: " << n1.birthdate << endl;
  212. return c0ut;
  213. }
  214. ofstream& operator<<(ofstream &ofstr, Elem &n1)
  215. {
  216. ofstr << n1.name << endl;
  217. ofstr << n1.number << endl;
  218. ofstr << n1.birthdate << endl;
  219. return ofstr;
  220. }
  221.  
  222. template <class Tkey, class Tvalue>
  223. class Dictionary
  224. {
  225. struct KeyValue
  226. {
  227. Tkey key;
  228. Tvalue value;
  229. };
  230. KeyValue *arr;
  231. int current;
  232. int size;
  233. public:
  234. Dictionary()
  235. {
  236. size = 1;
  237. current = 0;
  238. arr = new KeyValue[size];
  239. }
  240. Dictionary(int size)
  241. {
  242. arr = new KeyValue[size];
  243. this->size = size;
  244. current = 0;
  245. }
  246. ~Dictionary() { if (arr) delete[] arr; }
  247. bool add(Tkey &key, Tvalue &value)
  248. {
  249. for (int i = 0; i < current; i++)
  250. if (arr[i].key == key)
  251. return FALSE;
  252. if (current == size)
  253. {
  254. KeyValue *temp = new KeyValue[size + 1];
  255. for (int i = 0; i < size; i++)
  256. temp[i] = arr[i];
  257. delete[] arr;
  258. arr = temp;
  259. arr[current].key = key;
  260. arr[current].value = value;
  261. size++;
  262. current++;
  263. return TRUE;
  264. }
  265. arr[current].key = key;
  266. arr[current].value = value;
  267. current++;
  268. return 1;
  269. }
  270. Tvalue& getValue(int i){return arr[i].value;}
  271. void sort()
  272. {
  273. for (int i = 0; i<current; i++)
  274. for (int j = 1; j<current; j++)
  275. if (arr[j - 1].key > arr[j].key)
  276. {
  277. KeyValue tmp;
  278. tmp = arr[j - 1];
  279. arr[j - 1] = arr[j];
  280. arr[j] = tmp;
  281. }
  282. }
  283. bool deleteEl(Tkey &key)
  284. {
  285. int pos = -1;
  286. if (!current)
  287. return FALSE;
  288. for (int i = 0; i<size; i++)
  289. if (arr[i].key == key)
  290. {
  291. pos = i;
  292. break;
  293. }
  294. if (pos < 0)
  295. return FALSE;
  296. for (int i = pos+1; i < size; i++)
  297. arr[i - 1] = arr[i];
  298. size--;
  299. current--;
  300. KeyValue *tmp = new KeyValue[size];
  301. for (int i = 0; i < size; i++)
  302. tmp[i] = arr[i];
  303. delete[] arr;
  304. arr = tmp;
  305. return TRUE;
  306. }
  307. bool deleteALL()
  308. {
  309. if (!arr)
  310. return FALSE;
  311. if (arr) delete[] arr;
  312. current = 0;
  313. size = 1;
  314. arr = new KeyValue[size];
  315. return TRUE;
  316. }
  317. void print()
  318. {
  319. if (!current)
  320. {
  321. cout << "Записей не найденно!" << endl;
  322. return;
  323. }
  324. for (short i = 0; i < current; i++)
  325. cout << arr[i].value << endl;
  326. }
  327. void save(ofstream& stream)
  328. {
  329. for (int i = 0; i < current; i++)
  330. stream << arr[i].value;
  331. }
  332. Tvalue& operator[](Tkey &keyfind)
  333. {
  334. if (!current)
  335. {
  336. Tvalue *tmp = new Tvalue();
  337. Tvalue &empty = *tmp;
  338. return empty;
  339. }
  340.  
  341. for (int i = 0; i < size; i++)
  342. if (arr[i].key == keyfind)
  343. return arr[i].value;
  344. Tvalue *tmp = new Tvalue();
  345. Tvalue &empty = *tmp;
  346. return empty;
  347. }
  348. };
  349. class Notebook
  350. {
  351. int count;
  352. Dictionary<String, Elem> dictionary;
  353. public:
  354. Notebook() { count = 0; }
  355. bool add(String &s1, Elem &e2) { if (dictionary.add(s1, e2)) return TRUE; else return FALSE; }
  356. bool deleteEl(String &s1) { if (dictionary.deleteEl(s1)) return TRUE; else return FALSE; }
  357. bool deleteALL() { if (dictionary.deleteALL()) return TRUE; else return FALSE; }
  358. void sort() { dictionary.sort(); }
  359.  
  360. void menu()
  361. {
  362. bool sortM;
  363. cout << "Выберите метод поиска или сортировки: " << endl << "0)По ФИО" << endl << "1)По номеру" << endl;
  364. cin >> sortM;
  365.  
  366. ifstream fileRead("Notebook.txt");
  367. if (fileRead.is_open())
  368. {
  369. char filecount[7];
  370. fileRead.getline(filecount, 7);
  371. count = atoi(filecount);
  372. for (int i = 0; i < count; i++)
  373. {
  374. char bufname[255];
  375. char bufnumber[50];
  376. char bufbirthdate[50];
  377. fileRead.getline(bufname, 255);
  378. fileRead.getline(bufnumber, 50);
  379. fileRead.getline(bufbirthdate, 50);
  380. Elem tmp(bufname, bufnumber, bufbirthdate);
  381. String name;
  382. if (!sortM)
  383. name = bufname;
  384. else
  385. name = bufnumber;
  386. add(name, tmp);
  387. }
  388. }
  389. fileRead.close();
  390.  
  391.  
  392. bool ex1t = 1;
  393. String name;
  394. Elem note;
  395. short menuSelection;
  396. while (ex1t)
  397. {
  398.  
  399. system("cls");
  400. cout << "Записная книжка" << endl << "1)Вывести все записи" << endl << "2)Добавить запись" << endl << "3)Удалить запись" << endl;
  401. if (!sortM)
  402. cout << "4)Поиск по ФИО" << endl << "5)Сортировать по ФИО" << endl;
  403. else
  404. cout << "4)Поиск по номеру" << endl << "5)Сортировать по номеру" << endl;
  405. cout << "6)Удалить все записи" << endl << "7)Сохранение и выход из программы" << endl;
  406. char *buftmp = new char[50];
  407. cin >> buftmp;
  408. menuSelection = atoi(buftmp);
  409. delete[] buftmp;
  410. system("cls");
  411. switch (menuSelection)
  412. {
  413. case 1:
  414. print();
  415. cout << "1)Вернуться в меню" << endl;
  416. buftmp = new char[50];
  417. cin >> buftmp;
  418. menuSelection = atoi(buftmp);
  419. delete[] buftmp;
  420. break;
  421. case 2:
  422. cin.get();
  423. cin >> note;
  424. if (!sortM)
  425. name = note.getName();
  426. else
  427. name = note.getNumber();
  428.  
  429. if (add(name, note))
  430. count++;
  431. break;
  432. case 3:
  433. cin.get();
  434. if (!sortM)
  435. cout << "Введите ФИО элемента, который хотите удалить: ";
  436. else
  437. cout << "Введите номер элемента, который хотите удалить: ";
  438. cin >> name;
  439. if (deleteEl(name))
  440. count--;
  441. break;
  442. case 4:
  443. cin.get();
  444. if (!sortM)
  445. cout << "Введите ФИО искомого элемента: ";
  446. else
  447. cout << "Введите номер искомого элемента: ";
  448. cin >> name;
  449. cout << dictionary[name] << endl;
  450. cout << "1)Вернуться в меню" << endl;
  451. buftmp = new char[50];
  452. cin >> buftmp;
  453. menuSelection = atoi(buftmp);
  454. delete[] buftmp;
  455. break;
  456. case 5:
  457. sort();
  458. break;
  459. case 6:
  460. bool deleteallQuestion;
  461. cout << "Вы уверенны, что хотите удалить все записи?" << endl << "0)Нет" << endl << "1)Да" << endl;
  462. buftmp = new char[50];
  463. cin >> buftmp;
  464. deleteallQuestion = atoi(buftmp);
  465. delete[] buftmp;
  466. if (deleteallQuestion)
  467. if (deleteALL())
  468. count = 0;
  469. break;
  470. case 7:
  471. {
  472. ex1t = 0;
  473. ofstream fileSave;
  474. fileSave.open("Notebook.txt");
  475. fileSave << count << endl;
  476. dictionary.save(fileSave);
  477. fileSave.close();
  478. break;
  479. }
  480. default:
  481. cout << "Неверное действие." << endl << "1)Вернуться в меню" << endl;
  482. buftmp = new char[50];
  483. cin >> buftmp;
  484. menuSelection = atoi(buftmp);
  485. delete[] buftmp;
  486. break;
  487. }
  488. }
  489. }
  490. void print() { dictionary.print(); }
  491. };
  492.  
  493. int main()
  494. {
  495. setlocale(LC_ALL, "rus");
  496.  
  497. Notebook test;
  498. test.menu();
  499. return 0;
  500. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement