Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include<iostream>
- #include<string>
- #include<windows.h>
- #include<stdlib.h>
- #include<string.h>
- #include<fstream>
- using namespace std;
- string adress = "БД студентов.txt";
- ifstream readFile;
- ofstream writeFile;
- struct Data {
- int days;
- int mounth;
- int year;
- };
- struct BD {
- Data birt;//обьединение
- string name, surname, midname;
- int avr_mark;
- };
- void sort(BD *student, int &N) {//сортировка по фамилии
- string temp;
- if (N == 1) {
- }
- else {
- for (int i = 0; i < N - 1; i++) {
- for (int j = 0; j < N - i - 1; j++) {
- if (student[j].surname > student[j + 1].surname) {
- swap(student[j].surname, student[j + 1].surname);
- swap(student[j].name, student[j + 1].name);
- swap(student[j].midname, student[j + 1].midname);
- swap(student[j].avr_mark, student[j + 1].avr_mark);
- swap(student[j].birt.days, student[j + 1].birt.days);
- swap(student[j].birt.mounth, student[j + 1].birt.mounth);
- swap(student[j].birt.year, student[j + 1].birt.year);
- }
- }
- }
- }
- }
- void search_data(BD *student, int &N) {//поиск по дате рождения
- int buf_day;
- int buf_mounth;
- int q = 0;//счётчик студентов на корректность
- cout << "Введите дату рождения: " << endl;
- cout << "Введите день: ";
- cin >> buf_day;
- cout << "Введите месяц: ";
- cin >> buf_mounth;
- system("cls");
- for (int i = 0; i < N; i++) {
- if (buf_day == student[i].birt.days && buf_mounth == student[i].birt.mounth) {
- cout << "Номер в списке: ";
- cout << i + 1 << endl;
- cout << "Имя: ";
- cout << student[i].name << endl;
- cout << "Фамилия: ";
- cout << student[i].surname << endl;
- cout << "Отчество: ";
- cout << student[i].midname << endl;
- cout << "Средний балл: ";
- cout << student[i].avr_mark << endl;
- cout << "Дата рождения: ";
- cout << student[i].birt.days << ".";
- cout << student[i].birt.mounth << ".";
- cout << student[i].birt.year << endl;
- cout << "////////////////////////////////////////////////" << endl;
- q++;
- }
- }
- if (q == 0) {
- cout << "Таких студентов нет!" << endl;
- }
- }
- void serach_avrg(BD *student, int &N) {//поиск по среднему баллу
- int buf_avrg_min;
- int buf_avrg_max;
- int q = 0;//счётчик студентов на корректность
- cout << "Введите диапазон(нижний): ";
- cin >> buf_avrg_min;
- cout << "Введите диапазон(вверхний): ";
- cin >> buf_avrg_max;
- system("cls");
- for (int i = 0; i < N; i++) {
- if (buf_avrg_min < student[i].avr_mark && buf_avrg_max > student[i].avr_mark) {
- cout << "Номер в списке: ";
- cout << i + 1 << endl;
- cout << "Имя: ";
- cout << student[i].name << endl;
- cout << "Фамилия: ";
- cout << student[i].surname << endl;
- cout << "Отчество: ";
- cout << student[i].midname << endl;
- cout << "Средний балл: ";
- cout << student[i].avr_mark << endl;
- cout << "Дата рождения: ";
- cout << student[i].birt.days << ".";
- cout << student[i].birt.mounth << ".";
- cout << student[i].birt.year << endl;
- cout << "////////////////////////////////////////////////" << endl;
- q++;
- }
- }
- if (q == 0) {
- cout << "Таких студентов нет!" << endl;
- }
- }
- void search_surname(BD *student, int &N) {//поиск по фамилии
- string buf_surname;
- int q = 0;//счётчик студентов на корректность
- cout << "Введите фамилию студента: ";
- cin >> buf_surname;
- system("cls");
- for (int i = 0; i < N; i++) {
- if (buf_surname == student[i].surname) {
- cout << "Номер в списке: ";
- cout << i + 1 << endl;
- cout << "Имя: ";
- cout << student[i].name << endl;
- cout << "Фамилия: ";
- cout << student[i].surname << endl;
- cout << "Отчество: ";
- cout << student[i].midname << endl;
- cout << "Средний балл: ";
- cout << student[i].avr_mark << endl;
- cout << "Дата рождения: ";
- cout << student[i].birt.days << ".";
- cout << student[i].birt.mounth << ".";
- cout << student[i].birt.year << endl;
- cout << "////////////////////////////////////////////////" << endl;
- q++;
- }
- }
- if (q == 0) {
- cout << "Таких студентов нет!" << endl;
- }
- }
- BD *delete_stud(int &N, BD *student) {
- int num;
- if (N == 0) {
- cout << "Некого удалять!!!" << endl;
- return 0;
- }
- cout << "Введите номер студента которого хотите удалить " << endl;
- cin >> num;
- if (N < num) {
- cout << "Такого студента нет!" << endl;
- return student;
- }
- BD *buf1 = new BD[N - 1];
- if (num == 1) {
- for (int i = 0; i < N - 1; i++) {
- buf1[i].name = student[i + 1].name;
- buf1[i].surname = student[i + 1].surname;
- buf1[i].midname = student[i + 1].midname;
- buf1[i].avr_mark = student[i + 1].avr_mark;
- buf1[i].birt.days = student[i + 1].birt.days;
- buf1[i].birt.mounth = student[i + 1].birt.mounth;
- buf1[i].birt.year = student[i + 1].birt.year;
- }
- cout << "Студент успешно отчислен!" << endl;
- N--;
- return buf1;
- }
- for (int i = 0; i < num - 1; i++) {
- buf1[i].name = student[i].name;
- buf1[i].surname = student[i].surname;
- buf1[i].midname = student[i].midname;
- buf1[i].avr_mark = student[i].avr_mark;
- buf1[i].birt.days = student[i].birt.days;
- buf1[i].birt.mounth = student[i].birt.mounth;
- buf1[i].birt.year = student[i].birt.year;
- }
- for (int i = num; i < N; i++) {
- buf1[i - 1].name = student[i].name;
- buf1[i - 1].surname = student[i].surname;
- buf1[i - 1].midname = student[i].midname;
- buf1[i - 1].avr_mark = student[i].avr_mark;
- buf1[i - 1].birt.days = student[i].birt.days;
- buf1[i - 1].birt.mounth = student[i].birt.mounth;
- buf1[i - 1].birt.year = student[i].birt.year;
- }
- cout << "Студент успешно отчислен!" << endl;
- N--;
- return buf1;
- }
- BD *push_back(int &N, BD *student) {//увеличение массива на 1
- int buf_var;//проверка корректности данных
- BD *buf = new BD[N + 1];
- for (int i = 0; i < N; i++) {
- buf[i].name = student[i].name;
- buf[i].surname = student[i].surname;
- buf[i].midname = student[i].midname;
- buf[i].avr_mark = student[i].avr_mark;
- buf[i].birt.days = student[i].birt.days;
- buf[i].birt.mounth = student[i].birt.mounth;
- buf[i].birt.year = student[i].birt.year;
- }
- cout << "Имя: ";
- cin >> buf[N].name;
- cout << "Фамилия: ";
- cin >> buf[N].surname;
- cout << "Отчество: ";
- cin >> buf[N].midname;
- while (1) {
- cout << "Средний балл(0-100): ";
- cin >> buf_var;
- if (buf_var >= 0 && buf_var <= 100) {
- buf[N].avr_mark = buf_var;
- break;
- }
- else {
- cout << "Введите количестов баллов корректно" << endl;
- continue;
- }
- }
- cout << "Дата рождения: " << endl;
- while (1) {
- cout << "День: ";
- cin >> buf_var;
- if (buf_var <= 31 && buf_var != 0) {
- buf[N].birt.days = buf_var;
- break;
- }
- else {
- cout << "Введите дату корректно" << endl;
- continue;
- }
- }
- while (1) {
- cout << "Месяц: ";
- cin >> buf_var;
- if (buf_var <= 12 && buf_var != 0) {
- buf[N].birt.mounth = buf_var;
- break;
- }
- else {
- cout << "Введите дату корректно" << endl;
- continue;
- }
- }
- while (1) {
- cout << "Год: ";
- cin >> buf_var;
- if (buf_var <= 2019) {
- buf[N].birt.year = buf_var;
- break;
- }
- else {
- cout << "Введите дату корректно" << endl;
- continue;
- }
- }
- N++;
- return buf;
- }
- void recap(int &N, BD *student) {//вывод в консоль
- if (N == 0) {
- cout << "Никого нет в списке!!!" << endl;
- }
- for (int i = 0; i < N; i++) {
- cout << "Номер в списке: ";
- cout << i + 1 << endl;
- cout << "Имя: ";
- cout << student[i].name << endl;
- cout << "Фамилия: ";
- cout << student[i].surname << endl;
- cout << "Отчество: ";
- cout << student[i].midname << endl;
- cout << "Средний балл: ";
- cout << student[i].avr_mark << endl;
- cout << "Дата рождения: ";
- cout << student[i].birt.days << ".";
- cout << student[i].birt.mounth << ".";
- cout << student[i].birt.year << endl;
- cout << "////////////////////////////////////////////////" << endl;
- }
- }
- BD *read_from_file(int &N, BD *student) {//считываем инфу из файла и записываем в массив
- readFile.open(adress);
- string none;//пропуск слов
- readFile >> none;
- for (int i = 0; i < N; i++) {
- readFile >> student[i].name;
- readFile >> student[i].surname;
- readFile >> student[i].midname;
- readFile >> student[i].avr_mark;
- readFile >> student[i].birt.days;
- readFile >> student[i].birt.mounth;
- readFile >> student[i].birt.year;
- }
- readFile.close();
- return student;
- }
- void add_to_file(int &N, BD *student) {//переписываем файл добавля нового тсудента
- writeFile.open(adress);
- writeFile << N << endl;
- for (int i = 0; i < N; i++) {
- writeFile << student[i].name << endl;
- writeFile << student[i].surname << endl;
- writeFile << student[i].midname << endl;
- writeFile << student[i].avr_mark << endl;
- writeFile << student[i].birt.days << endl;
- writeFile << student[i].birt.mounth << endl;
- writeFile << student[i].birt.year << endl;
- }
- writeFile.close();
- }
- void check_file_empty(int &N) {
- int file_size;//размер файла
- readFile.open(adress, ios::app);//узнаём сколько студентов в списке
- readFile.seekg(0, ios::beg);//указатель на начало файла // куда поставить указатель
- file_size = readFile.tellg();// считывает место указателя и возвращает эго//считывает количестов байтов слево от него
- if (file_size == 0) {
- writeFile << 0;
- }
- readFile >> N;
- readFile.close();
- }
- int main() {
- setlocale(LC_ALL, "RU");
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- int N = 0;//количество студентов
- char switch_on;//меню
- char switch_on1;//под меню
- /*readFile.open(adress, ios::app);//узнаём сколько студентов в списке
- readFile >> N;
- readFile.close();*/
- check_file_empty(N);
- BD *student = new BD[N];//конструктор
- student = read_from_file(N, student);
- while (1) {
- cout << "1. Новый студент" << endl;
- cout << "2. Вывод всех студентов" << endl;
- cout << "3. Поиск студента" << endl;
- cout << "4. Отчисление студента" << endl;
- cout << "0. Выход" << endl;
- cin >> switch_on;
- switch (switch_on)
- {
- case '0': {
- return 0;
- }
- case '1': {
- system("cls");
- student = push_back(N, student);
- sort(student, N);
- add_to_file(N, student);
- break;
- }case '2': {
- system("cls");
- recap(N, student);
- break;
- }case '3': {
- cout << "1. Поиск по фамилии" << endl;
- cout << "2. Поиск по среднему баллу" << endl;
- cout << "3. Поиск по дате рождения" << endl;
- cout << "4. Выйти в первоначальное меню" << endl;
- cin >> switch_on1;
- switch (switch_on1)
- {
- case '1': {
- search_surname(student, N);
- break;
- }
- case '2': {
- serach_avrg(student, N);
- break;
- }
- case '3': {
- search_data(student, N);
- break;
- }
- case '4': {
- system("cls");
- break;
- }
- default: {
- cout << "ERROR" << endl;
- break;
- }
- }
- break;
- }case '4': {
- system("cls");
- student = delete_stud(N, student);
- add_to_file(N, student);
- break;
- }
- default:
- cout << "ERROR" << endl;
- break;
- }
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement