Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string>
- #include <stdlib.h>
- #include <fstream>
- #include <io.h>
- #include <vector>
- #include <iostream>
- struct Man {
- char fam[20];
- char name[20];
- char patronymic[20];
- int year;
- };
- struct Student {
- int group;
- int marks[4];
- double sr_ball;
- Man ob;
- };
- //Функция ввода студентов в массив, где первый
- //параметр - это имя заполняемого массива,
- //а два следующих параметра - это номера
- //элементов массива, т.е. с какого по какой.
- void vvod_stud(Student*, int, int, std::ofstream &file);
- //Функция вывода тех студентов, у кого средний балл выше 4.0
- void vivod_max_srball(const Student*, int);
- //Функция сортировки по полю фамилия
- void sort_fam(Student*, int, std::ofstream &f);
- void read_file(std::ifstream &file, std::vector <std::string> &vec, std::string str);
- //Функция ввода одного студента.
- void vvod(Student*, std::ofstream &file);
- //Функция вывода одного судента.
- void vivod(const Student*);
- //Функция вывода всех студентов
- void vivod_stud(const Student*, int);
- int kol_udal_stud(Student*, int);
- //функция проверки файла на существование
- bool FileExists(const char *fname);
- //вывод студентов у которых ср. балл больше 4
- void file_max_ball(std::string n_, std::vector <std::string> v, int ecx, int z);
- //вывод сортирочки с файла
- void file_sort(int ecx, int z, std::vector<std::string> v);
- //вывод инфо с файла
- void vivod_file(bool count, int ecx, int z, std::string str, std::string n_, std::vector<std::string> v, std::vector<std::string> s);
- void main(void) {
- std::vector <std::string> v,s;
- std::string str;
- std::ofstream _out;
- if (FileExists("Students_List.txt") == true) {
- std::ifstream in("Students_List.txt");
- std::ofstream _out;
- bool count = false;
- char str1[20];
- read_file(in, v, str);
- std::string n_ = v[2];
- int z = atoi(n_.c_str());
- z *= 4;
- int n, k, n1, ecx(0);
- while (1)
- {
- puts("\nEnter:\n1 - vivod\n2 - max_sr_ball");
- puts("3-sort\n4-delete\n5-add\n0 exit");
- scanf_s("%d", &k);
- if (!k) break;
- switch (k)
- {
- case 1:
- {
- printf_s("%-20s%-20s%-20s%s\n", "Fam", "Name", "Group", "Sr_ball");
- vivod_file(count,ecx,z,str,n_,v,s);
- /*if (count == false) {
- if (FileExists("Students_List_New.txt") == true) {
- ecx = 0;
- std::ifstream _in("Students_List_New.txt");
- for (int i = 3; i <= z + 2; i++) {
- if (ecx % 4 != 0)
- printf_s("%-20s ", v[i].c_str());
- else {
- printf_s("\n");
- printf_s("%-20s ", v[i].c_str());
- }
- ecx++;
- }
- s.clear();
- read_file(_in, s, str);
- std::string j_ = s[0];
- int z1 = atoi(j_.c_str());
- z1 *= 4;
- ecx = 0;
- for (int i = 1; i <= z1; i++) {
- if (ecx % 4 != 0)
- printf_s("%-20s ", s[i].c_str());
- else {
- printf_s("\n");
- printf_s("%-20s ", s[i].c_str());
- }
- ecx++;
- }
- }
- else {
- ecx = 0;
- for (int i = 3; i <= z + 2; i++) {
- if (ecx % 4 != 0)
- printf_s("%-20s ", v[i].c_str());
- else {
- printf_s("\n");
- printf_s("%-20s ", v[i].c_str());
- }
- ecx++;
- }
- }
- }
- else {
- if (atoi(n_.c_str()) == 1) {
- for (int i = z + 3; i <= z + 4 * 2; i++)
- printf_s("%-20s", v[i].c_str());
- }
- ecx = 0;
- for (int i = z + 6; i < z*atoi(n_.c_str()); i += 4) {
- if (atoi(v[i].c_str()) >= 4) {
- for (int j = 3; j >= 0; j--)
- printf_s("%-20s ", v[i - j].c_str());
- printf_s("\n");
- }
- ecx++;
- if (ecx >= atoi(n_.c_str()))
- break;
- }
- }*/
- break;
- }
- case 2:
- {
- printf_s("%-20s%-20s%-20s%s\n", "Fam", "Name", "Group", "Sr_ball");
- file_max_ball(n_,v,ecx,z);
- /*if (atoi(n_.c_str()) == 1) {
- for (int i = z + 3; i <= z + 4*2; i++)
- printf_s("%-20s", v[i].c_str());
- }
- ecx = 0;
- for (int i = z + 6; i < z*atoi(n_.c_str()); i+=4) {
- if (atoi(v[i].c_str()) >= 4) {
- for (int j = 3; j >= 0; j--)
- printf_s("%-20s ", v[i-j].c_str());
- printf_s("\n");
- }
- ecx++;
- if (ecx >= atoi(n_.c_str()))
- break;
- }*/
- break;
- }
- case 3:
- {
- printf_s("%-20s%-20s%-15s%s\n", "Fam", "Name", "Group", "Sr_ball");
- file_sort(ecx, z, v);
- /*ecx = 0;
- printf_s("%-20s%-20s%-15s%s\n", "Fam", "Name", "Group", "Sr_ball");
- for (int i = z + 3; i < z*2+3; i++) {
- if (ecx % 4 == 0) {
- printf_s("\n");
- printf_s("%-20s", v[i].c_str());
- }
- else
- printf_s("%-20s", v[i].c_str());
- ecx++;
- }*/
- break;
- }
- case 4:
- {
- count = true;
- break;
- }
- case 5:
- {
- if (FileExists("Students_List_New.txt") == false) {
- printf_s("Vvedite kol dobavl: ");
- scanf_s("%d", &n1);
- _out.open("Students_List_New.txt");
- _out << n1 << '\n';
- ecx = n1;
- double arr[4];
- double sr_ball;
- for (int i = 0; i < ecx; i++) {
- printf_s("Fam:\t");
- scanf_s("%s", str1, 20);
- _out << str1 << ' ';
- printf_s("Name:\t");
- scanf_s("%s", str1, 20);
- _out << str1 << ' ';
- printf_s("Patronymic:\t");
- scanf_s("%s", str1, 20);
- printf_s("Year:\t");
- scanf_s("%d", &n1);
- printf_s("Group:\t");
- scanf_s("%d", &n1);
- _out << n1 << ' ';
- printf_s("Marks:\nMat:\t");
- std::cin >> arr[0];
- printf_s("Info:\t");
- std::cin >> arr[1];
- printf_s("Fizika:\t");
- std::cin >> arr[2];
- printf_s("Ekonom:\t");
- std::cin >> arr[3];
- sr_ball = (arr[0] + arr[1] + arr[2] + arr[3]) / 4;
- _out << sr_ball << '\n';
- }
- _out.close();
- }
- else {
- std::ifstream _in("Students_List_New.txt");
- read_file(_in, s, str);
- int a(0), n(0), lim(0), dec(0), cnt(0);
- lim = atoi(s[0].c_str()) * 4;
- a = atoi(s[0].c_str());
- printf_s("Vvedite kol dobav: ");
- scanf_s("%d", &n);
- a += n;
- s[0] = std::to_string(a);
- std::string j_ = s[0];
- int z1 = atoi(j_.c_str());
- z1 *= 4;
- _out.open("Students_List_New.txt");
- _out << s[0].c_str() << '\n';
- for (int i = 0; i <= lim; i++) {
- if (cnt != 0 && cnt % 4 != 0) {
- _out << s[i].c_str() << ' ';
- }
- else if (cnt != 0 && cnt % 4 == 0)
- _out << s[i].c_str() << '\n';
- cnt++;
- }
- double arr[4];
- double sr_ball;
- for (int i = 0; i < n; i++) {
- printf_s("Fam:\t");
- scanf_s("%s", str1, 20);
- _out << str1 << ' ';
- printf_s("Name:\t");
- scanf_s("%s", str1, 20);
- _out << str1 << ' ';
- printf_s("Patronymic:\t");
- scanf_s("%s", str1, 20);
- printf_s("Year:\t");
- scanf_s("%d", &n1);
- printf_s("Group:\t");
- scanf_s("%d", &n1);
- _out << n1 << ' ';
- printf_s("Marks:\nMat:\t");
- std::cin >> arr[0];
- printf_s("Info:\t");
- std::cin >> arr[1];
- printf_s("Fizika:\t");
- std::cin >> arr[2];
- printf_s("Ekonom:\t");
- std::cin >> arr[3];
- sr_ball = (arr[0] + arr[1] + arr[2] + arr[3]) / 4;
- _out << sr_ball << '\n';
- }
- _out.close();
- }
- }
- break;
- }
- }
- }
- else {
- std::ofstream out("Students_List.txt");
- std::ifstream in("Students_List.txt");
- int n, k, n1;
- Student *ob, *ob1;
- printf_s("Vvedite kol studentov: ");
- scanf_s("%d", &n);
- if (n <= 0) {
- puts("Informacia vvedena neverno"); return;
- }
- ob = (Student*)malloc(n * sizeof(Student));
- if (!ob) { printf_s("Net mesta!\n"); return; }
- out << "kol studentov: " << n << '\n';
- vvod_stud(ob, 0, n, out);
- while (1) {
- puts("Enter:\n1 - vivod\n2 - max_sr_ball");
- puts("3-sort\n4-delete\n5-add\n0 exit");
- scanf_s("%d", &k);
- if (!k) break;
- switch (k) {
- case 1:
- if (FileExists("Students_List_New.txt") == true) {
- vivod_stud(ob, n);
- std::ifstream _in("Students_List_New.txt");
- s.clear();
- read_file(_in, s, str);
- std::string j_ = s[0];
- int z1 = atoi(j_.c_str());
- z1 *= 4;
- int ecx = 0;
- for (int i = 1; i <= z1; i++) {
- if (ecx % 4 != 0)
- printf_s("%-20s", s[i].c_str());
- else {
- printf_s("\n");
- printf_s("%-20s", s[i].c_str());
- }
- ecx++;
- }
- printf_s("\n");
- }
- else
- vivod_stud(ob, n);
- break;
- case 2:
- vivod_max_srball(ob, n);
- break;
- case 3:
- sort_fam(ob, n, out);
- break;
- case 4:
- {
- n1 = kol_udal_stud(ob, n);
- if (!n1) {
- printf_s("Net takih studentov\n");
- break;
- }
- ob1 = (Student*)realloc(ob, (n - n1) * sizeof(Student));
- if (ob1)
- {
- ob = ob1;
- n = n - n1;
- }
- else printf_s("Net mesta\n");
- break;
- }
- case 5:
- {
- char str1[20];
- if (FileExists("Students_List_New.txt") == false) {
- printf_s("Vvedite kol dobavl: ");
- scanf_s("%d", &n1);
- _out.open("Students_List_New.txt");
- _out << n1 << '\n';
- int ecx = n1;
- double arr[4];
- double sr_ball;
- for (int i = 0; i < ecx; i++) {
- printf_s("Fam:\t");
- scanf_s("%s", str1, 20);
- _out << str1 << ' ';
- printf_s("Name:\t");
- scanf_s("%s", str1, 20);
- _out << str1 << ' ';
- printf_s("Patronymic:\t");
- scanf_s("%s", str1, 20);
- printf_s("Year:\t");
- scanf_s("%d", &n1);
- printf_s("Group:\t");
- scanf_s("%d", &n1);
- _out << n1 << ' ';
- printf_s("Marks:\nMat:\t");
- std::cin >> arr[0];
- printf_s("Info:\t");
- std::cin >> arr[1];
- printf_s("Fizika:\t");
- std::cin >> arr[2];
- printf_s("Ekonom:\t");
- std::cin >> arr[3];
- sr_ball = (arr[0] + arr[1] + arr[2] + arr[3]) / 4;
- _out << sr_ball << '\n';
- }
- _out.close();
- }
- else {
- std::ifstream _in("Students_List_New.txt");
- read_file(_in, s, str);
- int a(0), n(0), lim(0), dec(0), cnt(0);
- lim = atoi(s[0].c_str()) * 4;
- a = atoi(s[0].c_str());
- printf_s("Vvedite kol dobav: ");
- scanf_s("%d", &n);
- a += n;
- s[0] = std::to_string(a);
- std::string j_ = s[0];
- int z1 = atoi(j_.c_str());
- z1 *= 4;
- _out.open("Students_List_New.txt");
- _out << s[0].c_str() << '\n';
- for (int i = 0; i <= lim; i++) {
- if (cnt != 0 && cnt % 4 != 0) {
- _out << s[i].c_str() << ' ';
- }
- else if (cnt != 0 && cnt % 4 == 0)
- _out << s[i].c_str() << '\n';
- cnt++;
- }
- double arr[4];
- double sr_ball;
- for (int i = 0; i < n; i++) {
- printf_s("Fam:\t");
- scanf_s("%s", str1, 20);
- _out << str1 << ' ';
- printf_s("Name:\t");
- scanf_s("%s", str1, 20);
- _out << str1 << ' ';
- printf_s("Patronymic:\t");
- scanf_s("%s", str1, 20);
- printf_s("Year:\t");
- scanf_s("%d", &n1);
- printf_s("Group:\t");
- scanf_s("%d", &n1);
- _out << n1 << ' ';
- printf_s("Marks:\nMat:\t");
- std::cin >> arr[0];
- printf_s("Info:\t");
- std::cin >> arr[1];
- printf_s("Fizika:\t");
- std::cin >> arr[2];
- printf_s("Ekonom:\t");
- std::cin >> arr[3];
- sr_ball = (arr[0] + arr[1] + arr[2] + arr[3]) / 4;
- _out << sr_ball << '\n';
- }
- _out.close();
- }
- break;
- }
- }
- }
- }
- }
- //ф-ция вывода информации с файла
- void vivod_file(bool count, int ecx, int z, std::string str, std::string n_, std::vector<std::string> v, std::vector<std::string> s) {
- if (count == false) {
- if (FileExists("Students_List_New.txt") == true) {
- ecx = 0;
- std::ifstream _in("Students_List_New.txt");
- for (int i = 3; i <= z + 2; i++) {
- if (ecx % 4 != 0)
- printf_s("%-20s ", v[i].c_str());
- else {
- printf_s("\n");
- printf_s("%-20s ", v[i].c_str());
- }
- ecx++;
- }
- s.clear();
- read_file(_in, s, str);
- std::string j_ = s[0];
- int z1 = atoi(j_.c_str());
- z1 *= 4;
- ecx = 0;
- for (int i = 1; i <= z1; i++) {
- if (ecx % 4 != 0)
- printf_s("%-20s ", s[i].c_str());
- else {
- printf_s("\n");
- printf_s("%-20s ", s[i].c_str());
- }
- ecx++;
- }
- }
- else {
- ecx = 0;
- for (int i = 3; i <= z + 2; i++) {
- if (ecx % 4 != 0)
- printf_s("%-20s ", v[i].c_str());
- else {
- printf_s("\n");
- printf_s("%-20s ", v[i].c_str());
- }
- ecx++;
- }
- }
- }
- else {
- if (atoi(n_.c_str()) == 1) {
- for (int i = z + 3; i <= z + 4 * 2; i++)
- printf_s("%-20s", v[i].c_str());
- }
- ecx = 0;
- for (int i = z + 6; i < z*atoi(n_.c_str()); i += 4) {
- if (atoi(v[i].c_str()) >= 4) {
- for (int j = 3; j >= 0; j--)
- printf_s("%-20s ", v[i - j].c_str());
- printf_s("\n");
- }
- ecx++;
- if (ecx >= atoi(n_.c_str()))
- break;
- }
- }
- }
- //функция для чтения с файла и вывод тех студентов у которых средний балл больше 4
- void file_max_ball(std::string n_, std::vector <std::string> v, int ecx, int z) {
- if (atoi(n_.c_str()) == 1) {
- for (int i = z + 3; i <= z + 4 * 2; i++)
- printf_s("%-20s", v[i].c_str());
- }
- ecx = 0;
- for (int i = z + 6; i <= z*atoi(n_.c_str()); i += 4) {
- if (atoi(v[i].c_str()) >= 4) {
- for (int j = 3; j >= 0; j--)
- printf_s("%-20s ", v[i - j].c_str());
- printf_s("\n");
- }
- ecx++;
- if (ecx >= atoi(n_.c_str()))
- break;
- }
- }
- //Ввод одного студента
- void vvod(Student*p, std::ofstream &file)
- {
- printf_s("Fam:\t");
- scanf_s("%s", p->ob.fam, 20);
- file << p->ob.fam << ' ';
- printf_s("Name:\t");
- scanf_s("%s", p->ob.name, 20);
- file << p->ob.name << ' ';
- printf_s("Patronymic:\t");
- scanf_s("%s", p->ob.patronymic, 20);
- printf_s("Year:\t");
- scanf_s("%d", &p->ob.year);
- printf_s("Group:\t");
- scanf_s("%d", &p->group);
- file << p->group << ' ';
- printf_s("Marks:\nMat:\t");
- scanf_s("%d", &p->marks[0]);
- printf_s("Info:\t");
- scanf_s("%d", &p->marks[1]);
- printf_s("Fizika:\t");
- scanf_s("%d", &p->marks[2]);
- printf_s("Ekonom:\t");
- scanf_s("%d", &p->marks[3]);
- p->sr_ball = (p->marks[0] + (double)p->marks[1] + p->marks[2] + p->marks[3]) / 4;
- file << p->sr_ball << '\n';
- }
- //чтение сортировки с файла
- void file_sort(int ecx, int z, std::vector<std::string> v) {
- ecx = 0;
- for (int i = z + 3; i < z * 2 + 3; i++) {
- if (ecx % 4 == 0) {
- printf_s("\n");
- printf_s("%-20s", v[i].c_str());
- }
- else
- printf_s("%-20s", v[i].c_str());
- ecx++;
- }
- }
- //чтение с файла и запись его содержимого в вектор
- void read_file(std::ifstream &file, std::vector <std::string> &vec, std::string str) {
- if (!file.is_open())
- {
- printf_s("cannot open file");
- }
- while (file >> str)
- vec.push_back(str);
- }
- //Ввод массива студентов
- void vvod_stud(Student*ob, int k1, int k2, std::ofstream &file) {
- for (int i = k1; i<k2; i++) vvod(&ob[i], file);
- }
- bool FileExists(const char *fname)
- {
- return _access(fname, 0) != -1;
- }
- //Вывод одного студента
- void vivod(const Student*ob) {
- printf_s("%-20s%-20s%-15d%.2lf\n", ob->ob.fam, ob->ob.name, ob->group, ob->sr_ball);
- }
- //Вывод массива студентов
- void vivod_stud(const Student*ob, int n)
- {
- printf_s("%-20s%-20s%-15s%s\n", "Fam", "Name", "Group", "Sr_ball");
- for (int i = 0; i<n; i++) vivod(&ob[i]);
- }
- //Функция вывода тех студентов у кого средний балл выше 4.0
- void vivod_max_srball(const Student*ob, int n)
- { int k = 0;
- for (int i = 0; i<n; i++)
- {
- if (ob[i].sr_ball >= 4.0) {
- k++;
- vivod(&ob[i]);
- }
- }
- if (k == 0) printf_s("Net takih studentov\n");
- }
- //Функция сортировки по полю фамилия
- void sort_fam(Student*ob, int n, std::ofstream &f)
- {
- int i, j, k;
- Student buf;
- for (i = 0; i<n; i++)
- {
- k = i;
- for (j = i + 1; j<n; j++)
- {
- if (ob[k].group > ob[j].group) {
- k = j;
- buf = ob[i];
- ob[i] = ob[k];
- ob[k] = buf;
- }
- }
- }
- for (i = 0; i<n; i++)
- {
- k = i;
- for (j = i + 1; j<n; j++)
- {
- if ((ob[k].group == ob[j].group) && (ob[k].sr_ball < ob[j].sr_ball)) {
- k = j;
- buf = ob[i];
- ob[i] = ob[k];
- ob[k] = buf;
- }
- }
- }
- for (i = 0; i < n; i++)
- f << (&ob[i])->ob.fam << ' ' << (&ob[i])->ob.name << ' ' << (&ob[i])->group << ' ' << (&ob[i])->sr_ball << '\n';
- f.close();
- }
- //Функция считает тех студентов, у кого средний балл ниже 4.0 и смещает их.
- int kol_udal_stud(Student*ob, int n)
- {
- int k = 0;
- for (int i = 0; i<n - k; i++)
- {
- if (ob[i].sr_ball<4.0)
- {
- k++;
- for (int j = i; j<n - k; j++) ob[j] = ob[j + 1];
- i--;
- }
- }
- return k;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement