Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <iomanip>
- using namespace std;
- struct Student {
- string name;
- string secondname;
- string surname;
- string sex;
- unsigned int course = 0;
- unsigned int groupNum = 0;
- unsigned int grade[5] = {0, 0, 0, 0, 0};
- };
- int takeNum(int Min, int Max) {
- const string error = "Проверьте корректность введнных данных!\n";
- bool isIncorrect;
- int num;
- do {
- isIncorrect = false;
- cin >> num;
- if (cin.fail()) {
- isIncorrect = true;
- cout << error;
- cin.clear();
- while (cin.get() != '\n');
- }
- if (!isIncorrect && cin.get() != '\n') {
- cin.clear();
- while (cin.get() != '\n');
- cout << error;
- isIncorrect = true;
- }
- if (!isIncorrect && (num < Min || num > Max)) {
- isIncorrect = true;
- cout << error;
- }
- } while (isIncorrect);
- return num;
- }
- string wayToFile() {
- fstream checkFile;
- string way;
- bool isIncorrect;
- do {
- isIncorrect = false;
- cout << "Введите путь к файлу: ";
- cin >> way;
- checkFile.open(way);
- if (!checkFile.is_open()) {
- cout << "Проверьте правильность и нахождение вашего файла по заданному пути\n";
- isIncorrect = true;
- }
- else {
- isIncorrect = false;
- }
- checkFile.close();
- } while (isIncorrect);
- return way;
- }
- void createFile() {
- fstream F;
- string fileName;
- cout << "Введите имя файла: ";
- cin >> fileName;
- F.open(fileName, ios::out | ios::binary);
- F.close();
- cout << "Файл успешно создан!\n";
- }
- void addStudent(Student student[], int& count) {
- bool female;
- fstream F;
- string fileName = wayToFile();
- F.open(fileName, ios::in | ios::binary);
- int i = 0;
- while (F.read((char*)&student[i], sizeof(student[i])))
- i++;
- F.close();
- count = i;
- F.open(fileName, ios::out | ios::binary | ios::app);
- cout << "Введите имя студента: ";
- cin >> student[count].name;
- cout << "Введите фамилию студента: ";
- cin >> student[count].secondname;
- cout << "Введите отчество студента: ";
- cin >> student[count].surname;
- cout << "Введите пол студента(нажмите 0 - М, 1 - Ж): ";
- female = takeNum(0, 1);
- if(female == 0)
- student[count].sex = "male";
- else
- student[count].sex = "female";
- cout << "Введите курс студента: ";
- student[count].course = takeNum(1,5);
- cout << "Введите группу студента: ";
- student[count].groupNum = takeNum(1, 99999);
- cout << "Введите оценки студента по пяти предметам: \n";
- for (int i = 0; i < 5; i++) {
- cout << i << " предмет = : ";
- student[count].grade[i] = takeNum(1, 10);
- }
- F.write((char*)&student[count], sizeof(student[count]));
- F.close();
- count++;
- system("cls");
- }
- void viewFile(Student* student, int& count){
- system("cls");
- fstream F;
- string fileName = wayToFile();
- F.open(fileName, ios::in | ios::binary);
- int i = 0;
- cout << setw(20) << right << "Имя студента |"; // таблица setw смещение границы
- cout << setw(20) << right << "Фамилия студента|";
- cout << setw(20) << right << "Отчество студента|";
- cout << setw(14) << right << "Пол студента|";
- cout << setw(14) << right << "Курс студента|";
- cout << setw(14) << right << "Группа студента|";
- cout << setw(20) << right << "Успеваемость студента|\n";
- while (F.read((char*)&student[i], sizeof(student[i]))) {
- cout << setw(19) << right << student[i].name;
- cout << setw(20) << right << student[i].secondname;
- cout << setw(20) << right << student[i].surname;
- cout << setw(14) << right << student[i].sex;
- cout << setw(14) << right << student[i].course;
- cout << setw(14) << right << student[i].groupNum;
- cout << setw(14);
- for (int j = 0; j < 5; j++)
- cout << student[i].grade[j] << " ";
- cout << endl;
- i++;
- }
- count = i;
- F.close();
- }
- void deleteStudent(Student student[], int& count) {
- fstream F;
- string fileName = wayToFile();
- string secname;
- int i = 0;
- cout << "Введите фамилию студента, которого хотите удалить: ";
- cin >> secname;
- while (i < count && student[i].secondname != secname)
- i++;
- if (i == count) {
- cout << "Студент не найден!\n";
- return;
- }
- F.open(fileName, ios::out | ios::binary);
- for (int j = i + 1; j < count; j++)
- student[j - 1] = student[j];
- count--;
- for (int l = 0; l < count; l++)
- F.write((char*)&student[l], sizeof(student[l]));
- F.close();
- system("cls");
- cout << "Студент удален!\n";
- }
- void editStudentInformation(Student*& student, int count) {
- fstream F;
- string fileName = wayToFile();
- bool female;
- int choice;
- string secname;
- bool isExists = false;
- cout << "Введите фамилию студента, информацию которого вы хотите изменить: ";
- cin >> secname;
- for (int i = 0; i < count; i++) {
- if (student[i].secondname == secname) {
- isExists = true;
- cout << "Студент найден!\n";
- while (true) {
- cout << "Можно изменить:\n";
- cout << "1. Имя\n";
- cout << "2. Фамилию\n";
- cout << "3. Отчество\n";
- cout << "4. Пол\n";
- cout << "5. Курс\n";
- cout << "6. Номер группы\n";
- cout << "7. Успеваемость\n";
- cout << "8. Выход.\n";
- cout << "Введите номер выбранного действия: ";
- choice = takeNum(1, 2147483647);
- switch (choice) {
- case 1:
- cout << "Введите имя студента: ";
- cin >> student[i].name;
- break;
- case 2:
- cout << "Введите фамилию студента: ";
- cin >> student[i].secondname;
- break;
- case 3:
- cout << "Введите отчество студента: ";
- cin >> student[i].surname;
- break;
- case 4:
- cout << "(нажмите 0 - М, 1 - Ж): ";
- female = takeNum(0, 1);
- if (female == 0)
- student[i].sex = "male";
- else
- student[i].sex = "female";
- break;
- case 5:
- cout << "Введите курс студента: ";
- student[i].course = takeNum(1, 5);
- break;
- case 6:
- cout << "Введите группу студента: ";
- student[i].groupNum = takeNum(1, 99999);
- break;
- case 7:
- cout << "Введите оценки студента по пяти предметам: \n";
- for(int j = 0; j < 5; j++) {
- cout << j + 1 << " предмет = ";
- student[i].grade[j] = takeNum(1, 10);
- }
- break;
- case 8:
- F.open(fileName, ios::out | ios::binary);
- for (int l = 0; l < count; l++)
- F.write((char*)&student[l], sizeof(student[l]));
- F.close();
- return;
- default:
- cout << "Ошибка ввода! Введите ту цифру, которое есть в меню\n";
- break;
- }
- }
- }
- }
- if (!isExists)
- cout << "Такого студента не существует.\n";
- }
- void createExcellentStudentFile(Student student[], int count) {
- bool isExists = false;
- int mark;
- fstream F;
- string fileName = wayToFile();
- F.open(fileName, ios::in | ios::binary);
- int i = 0;
- while (F.read((char*)&student[i], sizeof(student[i])))
- i++;
- F.close();
- count = i;
- for (int i = 0; i < count; i++) {
- mark = 0;
- for (int j = 0; j < 5; j++)
- if (student[i].grade[j] > 8)
- mark++;
- if (mark == 5) {
- if (!isExists) {
- cout << "Введите имя файла, в который хотите записать отличников: ";
- cin >> fileName;
- F.open(fileName, ios::out | ios::binary);
- }
- F.write((char*)&student[i], sizeof(student[i]));
- isExists = true;
- }
- }
- if (isExists) {
- cout << "Отличники успешно записаны в новый файл!\n";
- }
- else {
- cout << "Отличников среди студентов не найдено!\n";
- }
- F.close();
- }
- void menu_Program(Student student[]) {
- int count = 0;
- while (true) {
- int choice;
- cout << "Текстовое меню:\n";
- cout << "1. Добавление студента\n";
- cout << "2. Удаление студента\n";
- cout << "3. Корректировка информации о студенте\n";
- cout << "4. Просмотр содержимого файла\n";
- cout << "5. Создать файл из всех отличников\n";
- cout << "6. Создать файл\n";
- cout << "7. Выход\n";
- cout << "Введите номер выбранного действия: ";
- choice = takeNum(1, 7);
- switch (choice) {
- case 1:
- if (count > 31) {
- cout << "Достигнуто максимальное значение студентов в группе";
- }
- else
- addStudent(student, count);
- break;
- case 2:
- deleteStudent(student, count);
- break;
- case 3:
- editStudentInformation(student, count);
- break;
- case 4:
- viewFile(student, count);
- break;
- case 5:
- createExcellentStudentFile(student, count);
- break;
- case 6:
- createFile();
- break;
- case 7:
- cout << "Программа завершена!";
- return;
- default:
- cout << "Ошибка ввода! Введите то число, которое есть в меню\n";
- break;
- }
- }
- }
- int main() {
- setlocale(LC_ALL, "Rus");
- Student student[32];
- menu_Program(student);
- }
Advertisement
Add Comment
Please, Sign In to add comment