Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- const string BDFIle = "StudBD.txt";
- class StudentApplication {
- private:
- int getCountStudent() {
- ifstream bd(BDFIle);
- if (!bd)
- return 0;
- int g = 0;
- string a;
- while (bd >> a) {
- for (int i = 0; i < 5; i++)
- bd >> a;
- g++;
- }
- bd.close();
- return g;
- }
- public:
- string name, surname, patronymic, group, phone;
- int year;//Курс
- bool addInBD() {
- ofstream bd(BDFIle, ios::app);
- bd << surname << " "
- << name << " "
- << name << " "
- << group << " "
- << year << " "
- << phone << endl;
- bd.close();
- return true;
- }
- bool search(string search, bool byGroup) {
- ifstream bd(BDFIle);
- if (!bd) return false;
- StudentApplication stud;
- int print = 0;
- while (bd >> stud.surname) {
- bd >> stud.name;
- bd >> stud.patronymic;
- bd >> stud.group;
- bd >> stud.year;
- bd >> stud.phone;
- if ((byGroup && stud.group == search) || (!byGroup && stud.surname == search)) {
- print++;
- setlocale(0, ".866");
- cout << print << ") "
- << stud.surname << " "
- << stud.patronymic << " - "
- << stud.group << "("
- << stud.year << ")" << endl;
- setlocale(0, ".1251");
- cout << " Телефон: " << stud.phone << endl;
- }
- }
- return print != 0;
- }
- bool searchBySurname(string surname) {
- return search(surname, false);
- }
- bool searchByGroup(string group) {
- return search(group, true);
- }
- bool printListStudents() {
- int a = getCountStudent();
- if (a == 0)
- return false;
- StudentApplication *studs = new StudentApplication[a];
- ifstream bd(BDFIle);
- int i = 0;
- /* Чтение */
- while (bd >> studs[i].surname) {
- bd >> studs[i].name;
- bd >> studs[i].patronymic;
- bd >> studs[i].group;
- bd >> studs[i].year;
- bd >> studs[i].phone;
- i++;
- }
- /* Сортировка */
- for (i = 0; i < a-1; i++) {
- for (int j = 0; j < a - i - 1; j++) {
- if (studs[j].surname > studs[j + 1].surname) {
- StudentApplication s = studs[j];
- studs[j] = studs[j + 1];
- studs[j+1] = s;
- }
- }
- }
- /* Вывод */
- for (i = 0; i < a; i++) {
- setlocale(0, ".866");
- cout << i + 1 << ") "
- << studs[i].surname << " "
- << studs[i].name << " "
- << studs[i].patronymic << " - "
- << studs[i].group << "(" << studs[i].year << ")" << endl;
- setlocale(0, ".1251");
- cout << " Телефон: " << studs[i].phone << endl;
- }
- bd.close();
- delete []studs;
- return true;
- }
- };
- void openMenu();
- void printListStudent();
- void openNewStudent();
- void openSearchStudent(bool);
- int main()
- {
- setlocale(0, ".1251");
- openMenu();
- return 0;
- }
- void openMenu() {
- int menu;
- cout << "Меню:" << endl
- << "1 - Добавить запись о студенте" << endl
- << "2 - Поиск студентов по фамилии" << endl
- << "3 - Поиск студентов по группе" << endl
- << "4 - Список студентов" << endl
- << "0 - Завершить работу" << endl;
- cin >> menu;
- switch (menu)
- {
- case 1:
- openNewStudent();
- break;
- case 2:
- openSearchStudent(false);
- break;
- case 3:
- openSearchStudent(true);
- break;
- case 4:
- printListStudent();
- break;
- case 0:
- cout << "Завершение работы." << endl;
- break;
- default:
- openMenu();
- break;
- }
- }
- void printListStudent() {
- cout << "Список студентов:" << endl;
- StudentApplication s;
- bool a = s.printListStudents();
- if (a == false)
- cout << "Студентов нет." << endl;
- openMenu();
- }
- void openNewStudent() {
- cout << "Добавить запись о студенте:" << endl
- << "0 - Выход" << endl;
- StudentApplication stud;
- cout << "Введите фамилию: ";
- cin >> stud.surname;
- if (stud.surname == "0") {
- openMenu();
- return;
- }
- cout << "Введите имя: ";
- cin >> stud.name;
- if (stud.name== "0") {
- openMenu();
- return;
- }
- cout << "Введите отчество: ";
- cin >> stud.patronymic;
- if (stud.patronymic == "0") {
- openMenu();
- return;
- }
- cout << "Введите группу: ";
- cin >> stud.group;
- if (stud.group == "0") {
- openMenu();
- return;
- }
- cout << "Введите курс: ";
- cin >> stud.year;
- if (stud.year == 0) {
- openMenu();
- return;
- }
- cout << "Введите телефон: ";
- cin >> stud.phone;
- if (stud.phone == "0") {
- openMenu();
- return;
- }
- stud.addInBD();
- openNewStudent();
- }
- void openSearchStudent(bool byGroups) {
- string search;
- cout << "Поиск студентов по ";
- if (byGroups)
- cout << "группе:";
- else
- cout << "фамилии:";
- cout << endl << "0 - Выход" << endl;
- cout << "Введите ";
- if (byGroups)
- cout << "группу: ";
- else
- cout << "фамилию: ";
- cin >> search;
- if (search == "0") {
- openMenu();
- return;
- }
- cout << "Список студентов:" << endl;
- StudentApplication stud;
- bool a;
- if (byGroups)
- a = stud.searchByGroup(search);
- else
- a = stud.searchBySurname(search);
- if (a == false)
- cout << "Студентов не найдено." << endl;
- openSearchStudent(byGroups);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement