Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include<iostream>
- #include<ctime>
- #include<fstream>
- #include<string>
- #include<cstdio>
- #include<Windows.h>
- using namespace std;
- //params
- struct Person
- {
- char FIO[100] = "";
- int yearofborn, groupnumber, phys, math, IT, chem;
- double averageball;
- }Student[100];
- typedef void(*Function)();
- string pathtofile = "stud_list.txt";
- ofstream fout;
- ifstream fin;
- FILE* F;
- //skeleton
- void CheckFile(Function);
- void ListView();//2
- void Personadd();//3
- void PersonChanges();
- void FileClean();
- int NumOfStudents();
- void TaskSolution();//4
- void FromFileToStruct();
- void menuUI();
- void menuPointsUI(Function);
- //program
- void main()
- {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- setlocale(0, "");
- menuUI();
- }
- //works
- void menuUI()
- {
- bool exit = false;
- int choose;
- do
- {
- cout << "Press any key to escape or :\n1)Изменение содержимого файла.\n2)Просмотр.\n3)Индивидуальное задание." << endl;
- cin >> choose;
- switch (choose)
- {
- case 1://Содержимое
- {
- cout << "1)Добавить запись\n2)Отредактировать запись\n3)Очистка файла" << endl;
- int choose1;
- cin >> choose1;
- switch (choose1)
- {
- case 1:
- {
- CheckFile(Personadd);
- break;
- }
- case 2:
- {
- CheckFile(PersonChanges);
- break;
- }
- case 3:
- {
- CheckFile(FileClean);
- break;
- }
- }
- break; }
- case 2://Просмотр
- {
- system("cls");
- CheckFile(ListView);
- cout << "Для выхода в меню введите что-нибудь" << endl;
- cin.ignore(1000, '\n'); getchar();
- system("cls"); cin.ignore(1000, '\n');
- break; }
- case 3://Задание
- {
- system("cls");
- CheckFile(TaskSolution);
- cout << "Для выхода в меню введите что-нибудь" << endl;
- cin.ignore(1000, '\n'); getchar();
- system("cls"); cin.ignore(1000, '\n');
- break; }
- default:
- {
- cout << "Escape" << endl;
- exit = true;
- }
- }
- } while (!exit);
- }
- //works
- void menuPointsUI(Function X)
- {
- bool exitfrompoint = false;
- do
- {
- X();
- cout << "Продолжить/Выйти в меню(y/any other key)" << endl;
- if (getchar() == 'y')
- {
- exitfrompoint = false;
- }
- else
- {
- getchar();
- exitfrompoint = true;
- break;
- }
- } while (!exitfrompoint);
- }
- //works
- void FromFileToStruct()
- {
- int numofstudents = NumOfStudents();
- for (int i = 0; i < numofstudents; i++)
- {
- int size = 0;
- char t;
- do
- {
- t = fgetc(F);
- size++;
- } while ((int)t >57 || (int)t <48);
- fseek(F, -size, SEEK_CUR);
- fgets(Student[i].FIO, size, F);
- fscanf(F, "%i %i %i %i %i %i %lf\n",&Student[i].yearofborn, &Student[i].groupnumber, &Student[i].phys, &Student[i].math, &Student[i].IT, &Student[i].chem, &Student[i].averageball);
- }
- }
- //works
- void Personadd()
- {
- FILE* F;
- F = fopen("stud_list.txt", "a+");
- for (int i = NumOfStudents();; i++)
- {
- cout << "Запись студента с номером:" << i + 1 << endl;
- cout << "ФИО" << endl; cin.ignore(500, '\n'); gets_s(Student[i].FIO);
- cout << "Год рождения" << endl; cin >> Student[i].yearofborn;
- cout << "Номер группы" << endl; cin >> Student[i].groupnumber;
- cout << "Физика" << endl; cin >> Student[i].phys;
- cout << "Математика" << endl; cin >> Student[i].math;
- cout << "ОАиП" << endl; cin >> Student[i].IT;
- cout << "Химия" << endl; cin >> Student[i].chem;
- Student[i].averageball = (Student[i].phys + Student[i].math + Student[i].IT + Student[i].chem) / 4.;
- fprintf(F, "%s %i %i %i %i %i %i %f\n", Student[i].FIO, Student[i].yearofborn, Student[i].groupnumber, Student[i].phys, Student[i].math, Student[i].IT, Student[i].chem, Student[i].averageball);
- cout << "Продолжить или выйти(y/any other key)" << endl;
- _flushall();
- if (getchar() == 'y')
- {
- continue;
- }
- else
- {
- getchar();
- system("cls");
- return;
- }
- }
- fclose(F);
- }
- //in progress
- void PersonChanges()
- {
- FromFileToStruct();
- for (int i = 0; i < NumOfStudents(); i++)
- {
- cout << "ID:" << i + 1 << "\t" << Student[i].FIO<<endl;
- }
- cout << endl;
- cout << "Укажите ID студента,данные которого необходимо изменить:" << endl; int ID; cin >> ID;
- --ID;
- cout << "Что хотите изменить?1)ФИО\n2)Год рождения\n3)Номер группы\n4)Оценку по физике\n5)Оценку по математике\n6)Оценку по программированию\n7)Оценку по химии" << endl;
- int choice; cin >> choice;
- long size = 0;
- char* buff = new char[1000]; string b;
- char* buff2 = new char[1000];
- char* requiredstring = new char[1000];
- switch (choice)
- {
- case 1:
- {
- ifstream fin;
- for (int i = 0; i < 8 * ID; i++)
- {
- getline(fin, b);
- }
- cout << b;
- break;
- }
- case 2:
- {
- break;
- }
- case 3:
- {
- break;
- }
- case 4:
- {
- break;
- }
- case 5:
- {
- break;
- }
- case 6:
- {
- break;
- }
- case 7:
- {
- break;
- }
- }
- delete[]buff;
- delete[]buff2;
- delete[]requiredstring;
- }
- //works
- void ListView()
- {
- int numofstudents = NumOfStudents();
- cout << "Кол-во студентов в файле:" << numofstudents << endl;
- int ch; cout << "Способ показа информации:\n1)В чистом виде(как в файле)\n2)Запись из файла в структуру и чтение из неё" << endl;
- cin >> ch;
- switch (ch)
- {
- case 1:
- {
- string tmp;
- ifstream fin;
- fin.open(pathtofile);
- while (getline(fin, tmp))
- {
- cout << tmp << endl;
- }
- tmp = "";
- break;
- }
- case 2:
- {
- FromFileToStruct();
- cout << "Вывести на экран\n1)Всех студентов.\n2)Определённое кол-во\n3)Студента под номером" << endl;
- int vibor;
- ifstream fin;
- cin >> vibor;
- switch (vibor)
- {
- case 1:
- {
- for (int i = 0; i < numofstudents; i++)
- {
- cout << "ФИО:" << Student[i].FIO << endl << "Год рождения:" << Student[i].yearofborn << endl << "Номер группы:" << Student[i].groupnumber << endl << "Математика:" << Student[i].math << endl << "Физика:" << Student[i].phys << endl << "ОАиП:" << Student[i].IT << endl << "Химия:" << Student[i].chem << endl << "Средний балл:" << Student[i].averageball << endl;
- cout << "-----------------------------------------------------------------------------------------------------" << endl;
- }
- break;
- }
- case 2:
- {
- int x = 0; int count; cout << "Укажите количество:"; cin >> count;
- for (int i = 0; i < count; i++)
- {
- cout << "ФИО:" << Student[i].FIO << endl << "Год рождения:" << Student[i].yearofborn << endl << "Номер группы:" << Student[i].groupnumber << endl << "Математика:" << Student[i].math << endl << "Физика:" << Student[i].phys << endl << "ОАиП:" << Student[i].IT << endl << "Химия:" << Student[i].chem << endl << "Средний балл:" << Student[i].averageball << endl;
- cout << "-----------------------------------------------------------------------------------------------------" << endl;
- }
- break;
- }
- case 3:
- {
- for (int i = 0; i < NumOfStudents(); i++)
- {
- cout <<"Number "<<i+1<<"\t"<<Student[i].FIO<<endl;
- }
- int number; cout << "Укажите номер:"; cin >> number;
- --number;
- cout << "ФИО:" << Student[number].FIO << endl << "Год рождения:" << Student[number].yearofborn << endl << "Номер группы:" << Student[number].groupnumber << endl << "Математика:" << Student[number].math << endl << "Физика:" << Student[number].phys << endl << "ОАиП:" << Student[number].IT << endl << "Химия:" << Student[number].chem << endl << "Средний балл:" << Student[number].averageball << endl;
- cout << "-----------------------------------------------------------------------------------------------------" << endl;
- break;
- }
- }
- break;
- }
- }
- }
- //works
- void TaskSolution()
- {
- FromFileToStruct();
- for (int i = 0; i < NumOfStudents(); i++)
- {
- if (Student[i].phys == 5 || Student[i].phys == 4 && Student[i].math > 8 && Student[i].IT > 8 && Student[i].chem > 8)
- {
- cout << "ФИО:" << Student[i].FIO << endl << "Год рождения:" << Student[i].yearofborn << endl << "Номер группы:" << Student[i].groupnumber << endl << "Математика:" << Student[i].math << endl << "Физика:" << Student[i].phys << endl << "ОАиП:" << Student[i].IT << endl << "Химия:" << Student[i].chem << endl << "Средний балл:" << Student[i].averageball << endl;
- }
- }
- }
- //works
- void CheckFile(Function X)
- {
- system("cls");
- if ((F = fopen("stud_list.txt", "a+") )== NULL)
- {
- cout << "Не удаётся открыть/использовать файл" << endl;
- }
- else
- {
- X(); fclose(F);
- }
- }
- //works
- void FileClean()
- {
- fout.open(pathtofile, ios::trunc);
- fout.close();
- }
- //works
- int NumOfStudents()
- {
- string tmp;
- ifstream fin("stud_list.txt");
- int num = 0;
- if (fin.is_open())
- {
- while (getline(fin, tmp))
- {
- num++;
- }
- }
- tmp = "";
- fin.close();
- return num;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement