Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <Windows.h>
  6.  
  7. bool isEmpty(const std::string& FileName) {
  8. std::fstream File(FileName);
  9. File.seekp(0, std::ios::end);
  10. size_t tmp = File.tellg();
  11. return tmp ? false : true;
  12. }
  13.  
  14. bool numberVerification(int number,int f_bound,int s_bound) {
  15. if (std::cin.peek() != '\n' || number <= f_bound || number >s_bound) {
  16. std::cin.clear();
  17. while (std::cin.get() != '\n');
  18. std::cout << " Введите реальный возраст: " << std::endl;
  19. return false;
  20. }
  21. return true;
  22. }
  23.  
  24. class Human {
  25. private:
  26. std::string m_Name = "default";
  27. int m_Age = 0;
  28. public:
  29. Human() {}
  30.  
  31. ~Human() {}
  32.  
  33. void Enter() {
  34. std::cout << "Введите имя: ";
  35. std::cin.clear();
  36. std::cin >> m_Name;
  37. std::cout << "Введите возраст: ";
  38. std::cin.clear();
  39. do {
  40. std::cin >> m_Age;
  41. } while (!numberVerification(m_Age,0,150));
  42. }
  43.  
  44. void EnterToFile(const std::string& FileName) {
  45. std::ofstream InFile(FileName, std::ios::app);
  46. if (isEmpty(FileName)) {
  47. InFile << m_Name << '\n' << m_Age;
  48. }
  49. else {
  50. InFile << '\n' << m_Name << '\n' << m_Age;
  51. }
  52. }
  53.  
  54. void DisplayFromFile(const std::string& FileName) {
  55. std::ifstream OutFile(FileName);
  56. if (!OutFile) {
  57. std::cout << "Файла нет\n";
  58. Sleep(1000);
  59. return;
  60. }
  61. std::string tmp;
  62. bool name = true;
  63. while (std::getline(OutFile, tmp)) {
  64. if (name) {
  65. std::cout << "Имя: " << tmp << std::endl;
  66. name = false;
  67. }
  68. else {
  69. std::cout << "Возраст: " << tmp << std::endl;
  70. name = true;
  71. }
  72. }
  73. }
  74.  
  75. void ReverseDisplayFromFile(const std::string& FileName) {
  76. std::ifstream OutFile(FileName);
  77. OutFile.seekg(0, OutFile.end);
  78. size_t length = OutFile.tellg();
  79. char tempC;
  80. std::string Result;
  81. std::string TempStr;
  82. std::string Temp;
  83. while (length--) {
  84. OutFile.seekg(length, OutFile.beg);
  85. OutFile.get(tempC);
  86. if (tempC == '\n') {
  87. length--;
  88. Temp = TempStr;
  89. for (size_t i = 0; i < TempStr.size(); i++) {
  90. Temp[i] = TempStr[TempStr.size() - i - 1];
  91. }
  92. TempStr = "";
  93. Result += Temp;
  94. }
  95. TempStr += tempC;
  96. }
  97. Temp = TempStr;
  98. for (size_t i = 0; i < TempStr.size(); i++) {
  99. Temp[i] = TempStr[TempStr.size() - i - 1];
  100. }
  101. Result += Temp;
  102. std::cout << Result << "\n";
  103. }
  104.  
  105. void Search(const std::string& FileName, const int& age) {
  106. std::ifstream OutFile(FileName);
  107. std::vector<std::string> str;
  108. OutFile >> str.at(0);
  109. OutFile >> str.at(1);
  110. if (std::stoi(str.at(1)) == age) {
  111. std::cout << str.at(0) << '\n' << str.at(1) << '\n';
  112. }
  113. }
  114.  
  115. void Search(const std::string& FileName, const std::string& name) {
  116. std::ifstream OutFile(FileName);
  117. std::vector<std::string> str;
  118. str.emplace_back();
  119. str.emplace_back();
  120. while (!OutFile.eof()) {
  121. OutFile >> str.at(0);
  122. OutFile >> str.at(1);
  123. if (str.at(0) == name) {
  124. std::cout << str.at(0) << '\n' << str.at(1) << '\n';
  125. }
  126. }
  127.  
  128. }
  129.  
  130. void DeletFileContet(const std::string& FileName) {
  131. std::ifstream OutFile(FileName, std::ios::trunc);
  132. }
  133.  
  134. void DeleteFound(const std::string& FileName, const std::string& name) {
  135. std::ifstream Input(FileName);
  136. std::string tmp = "1" + FileName;
  137. std::ofstream Output(tmp);
  138. std::vector<std::string> str;
  139. str.emplace_back();
  140. str.emplace_back();
  141. bool flag = false;
  142. while (!Input.eof()) {
  143. Input >> str.at(0);
  144. Input >> str.at(1);
  145. if (!(str.at(0) == name)) {
  146. //Output.close();
  147. if (!flag) {
  148. Output << str.at(0) << "\n" << atoi(str.at(1).c_str());
  149. flag = 1;
  150. }
  151. else {
  152. Output << "\n" << str.at(0) << "\n" << atoi(str.at(1).c_str());
  153. }
  154. }
  155. }
  156. Input.close();
  157. Output.close();
  158. Input.open(tmp);
  159. Output.open(FileName, std::ios::trunc);
  160. std::string tmp1;
  161. while (std::getline(Input, tmp1)) {
  162. Output << tmp1;
  163. }
  164. }
  165.  
  166. void DeleteFound(const std::string& FileName, const int &age_) {
  167. std::string age = std::to_string(age_);
  168. std::ifstream Input(FileName);
  169. std::string tmp = "1" + FileName;
  170. std::ofstream Output(tmp);
  171. std::vector<std::string> str;
  172. str.emplace_back();
  173. str.emplace_back();
  174. bool flag = false;
  175. while (!Input.eof()) {
  176. Input >> str.at(0);
  177. Input >> str.at(1);
  178. if (!(str.at(1) == age)) {
  179. //Output.close();
  180. if (!flag) {
  181. Output << str.at(0) << "\n" << atoi(str.at(1).c_str());
  182. flag = 1;
  183. }
  184. else {
  185. Output << "\n" << str.at(0) << "\n" << atoi(str.at(1).c_str());
  186. }
  187. }
  188. }
  189. Input.close();
  190. Output.close();
  191. Input.open(tmp);
  192. Output.open(FileName, std::ios::trunc);
  193. std::string tmp1;
  194. std::cout << "\n\n\n";
  195. while (std::getline(Input, tmp1)) {
  196. std::cout << tmp1;
  197. Output << tmp1;
  198. }
  199. }
  200. };
  201.  
  202. int main() {
  203. SetConsoleCP(1251);
  204. SetConsoleOutputCP(1251);
  205. Human human;
  206. std::string name;
  207. int age;
  208. int chose_ = 0, chose = 0;
  209. do {
  210. system("CLS");
  211. std::cout << "1) Внести информацию в файл\n2) Поиск в файле\n3) Удалить данные из файла\n4) Показать содержимое файла\n5) Удалить файл\n6) Выход из программы\n";
  212. do {
  213. std::cout << "Выбор: ";
  214. std::cin >> chose;
  215. } while (!numberVerification(chose, 0, 6));
  216. switch (chose) {
  217. case 1:
  218. human.Enter();
  219. human.EnterToFile("text.txt");
  220. break;
  221. case 2:
  222. if (isEmpty("text.txt")) {
  223. std::cout << "Файл пуст\n";
  224. Sleep(1000);
  225. continue;
  226. }
  227. std::cout << "1) Поиск по имени\n2) Поиск по возрасту\n";
  228. do {
  229. std::cout << "Выбор: ";
  230. std::cin.clear();
  231. std::cin >> chose_;
  232. } while (!numberVerification(chose_, 0, 2));
  233. switch (chose_) {
  234. case 1:
  235. std::cout << "Имя: ";
  236. std::cin.clear();
  237. std::cin >> name;
  238. human.Search("text.txt",name);
  239. break;
  240. case 2:
  241. do {
  242. std::cout << "Возраст: ";
  243. std::cin.clear();
  244. std::cin >> age;
  245. } while (!numberVerification(age, 0, 150));
  246. human.Search("text.txt",age);
  247. break;
  248. }
  249. Sleep(3000);
  250. chose_ = 0;
  251. break;
  252. case 3:
  253. if (isEmpty("text.txt")) {
  254. std::cout << "Файл пуст\n";
  255. Sleep(1000);
  256. continue;
  257. }
  258. std::cout << "1) Удаление по имени\n2) Удаление по возрасту\n";
  259. do {
  260. std::cout << "Выбор: ";
  261. std::cin.clear();
  262. std::cin >> chose_;
  263. } while (!numberVerification(chose_, 0, 2));
  264. switch (chose_) {
  265. case 1:
  266. std::cout << "Имя: ";
  267. std::cin.clear();
  268. std::cin >> name;
  269. human.DeleteFound("text.txt", name);
  270. break;
  271. case 2:
  272. do {
  273. std::cout << "Возраст: ";
  274. std::cin.clear();
  275. std::cin >> age;
  276. } while (!numberVerification(age, 0, 150));
  277. human.DeleteFound("text.txt", age);
  278. break;
  279. }
  280. std::cout << "Успешно удалено\n";
  281. Sleep(3000);
  282. chose_ = 0;
  283. break;
  284. case 4:
  285. if (isEmpty("text.txt")) {
  286. std::cout << "Файл пуст\n";
  287. Sleep(1000);
  288. continue;
  289. }
  290. human.DisplayFromFile("text.txt");
  291. Sleep(3000);
  292. break;
  293. case 5:
  294. remove("text.txt");
  295. std::cout << "Успешно удалено\n";
  296. Sleep(1000);
  297. break;
  298. }
  299. } while (chose != 6);
  300. return 0;
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement