Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- const int N = 50;
- struct Student
- {
- char nameSt[30];
- char group[10];
- int day;
- int month;
- int year;
- };
- void readSourceDateFromFile(Student a[], int &size);
- void outputDateToScreen(Student a[], int size);
- void sortSourceStudYear(Student a[], int size);
- void sortSourceStudAge(Student a[], int size);
- void addStudent(Student a[], int &size);
- void deleteStudent(Student a[], int &size);
- void searchStudents(Student a[], int sourceSize, Student b[], int &resultSize);
- void sortSearchStudent(Student a[], int size);
- void insertDateToFile(Student a[], int size);
- int main()
- {
- bool exit = true;
- int choice;
- Student sourceSt[N];
- Student searchSt[N];
- int realSourceSize, realSearchSize;
- while (exit)
- {
- system("clear");//system("CLS");
- cout << "1.\tВвести исходный массив из файла.\n";
- cout << "2.\tВывести исходный массив на экран.\n";
- cout << "3.\tСортировка исходного массива по дате рождения в порядке убывания возраста.\n";
- cout << "4.\tСортировка исходного массива по году рождения в порядку убывания.\n";
- cout << "5.\tДобавить студента в исходный массив студентов.\n";
- cout << "6.\tУдалить студента в исходный массив студентов.\n";
- cout << "7.\tЗаписать исходный массив в файл.\n";
- cout << "8.\tНайти всех студентов, родившихся весной (выходной массив).\n";
- cout << "9.\tВывести выходной массив на экран.\n";
- cout << "10.\tСортировка выходного массива по фамилии студентов в алфавитном порядке.\n";
- cout << "11.\tЗаписать выходной массив в файл.\n";
- cout << "0.\tВывод из программы.\n";
- cout << "Введите пункт меню: ";
- cin >> choice;
- switch (choice)
- {
- case 0:
- exit = false;
- break;
- case 1:
- readSourceDateFromFile(sourceSt, realSourceSize);
- break;
- case 2:
- outputDateToScreen(sourceSt, realSourceSize);
- break;
- case 3:
- sortSourceStudAge(sourceSt, realSourceSize);
- break;
- case 4:
- sortSourceStudYear(sourceSt, realSourceSize);
- break;
- case 5:
- addStudent(sourceSt, realSourceSize);
- break;
- case 6:
- deleteStudent(sourceSt, realSourceSize);
- break;
- case 7:
- insertDateToFile(sourceSt, realSourceSize);
- break;
- case 8:
- searchStudents(sourceSt, realSourceSize, searchSt, realSearchSize);
- break;
- case 9:
- outputDateToScreen(searchSt, realSearchSize);
- break;
- case 10:
- sortSearchStudent(searchSt, realSearchSize);
- break;
- case 11:
- insertDateToFile(searchSt, realSearchSize);
- break;
- }
- system("read -p 'Нажмите Enter для продолжения...' var");//system("pause");
- }
- }
- //1
- void readSourceDateFromFile(Student a[], int &size)
- {
- size = 0;
- char fileName[30];
- char iniz[10];
- cout << "\nВведите имя файла: ";
- cin >> fileName;
- //strcat(fileName, ".txt");//strcat_s(fileName, ".txt");
- ifstream fin(fileName);
- if (fin.is_open())
- cout << "\n\tФайл успешно открыт!\n";
- else
- {
- cout << "\n\tОшибка! Файл закрыт!\n";
- return;
- }
- while (!fin.eof())
- {
- fin >> a[size].nameSt >> iniz >> a[size].group >> a[size].day >> a[size].month >> a[size].year;
- strcat(a[size].nameSt, " ");//strcat_s(fileName, ".txt");
- strcat(a[size].nameSt, iniz);//strcat_s(fileName, ".txt");
- size++;
- }
- size--;
- cout << "\t\tЗапись завершена.\n";
- fin.close();
- if ( !fin.is_open() )
- cout << "\tФайл успешно закрыт!\n\n";
- else
- {
- cout << "\tОшибка! Файл открыт!\n\n";
- return;
- }
- }
- //2,9
- void outputDateToScreen(Student a[], int size)
- {
- if (size == 0)
- {
- cout << "\n\tМассив пуст.";
- return;
- }
- cout << "-------------------------------------------------------------\n";
- cout << "| № | Фамилия И. О. | Группа |Дата рождения|\n";
- cout << "-------------------------------------------------------------\n";
- for (int i = 0; i < size; i++)
- {
- int b = ( strlen(a[i].nameSt) ) / 2;
- int c = ( strlen(a[i].group) ) / 2;
- cout << "|" << setw(3) << i+1;
- cout << "|" << setw(30 + b) << a[i].nameSt;
- cout << "|" << setw(10 + c) << a[i].group;
- cout << "|" << setw(3) << a[i].day << "|" << setw(3) << a[i].month << "|" << setw(5) << a[i].year;
- cout << "|\n";
- }
- cout << "-------------------------------------------------------------\n\n";
- }
- //3
- void sortSourceStudYear(Student a[], int size)
- {
- if (size == 0)
- {
- cout << "\n\tИсходный массив пуст.";
- return;
- }
- for (int i = 0; i < size; i++)
- for (int j = 0; j < size; j++)
- {
- if (a[i].year >= a[j].year)
- {
- Student c = a[i];
- a[i] = a[j];
- a[j] = c;
- }
- }
- cout << "\n\tСортировка исходного массива по году рождения в порядке убывания завершена.\n\n";
- }
- //4
- void sortSourceStudAge(Student a[], int size)
- {
- if (size == 0)
- {
- cout << "\n\tИсходный массив пуст.";
- return;
- }
- for (int i = 0; i < size; i++)
- for (int j = 0; j < size; j++)
- {
- if (a[i].year > a[j].year)
- {
- Student c = a[i];
- a[i] = a[j];
- a[j] = c;
- }
- else if (a[i].year == a[j].year)
- {
- if (a[i].month > a[j].month)
- {
- Student c = a[i];
- a[i] = a[j];
- a[j] = c;
- }
- else if (a[i].month == a[j].month)
- {
- if (a[i].day > a[j].day)
- {
- Student c = a[i];
- a[i] = a[j];
- a[j] = c;
- }
- }
- }
- }
- cout << "\n\tСортировка исходного массива по дате рождения в порядке убывания возраста завершена.\n\n";
- }
- //5
- void addStudent(Student a[], int &size)
- {
- if (size == N)
- {
- cout << "\n\tИсходный массив заполнен.\n";
- return;
- }
- Student newStudent;
- char iniz[10];
- cout << "\nВведите данные о новом студенте:\n";
- cout << "\tФамилия: ";
- cin >> newStudent.nameSt;
- cout << "\tИнициалы: ";
- cin >> iniz;
- strcat(newStudent.nameSt, " ");//strcat_s(fileName, ".txt");
- strcat(newStudent.nameSt, iniz);//strcat_s(fileName, ".txt");
- cout << "\tДата рождения:\n";
- cout << "\t\tдень: ";
- cin >> newStudent.day;
- cout << "\t\tмесяц: ";
- cin >> newStudent.month;
- cout << "\t\tгод: ";
- cin >> newStudent.year;
- a[size++] = newStudent;
- cout << "\n\tЗапись добавлена.\n\n";
- }
- //6
- void deleteStudent(Student a[], int &size)
- {
- if (size == 0)
- {
- cout << "\n\tИсходный массив пуст.\n";
- return;
- }
- int number;
- char choice;
- bool exit = true;
- outputDateToScreen(a, size);
- while (true)
- {
- cout << "\nВведите номер удаляемой строки: ";
- cin >> number;
- number--;
- if ((number < 0) || (number >= size))
- {
- cout << "\n\t!!! Нет такого номера!\n";
- system("read -p 'Нажмите Enter для продолжения...' var");//system("pause");
- }
- else
- break;
- }
- cout << number+1 << " строка: " << a[number].nameSt << ' ' << a[number].group << ' ' << a[number].day << '.' << a[number].month << '.' << a[number].year << '\n';
- while ( exit )
- {
- cout << "Удалить данный элемент? (Y,y,Д,д - да; N,n,Н,н - нет) -> ";
- cin >> choice;
- switch (choice)
- {
- case 'n':
- case 'N':
- //case 'н'
- //case 'Н'
- cout << "\n\tЗапись не удалена.\n\n";
- exit = false;
- break;
- case 'y':
- case 'Y':
- //case 'д'
- //case 'Д'
- for (int i = number + 1; i < size; i++)
- a[i-1]=a[i];
- size--;
- cout << "\n\tЗапись удалена.\n\n";
- exit = false;
- break;
- default:
- cout << "Пожалуйста, введите 'Y' или 'N'\n";
- system("read -p 'Нажмите Enter для повторного ввода...' var");
- }
- }
- }
- //7, 11
- void insertDateToFile(Student a[], int size)
- {
- if (size == 0)
- {
- cout << "\n\tМассив пуст.";
- return;
- }
- char fileName[30];
- char iniz[10];
- cout << "\nВведите имя файла: ";
- cin >> fileName;
- //strcat(fileName, ".txt");//strcat_s(fileName, ".txt");
- ofstream fout(fileName);
- if (fout.is_open())
- cout << "\n\tФайл успешно открыт!\n";
- else
- {
- cout << "\n\tОшибка! Файл закрыт!\n";
- return;
- }
- fout << "-------------------------------------------------------------\n";
- fout << "| № | Фамилия И. О. | Группа |Дата рождения|\n";
- fout << "-------------------------------------------------------------\n";
- for (int i = 0; i < size; i++)
- {
- int b = ( strlen(a[i].nameSt) ) / 2;
- int c = ( strlen(a[i].group) ) / 2;
- fout << "|" << setw(3) << i+1;
- fout << "|" << setw(30 + b) << a[i].nameSt;
- fout << "|" << setw(10 + c) << a[i].group;
- fout << "|" << setw(3) << a[i].day << "|" << setw(3) << a[i].month << "|" << setw(5) << a[i].year;
- fout << "|\n";
- }
- fout << "-------------------------------------------------------------\n";
- cout << "\t\tЗапись массива в файл " << fileName << " завершена.\n";
- fout.close();
- if ( !fout.is_open() )
- cout << "\tФайл успешно закрыт!\n\n";
- else
- {
- cout << "\tОшибка! Файл открыт!\n\n";
- return;
- }
- }
- //8
- void searchStudents(Student a[], int sourceSize, Student b[], int &resultSize)
- {
- if (sourceSize == 0)
- {
- cout << "\n\tИсходный массив пуст.";
- return;
- }
- resultSize = 0;
- for (int i = 0; i < sourceSize; ++i)
- if ((a[i].month <= 5) && (a[i].month >= 3))
- b[resultSize++] = a[i];
- if (resultSize == 0)
- cout << "Студеты не найдены.\n";
- else
- cout << "\n\tПоиск завершен.\n\n";
- }
- //9
- void sortSearchStudent(Student a[], int size)
- {
- if (size == 0)
- {
- cout << "\n\tВыходной массив пуст.";
- return;
- }
- for (int i = 0; i < size; i++)
- for (int j = 0; j < size; j++)
- {
- if (strcmp(a[i].nameSt, a[j].nameSt) <= 0)
- {
- Student c = a[i];
- a[i] = a[j];
- a[j] = c;
- }
- }
- cout << "\n\tСортировка выходного массива по фамилии студентов в алфавитном порядке завершена.\n\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement