Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <coino.h>
- #include <cstring>
- using namespace std;
- const int MAX = 10; //КОЛИЧЕСТВО СТУДЕНТОВ
- enum {
- NAME_SIZE = 30,
- FAMILI_SIZE = 30,
- GRUPPA_SIZE = 20,
- };
- struct student {
- char name[NAME_SIZE]{ "Данные не указаны" };
- char famili[FAMILI_SIZE]{ "Данные не указаны" };
- char gruppa[GRUPPA_SIZE]{ "Данные не указаны" };
- int god_roz{};
- int god_post{};
- int kurs{};
- }student_list[MAX];
- void not_ready_yet() {
- cout << "\n функция обещает заработать 14.12.21\n";
- }
- void case_input(int i) {
- cout << "\nВведите данные студента №" << i + 1;
- cout << "\nВведите имя студента: ";
- cin.ignore();
- cin.getline(student_list[i].name, NAME_SIZE);
- cout << "Введите фамилию: ";
- //cin.ignore();
- cin.getline(student_list[i].famili, FAMILI_SIZE);
- cout << "Введите группу: ";
- // cin.ignore();
- cin.getline(student_list[i].gruppa, GRUPPA_SIZE);
- cout << "Введите год рождения: ";
- cin >> student_list[i].god_roz;
- cout << "Введите год поступления: ";
- cin >> student_list[i].god_post;
- cout << "Введите курс: ";
- cin >> student_list[i].kurs;
- }
- void case_ouput(int count) {
- cout << "\nВыбрана команда ВЫВОДА данных\n";
- if (count != 0) {
- cout << "-----------------+-----------------------------+-----------------------------+-------------------+-----------------------------+-----------------------------+----------------------------+";
- cout << "\nДанные студента №|";
- cout << setw(NAME_SIZE) << "Имя|";
- cout << setw(FAMILI_SIZE) << "Фамилия|";
- cout << setw(GRUPPA_SIZE) << "Группа|";
- cout << setw(NAME_SIZE) << "Год рождения|";
- cout << setw(NAME_SIZE) << "Год поступления|";
- cout << setw(NAME_SIZE) << "Курс|\n";
- cout << "-----------------+-----------------------------+-----------------------------+-------------------+-----------------------------+-----------------------------+----------------------------+";
- for (int i = 0; i < count; i++) {
- cout << "\n" << setw(17) << i + 1 << "|";
- cout << setw(NAME_SIZE - 1) << student_list[i].name << "|";
- cout << setw(NAME_SIZE - 1) << student_list[i].famili << "|";
- cout << setw(GRUPPA_SIZE - 1) << student_list[i].gruppa << "|";
- cout << setw(NAME_SIZE - 1) << student_list[i].god_roz << "|";
- cout << setw(NAME_SIZE - 1) << student_list[i].god_post << "|";
- cout << setw(NAME_SIZE - 2) << student_list[i].kurs << "|\n";
- cout << "-----------------+-----------------------------+-----------------------------+-------------------+-----------------------------+-----------------------------+----------------------------+";
- }
- }
- else { cout << "Данные не были введены или удалены"; }
- }
- int delete_data(int count) {
- cout << "Выбрана команда УДАЛЕНИЯ данных\n";
- for (int i = 0; i < count; i++) {
- strcpy_s(student_list[i].name, "Данные не указаны");
- strcpy_s(student_list[i].famili, "Данные не указаны");
- strcpy_s(student_list[i].gruppa, "Данные не указаны");
- student_list[i].god_roz = 0;
- student_list[i].god_post = 0;
- student_list[i].kurs = 0;
- }
- cout << "Все данные успешно удалены\n";
- count = 0;
- return 0;
- }
- void sort_data(int count_of_filled) {
- char a;
- int var_of_sort{};
- cout << "Выберите по какому параметру проводить сортировку: \n\n";
- cout << "1 - по имени\n";
- cout << "1 - по фамилии\n";
- cout << "3 - по группе\n";
- cout << "4 - по году рождения\n";
- cout << "5 - по году поступления\n";
- cout << "6 - по курсу\n";
- do
- {
- cout << "\nВведите порядковый номер вариантов сортировки: ";
- cin >> a;
- var_of_sort = a - '0';
- } while (var_of_sort < 0 && var_of_sort > 6);
- switch (var_of_sort){
- case 1:{
- student tmp; // сортировка по алфавиту
- for (int t = 0; t < count_of_filled; t++){
- for (int i = 0; i < count_of_filled; i++){
- if ( strcmp_s(student_list[i].name, student_list[t].name) > 0 )
- {
- tmp = student_list[i];
- student_list[i] = student_list[t];
- student_list[t] = tmp;
- }
- }
- }
- cout << "\nСортировка проведена. Введите 2 для вывода\n";
- break;
- }
- case 2: {
- student tmp; // сортировка по алфавиту
- for (int t = 0; t < count_of_filled; t++){
- for (int i = 0; i < count_of_filled; i++){
- if ( strcmp_s(student_list[i].famili, student_list[t].famili) > 0 )
- {
- tmp = student_list[i];
- student_list[i] = student_list[t];
- student_list[t] = tmp;
- }
- }
- }
- cout << "\nСортировка проведена. Введите 2 для вывода\n";
- break;
- }
- case 3: {
- student tmp; // сортировка по алфавиту
- for (int t = 0; t < count_of_filled; t++){
- for (int i = 0; i < count_of_filled; i++){
- if ( strcmp_s(student_list[i].gruppa, student_list[t].gruppa) > 0 )
- {
- tmp = student_list[i];
- student_list[i] = student_list[t];
- student_list[t] = tmp;
- }
- }
- }
- cout << "\nСортировка проведена. Введите 2 для вывода\n";
- break;
- }
- case 4: {
- student tmp;
- for (int i = 0; i < count_of_filled; i++){
- for (int j = 0; j < count_of_filled; j++){
- if (student_list[i].god_roz < student_list[j].god_roz)
- {
- tmp = student_list[i];
- student_list[i] = student_list[j];
- student_list[j] = tmp;
- }
- }
- }
- cout << "\nСортировка проведена. Введите 2 для вывода\n";
- break;
- }
- case 5: {
- student tmp;
- for (int t = 0; t < count_of_filled; t++){
- for (int i = 0; i < count_of_filled; i++){
- if (student_list[i].god_post > student_list[t].god_post)
- {
- tmp = student_list[i];
- student_list[i] = student_list[t];
- student_list[t] = tmp;
- }
- }
- }
- cout << "\nСортировка проведена. Введите 2 для вывода\n";
- break;
- }
- case 6: {
- student tmp;
- for (int t = 0; t < count_of_filled; t++) {
- for (int i = 0; i < count_of_filled; i++) {
- if (student_list[i].kurs > student_list[t].kurs)
- {
- tmp = student_list[i];
- student_list[i] = student_list[t];
- student_list[t] = tmp;
- }
- }
- }
- cout << "\nСортировка проведена. Введите 2 для вывода\n";
- break;
- }
- default: { cout << "\nВведено неверное значение. Попробуйте ещё раз. \n";
- sort_data(count_of_filled);
- break;
- }
- }
- }
- int text_file_input() {
- ifstream text_f("text_file.txt");
- if (!text_f.is_open()) return 666;
- int count{}, i{};
- text_f >> count;
- cout << "HELLO " << count << endl;
- for (int I = 0; I < count; I++ ) {
- text_f >> i; i--;
- char tmp[100]; text_f.getline(tmp, 100);
- text_f.getline(student_list[i].name, NAME_SIZE );
- text_f.getline(student_list[i].famili, FAMILI_SIZE);
- text_f.getline(student_list[i].gruppa, GRUPPA_SIZE);
- text_f >> student_list[i].god_roz;
- text_f >> student_list[i].god_post;
- text_f >> student_list[i].kurs;
- text_f.getline(tmp, 100);
- }
- text_f.close();
- return count;
- }
- void text_file_ouput(int count) {
- ofstream text_f("text_file.txt");
- text_f << count << endl;
- for (int i = 0; i < count; i++) {
- text_f << i + 1 << endl;
- cin.ignore();
- text_f << student_list[i].name << endl;
- text_f << student_list[i].famili << endl;
- text_f << student_list[i].gruppa << endl;
- text_f << student_list[i].god_roz << endl;
- text_f << student_list[i].god_post << endl;
- text_f << student_list[i].kurs << endl;
- }
- text_f.close();
- }
- void counvert_bin_file() {
- ifstream text_f("text_file.txt", ios::in);
- ofstream bin_f("bin_file.bin", ios::binary | ios::out);
- if (text_f.is_open() && bin_f.is_open()) {
- int i{}, count{};
- char tmp{};
- text_f >> count;
- //bin_f << count;
- bin_f.write((char*)&count,sizeof(count));
- for (int I = 0; I < count; I++ ) {
- text_f >> i; i--;
- char tmp[100]; text_f.getline(tmp, 100);
- //cin.ignore();
- text_f.getline(student_list[i].name, NAME_SIZE );
- cout << student_list[i].name;
- text_f.getline(student_list[i].famili, FAMILI_SIZE);
- text_f.getline(student_list[i].gruppa, GRUPPA_SIZE);
- //text_f >> student_list[i].name;
- //cin.ignore();
- //text_f >> student_list[i].famili;
- //cin.ignore();
- //text_f >> student_list[i].gruppa;
- text_f >> student_list[i].god_roz;
- text_f >> student_list[i].god_post;
- text_f >> student_list[i].kurs;
- text_f.getline(tmp, 100);
- }
- bin_f.write((char*)&student_list, sizeof(student_list));
- }
- else cout << "\nError!!!\n";
- text_f.close();
- bin_f.close();
- }
- int read_bin_file() {
- ifstream fin("bin_file.bin", ios::in | ios::binary);
- int index{};
- char tmp{};
- int count;
- //fin >> count;
- fin.read( (char*) &count, sizeof(count) );
- cout << count;
- for(int i = 0; i < count; i++) {
- fin.read((char*)&student_list[i], sizeof(student_list[i]));
- }
- //fin.read((char*)&student_list, sizeof(student_list));
- //while (fin >> tmp) {
- // fin.read((char*)&student_list[index], sizeof(student_list[index]));
- // index++;
- //}
- fin.close();
- return count;
- }
- /*void Input_from_binary(int& countOfStudents, Student* students) {
- ifstream fin;
- fin.open("database.bin", ios::binary);
- if (fin.is_open()) {
- fin.read((char*)&countOfStudents, sizeof(countOfStudents));
- for (int i = 0; i < countOfStudents; i++) {
- fin.read((char*)&students[i], sizeof(students[i]));
- }
- }
- else {
- system("cls");
- cout << "***Ошибка открытия файла***";
- system("pause");
- }
- fin.close();
- }*/
- int working_on_files(int count_of_filled) {
- int option;
- bool f_exit = true;
- while (f_exit != false) {
- cout << "\nВыберите функцию из меню:\n";
- cout << "--------------------------------------------";
- cout << "\n1 - ввод информации из уже созданного текстового файла\n";
- cout << "2 - ввод информации из уже созданного бинарного файла\n";
- cout << "3 - вывод данных в файл(текстовый)\n";
- cout << "4 - перевод содержимого текстового файла в бинарный\n";
- cout << "0 - выход из подрограммы\n";
- cout << "--------------------------------------------";
- do {
- cout << "\nВведите номер функции: ";
- cin >> option;
- } while (option < 0 || option > 4);
- switch (option) {
- case 1: {int count{}; count = text_file_input();
- if (count >= count_of_filled) count_of_filled = count;
- break; }
- case 2: {int count{}; count = read_bin_file();
- if (count >= count_of_filled) count_of_filled = count;
- break; }
- case 3: text_file_ouput(count_of_filled);
- break;
- case 4: counvert_bin_file();
- break;
- case 0: f_exit = false;
- break;
- default: { cout << "\nВведено неверное значение. Попробуйте ещё раз. \n";
- working_on_files(count_of_filled);
- break;
- }
- }
- }
- return count_of_filled;
- }
- void zapros(int count_of_filled) {
- student tmp;
- for (int t = 0; t < count_of_filled; t++)
- for (int i = 0; i < count_of_filled; i++)
- if (student_list[i].god_roz < student_list[t].god_roz)
- {
- tmp = student_list[i];
- student_list[i] = student_list[t];
- student_list[t] = tmp;
- }
- cout << "Фамилии и группы двух наиболее младших по возрасту студентов\n";
- for (int j = 0; j < 2; j++) {
- cout << j + 1 << ". " << student_list[j].famili << " | " << student_list[j].gruppa << endl;
- }
- }
- int return_menu(void) {
- char tp[MAX];
- int c;
- cout << "\n\n1 - ввести данные группы студентов\n";
- cout << "2 - вывести данные группы студентов в виде таблицы\n";
- cout << "3 - изменение записи данных группы студентов\n";
- cout << "4 - удаление записи данных группы студентов\n";
- cout << "5 - сортировка данных\n";
- cout << "6 - работа с файлами\n";
- cout << "7 - вывод данных по запросу(вывести фамилии и группы двух наиболее младших по возрасту студентов)\n";
- cout << "0 - выход из программы\n";
- do
- {
- cout << "\nВведите порядковый номер функции: ";
- cin >> tp;
- c = *tp - '0';
- cout << c;
- } while (c < 0 && c > 10);
- return c;
- }
- int main() {
- setlocale(0, "");
- int command{}, count_of_filled{};
- int c{};
- bool flag = true;
- cout << "Здравствуйте!\n";
- while (flag != false) {
- command = return_menu();
- switch (command) {
- case 1: {
- cout << "\nВыбрана команда ВВОДА данных\n";
- cout << "Введите количество студентов, данные которых вы хотите ввести: \n";
- cin >> c;
- for (int i = 0; i < c; i++) {
- case_input(i); count_of_filled++;
- }
- break;
- }
- case 2: case_ouput(count_of_filled); break;
- case 3: {
- int number{};
- cout << "Введите номер студента, данные которого вы хотите изменить: ";
- cin >> number;
- cout << "\nВыбрана команда ИЗМЕНЕНИЯ данных студента №" << number << "\n";
- case_input(number - 1);
- break;
- }
- case 4: {count_of_filled = delete_data(count_of_filled); break; }
- case 5: sort_data(count_of_filled); break;
- case 6: count_of_filled = working_on_files(count_of_filled); break;
- case 7: zapros(count_of_filled); break;
- case 0: cout << "Выбрана команда ВЫХОД\n"; flag = false; break;
- default: { cout << "\nВведено неверное значение. Попробуйте ещё раз. \n";
- return_menu();
- break;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement