Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <locale>
  4. #include <string>
  5. #include <cstdio>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. int space()
  11. {
  12. int s = 1;
  13. cout << endl;
  14. setlocale(0, ".866");
  15. for (int i = 0; i < 25; i++)
  16. cout << (char)196;
  17. setlocale(0, "Rus");
  18. cout << endl;
  19. return s;
  20. }
  21. struct addBook
  22. {
  23. string fam;
  24. string ini;
  25. string bkn;
  26. string pub;
  27. int year = 0;
  28. };
  29. int startPrg()
  30. {
  31. int num;
  32. setlocale(0, "Rus");
  33. cout << "Библиографическая карточка." << endl;
  34. cout << "1. Добавление." << endl;
  35. cout << "2. Поиск по названию книги." << endl;
  36. cout << "3. Вывод на экран списка книг." << endl;
  37. cout << "4. Завершение работы." << endl;
  38. cout << endl << "Выберите команду (1 - 4): ";
  39. do
  40. cin >> num;
  41. while (num >= 5 || num <= 0);
  42. return num;
  43. }
  44. int main()
  45. {
  46. switch (startPrg())
  47. {
  48. case 1:
  49. {
  50. // Добавление книги
  51. space();
  52. cout << endl << "Добавление книги: " << endl;
  53. addBook abk;
  54. setlocale(LC_ALL, "Rus");
  55. getline(cin, abk.fam);
  56. cout << endl;
  57. cout << "Введите фамилию автора: ";
  58. getline(cin, abk.fam);
  59. cout << "Введите инициалы автора: ";
  60. getline(cin, abk.ini);
  61. cout << "Введите название книги: ";
  62. getline(cin, abk.bkn);
  63. cout << "Введите название издательства: ";
  64. getline(cin, abk.pub);
  65. cout << "Введите год издания книги: ";
  66. cin >> abk.year;
  67. ofstream out("LibrCard.txt", ios::app);
  68. out << " - " << abk.bkn << " / ";
  69. out << abk.fam << ' ' << abk.ini << " / ";
  70. out << abk.pub << " / ";
  71. out << abk.year << " ; " << endl;
  72. out.close();
  73. space();
  74. return main();
  75. }
  76. case 2:
  77. {
  78. // Поиск по названию книги
  79. string textArch, sym1 = { ";" }, bookSearch, sym = { "\n" };
  80. int a = 0;
  81. ifstream in("LibrCard.txt");
  82. string buffer = "";
  83.  
  84. if (in.is_open())
  85. {
  86. space();
  87. cout << endl << "Введите название книги: ";
  88. getline(cin, bookSearch);
  89. getline(cin, bookSearch);
  90. cout << "Результаты поиска..." << endl;
  91. cout << endl;
  92. while (!in.eof())
  93. {
  94. in >> textArch;
  95.  
  96. // Skip everything after / (all info besides book name)
  97. if (textArch == "/") {
  98. while (textArch != sym1)
  99. {
  100.  
  101. in >> textArch;
  102.  
  103. if (in.eof()) {
  104. break;
  105. }
  106. }
  107. }
  108.  
  109.  
  110. if (bookSearch == textArch)
  111. {
  112. if (buffer.length() > 0) {
  113. cout << " " << buffer;
  114. buffer = "";
  115. } else {
  116. cout << bookSearch << ' ';
  117. }
  118. a++;
  119. while (textArch != sym)
  120. {
  121. if (textArch == sym1) {
  122. buffer = "";
  123. break;
  124. }
  125. else
  126. {
  127. cout << textArch << ' ';
  128. in >> textArch;
  129. }
  130. if (in.eof()) {
  131. break;
  132. }
  133. }
  134. cout << endl;
  135. } else if (textArch == sym1) {
  136. buffer = "";
  137. } else {
  138. buffer += textArch + " ";
  139. }
  140.  
  141. }
  142. if (a == 0)
  143. cout << "Книги с данным названием нет." << endl;
  144. space();
  145. return main();
  146. }
  147. else
  148. {
  149. space();
  150. cout << endl << "Ошибка открытия файла!" << endl;
  151. space();
  152. return main();
  153. }
  154. space();
  155. }
  156. case 3:
  157. {
  158. string text, yearSearch;
  159. string years = { "2019" }, sym3 = { ";" }, sym4 = { "-" }, pos;
  160. int count = 0, countstr = 0, numb;
  161. space();
  162. cout << endl << "Выберите вид вывода: " << endl;
  163. cout << "1. Вывод на экран списка книг, которые не старше пяти лет." << endl;
  164. cout << "2. Вывод на экран всего списка книг, где записи упорядочены по фамилии авторов книг в алфавитном порядке." << endl;
  165. cout << endl << "Выберите команду (1 - 2): ";
  166. do
  167. cin >> numb;
  168. while (numb != 1 && numb != 2);
  169. ifstream in("LibrCard.txt");
  170. if (in.is_open())
  171. {
  172. if (numb == 1)
  173. {
  174. cout << endl;
  175. while (!in.eof())
  176. {
  177. in >> text;
  178. if (text == sym4)
  179. in.tellg();
  180. if (text == sym3)
  181. break;
  182. else
  183. {
  184. if (text == "2019")
  185. {
  186. in.seekg(0);
  187. while (text != sym3)
  188. {
  189. in >> text;
  190. if (text == sym3)
  191. break;
  192. else
  193. cout << text << ' ';
  194. if (in.eof())
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. cout << endl;
  201. space();
  202. return main();
  203. }
  204. if (numb == 2)
  205. {
  206. //
  207. space();
  208. return main();
  209. }
  210. }
  211. else
  212. cout << "Ошибка открытия файла!" << endl;
  213. space();
  214. return main();
  215. }
  216. case 4:
  217. {
  218. // Завершение работы
  219. space();
  220. cout << endl << "Завершение работы." << endl;
  221. space();
  222. return 0;
  223. }
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement