Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <Windows.h>
- bool isEmpty(const std::string& FileName) {
- std::fstream File(FileName);
- File.seekp(0, std::ios::end);
- size_t tmp = File.tellg();
- return tmp ? false : true;
- }
- bool numberVerification(int number,int f_bound,int s_bound) {
- if (std::cin.peek() != '\n' || number <= f_bound || number >s_bound) {
- std::cin.clear();
- while (std::cin.get() != '\n');
- std::cout << " Введите реальный возраст: " << std::endl;
- return false;
- }
- return true;
- }
- class Human {
- private:
- std::string m_Name = "default";
- int m_Age = 0;
- public:
- Human() {}
- ~Human() {}
- void Enter() {
- std::cout << "Введите имя: ";
- std::cin.clear();
- std::cin >> m_Name;
- std::cout << "Введите возраст: ";
- std::cin.clear();
- do {
- std::cin >> m_Age;
- } while (!numberVerification(m_Age,0,150));
- }
- void EnterToFile(const std::string& FileName) {
- std::ofstream InFile(FileName, std::ios::app);
- if (isEmpty(FileName)) {
- InFile << m_Name << '\n' << m_Age;
- }
- else {
- InFile << '\n' << m_Name << '\n' << m_Age;
- }
- }
- void DisplayFromFile(const std::string& FileName) {
- std::ifstream OutFile(FileName);
- if (!OutFile) {
- std::cout << "Файла нет\n";
- Sleep(1000);
- return;
- }
- std::string tmp;
- bool name = true;
- while (std::getline(OutFile, tmp)) {
- if (name) {
- std::cout << "Имя: " << tmp << std::endl;
- name = false;
- }
- else {
- std::cout << "Возраст: " << tmp << std::endl;
- name = true;
- }
- }
- }
- void ReverseDisplayFromFile(const std::string& FileName) {
- std::ifstream OutFile(FileName);
- OutFile.seekg(0, OutFile.end);
- size_t length = OutFile.tellg();
- char tempC;
- std::string Result;
- std::string TempStr;
- std::string Temp;
- while (length--) {
- OutFile.seekg(length, OutFile.beg);
- OutFile.get(tempC);
- if (tempC == '\n') {
- length--;
- Temp = TempStr;
- for (size_t i = 0; i < TempStr.size(); i++) {
- Temp[i] = TempStr[TempStr.size() - i - 1];
- }
- TempStr = "";
- Result += Temp;
- }
- TempStr += tempC;
- }
- Temp = TempStr;
- for (size_t i = 0; i < TempStr.size(); i++) {
- Temp[i] = TempStr[TempStr.size() - i - 1];
- }
- Result += Temp;
- std::cout << Result << "\n";
- }
- void Search(const std::string& FileName, const int& age) {
- std::ifstream OutFile(FileName);
- std::vector<std::string> str;
- OutFile >> str.at(0);
- OutFile >> str.at(1);
- if (std::stoi(str.at(1)) == age) {
- std::cout << str.at(0) << '\n' << str.at(1) << '\n';
- }
- }
- void Search(const std::string& FileName, const std::string& name) {
- std::ifstream OutFile(FileName);
- std::vector<std::string> str;
- str.emplace_back();
- str.emplace_back();
- while (!OutFile.eof()) {
- OutFile >> str.at(0);
- OutFile >> str.at(1);
- if (str.at(0) == name) {
- std::cout << str.at(0) << '\n' << str.at(1) << '\n';
- }
- }
- }
- void DeletFileContet(const std::string& FileName) {
- std::ifstream OutFile(FileName, std::ios::trunc);
- }
- void DeleteFound(const std::string& FileName, const std::string& name) {
- std::ifstream Input(FileName);
- std::string tmp = "1" + FileName;
- std::ofstream Output(tmp);
- std::vector<std::string> str;
- str.emplace_back();
- str.emplace_back();
- bool flag = false;
- while (!Input.eof()) {
- Input >> str.at(0);
- Input >> str.at(1);
- if (!(str.at(0) == name)) {
- //Output.close();
- if (!flag) {
- Output << str.at(0) << "\n" << atoi(str.at(1).c_str());
- flag = 1;
- }
- else {
- Output << "\n" << str.at(0) << "\n" << atoi(str.at(1).c_str());
- }
- }
- }
- Input.close();
- Output.close();
- Input.open(tmp);
- Output.open(FileName, std::ios::trunc);
- std::string tmp1;
- while (std::getline(Input, tmp1)) {
- Output << tmp1;
- }
- }
- void DeleteFound(const std::string& FileName, const int &age_) {
- std::string age = std::to_string(age_);
- std::ifstream Input(FileName);
- std::string tmp = "1" + FileName;
- std::ofstream Output(tmp);
- std::vector<std::string> str;
- str.emplace_back();
- str.emplace_back();
- bool flag = false;
- while (!Input.eof()) {
- Input >> str.at(0);
- Input >> str.at(1);
- if (!(str.at(1) == age)) {
- //Output.close();
- if (!flag) {
- Output << str.at(0) << "\n" << atoi(str.at(1).c_str());
- flag = 1;
- }
- else {
- Output << "\n" << str.at(0) << "\n" << atoi(str.at(1).c_str());
- }
- }
- }
- Input.close();
- Output.close();
- Input.open(tmp);
- Output.open(FileName, std::ios::trunc);
- std::string tmp1;
- std::cout << "\n\n\n";
- while (std::getline(Input, tmp1)) {
- std::cout << tmp1;
- Output << tmp1;
- }
- }
- };
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- Human human;
- std::string name;
- int age;
- int chose_ = 0, chose = 0;
- do {
- system("CLS");
- std::cout << "1) Внести информацию в файл\n2) Поиск в файле\n3) Удалить данные из файла\n4) Показать содержимое файла\n5) Удалить файл\n6) Выход из программы\n";
- do {
- std::cout << "Выбор: ";
- std::cin >> chose;
- } while (!numberVerification(chose, 0, 6));
- switch (chose) {
- case 1:
- human.Enter();
- human.EnterToFile("text.txt");
- break;
- case 2:
- if (isEmpty("text.txt")) {
- std::cout << "Файл пуст\n";
- Sleep(1000);
- continue;
- }
- std::cout << "1) Поиск по имени\n2) Поиск по возрасту\n";
- do {
- std::cout << "Выбор: ";
- std::cin.clear();
- std::cin >> chose_;
- } while (!numberVerification(chose_, 0, 2));
- switch (chose_) {
- case 1:
- std::cout << "Имя: ";
- std::cin.clear();
- std::cin >> name;
- human.Search("text.txt",name);
- break;
- case 2:
- do {
- std::cout << "Возраст: ";
- std::cin.clear();
- std::cin >> age;
- } while (!numberVerification(age, 0, 150));
- human.Search("text.txt",age);
- break;
- }
- Sleep(3000);
- chose_ = 0;
- break;
- case 3:
- if (isEmpty("text.txt")) {
- std::cout << "Файл пуст\n";
- Sleep(1000);
- continue;
- }
- std::cout << "1) Удаление по имени\n2) Удаление по возрасту\n";
- do {
- std::cout << "Выбор: ";
- std::cin.clear();
- std::cin >> chose_;
- } while (!numberVerification(chose_, 0, 2));
- switch (chose_) {
- case 1:
- std::cout << "Имя: ";
- std::cin.clear();
- std::cin >> name;
- human.DeleteFound("text.txt", name);
- break;
- case 2:
- do {
- std::cout << "Возраст: ";
- std::cin.clear();
- std::cin >> age;
- } while (!numberVerification(age, 0, 150));
- human.DeleteFound("text.txt", age);
- break;
- }
- std::cout << "Успешно удалено\n";
- Sleep(3000);
- chose_ = 0;
- break;
- case 4:
- if (isEmpty("text.txt")) {
- std::cout << "Файл пуст\n";
- Sleep(1000);
- continue;
- }
- human.DisplayFromFile("text.txt");
- Sleep(3000);
- break;
- case 5:
- remove("text.txt");
- std::cout << "Успешно удалено\n";
- Sleep(1000);
- break;
- }
- } while (chose != 6);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement