Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <stdio.h>
- #include <clocale>
- #include <fstream>
- #include <iomanip>
- #include <string>
- #include <vector>
- using namespace std;
- struct FIO {
- char name[20];
- char surname[20];
- char patronymic[20];
- };
- struct Student {
- FIO fio;
- int group;
- int exams;
- int mark[7];
- char disp[7][20];
- };
- void read_file(FILE* filetxt,vector<Student> v) {
- Student a;
- while (filetxt) {
- fscanf_s(filetxt, "%s", a.fio.surname, _countof(a.fio.surname));
- if (strlen(a.fio.surname) < 2) {
- break;
- }
- fscanf_s(filetxt, "%d", &a.exams);
- for (int j = 0; j < a.exams; j++) {
- fscanf_s(filetxt, "%s", a.disp[j], _countof(a.disp[j]));
- }
- fscanf_s(filetxt, "%s", a.fio.name, _countof(a.fio.name));
- fscanf_s(filetxt, "%s", a.fio.patronymic, _countof(a.fio.patronymic));
- fscanf_s(filetxt, "%d", &a.group);
- for (int j = 0; j < a.exams; j++) {
- fscanf_s(filetxt, "%d", &a.mark[j]);
- }
- v.push_back(a);
- }
- }
- void print_info(Student a[]) {
- for (int i = 0; i < v.size(); i++) {
- cout << setw(25) << left << a[i].fio.surname << " " << setw(4) << left << a[i].exams;
- for (int j = 0; j < a[i].exams; j++) {
- cout << a[i].disp[j] << " ";
- }
- cout << endl;
- cout << setw(10) << left << a[i].fio.name << " " << setw(15) << left << a[i].fio.patronymic << setw(4) << left << a[i].group << " ";
- for (int j = 0; j < a[i].exams; j++) {
- cout << setw(4) << a[i].mark[j] << " ";
- }
- cout << endl;
- cout << endl;
- }
- cout << endl;
- }
- void add_info(vector<Student> v) {
- Student a;
- cout << "Введите ФИО студента: ";
- cin >> a.fio.surname;
- cin >> a.fio.name;
- cin >> a.fio.patronymic;
- cout << "Введите число экзаменов: ";
- cin >> a.exams;
- cout << "Введите номер группы: ";
- cin >> a.group;
- cout << "Введите названия экзаменов: ";
- for (int j = 0; j < a.exams; j++) {
- cin >> a.disp[j];
- }
- cout << "Введите оценки студента по этим предметам: ";
- for (int j = 0; j < a.exams; j++) {
- cin >> a.mark[j];
- }
- v.push_back(a);
- }
- void delete_info(vector<Student> v) {
- char key1[20];
- cout << "Введите ключ,по которому хотите удалить: ";
- cin >> key1;
- for (int i = 0; i < v.size(); i++) {
- if ((strcmp(key1, a[i].fio.name) == 0) || (strcmp(key1, a[i].fio.surname) == 0) || (strcmp(key1, a[i].fio.patronymic) == 0)) {
- for (int s = i; s < n - 1; s++) {
- strcpy_s(a[s].fio.name, a[s + 1].fio.name);
- strcpy_s(a[s].fio.surname, a[s + 1].fio.surname);
- strcpy_s(a[s].fio.patronymic, a[s + 1].fio.patronymic);
- a[s].group = a[s + 1].group;
- a[s].exams = a[s + 1].exams;
- for (int j = 0; j < a[s].exams; j++) {
- strcpy_s(a[s].disp[j], a[s + 1].disp[j]);
- }
- for (int j = 0; j < a[s].exams; j++) {
- a[s].mark[j] = a[s + 1].mark[j];
- }
- }
- i--;
- }
- }
- }
- void add_info1(vector<Student> v) {
- int p;
- printf("Введите номер ячейки, перед которой хотите вставить данные вставить данные (нумерация с 0): ");
- cin >> p;
- if (p>v.size()) p = v.size();
- if (p < 0) p = 0;
- for (int i = v.size(); i > p; i--) {
- strcpy_s(a[i].fio.name, a[i - 1].fio.name);
- strcpy_s(a[i].fio.surname, a[i - 1].fio.surname);
- strcpy_s(a[i].fio.patronymic, a[i - 1].fio.patronymic);
- a[i].exams = a[i - 1].exams;
- a[i].group = a[i - 1].group;
- for (int j = 0; j < a[i].exams; j++) {
- strcpy_s(a[i].disp[j], a[i - 1].disp[j]);
- }
- for (int j = 0; j < a[i].exams; j++) {
- a[i].mark[j] = a[i - 1].mark[j];
- }
- }
- cout << "Введите ФИО студента: ";
- cin >> a[p].fio.surname;
- cin >> a[p].fio.name;
- cin >> a[p].fio.patronymic;
- cout << "Введите число экзаменов: ";
- cin >> a[p].exams;
- cout << "Введите номер группы: ";
- cin >> a[p].group;
- cout << "Введите названия экзаменов: ";
- for (int j = 0; j < a[p].exams; j++) {
- cin >> a[p].disp[j];
- }
- cout << "Введите оценки студента по этим предметам: ";
- for (int j = 0; j < a[p].exams; j++) {
- cin >> a[p].mark[j];
- }
- }
- void add_info2(vector<Student> v) {
- int p;
- printf("Введите номер ячейки, после которой хотите вставить данные вставить данные (нумерация с 0): ");
- cin >> p;
- if (p>n) p = n;
- if (p < 0) p = 0;
- p++;
- for (int i = n; i > p; i--) {
- strcpy_s(a[i].fio.name, a[i - 1].fio.name);
- strcpy_s(a[i].fio.surname, a[i - 1].fio.surname);
- strcpy_s(a[i].fio.patronymic, a[i - 1].fio.patronymic);
- a[i].exams = a[i - 1].exams;
- a[i].group = a[i - 1].group;
- for (int j = 0; j < a[n].exams; j++) {
- strcpy_s(a[i].disp[j], a[i - 1].disp[j]);
- }
- for (int j = 0; j < a[n].exams; j++) {
- a[i].mark[j] = a[i - 1].mark[j];
- }
- }
- cout << "Введите ФИО студента: ";
- cin >> a[p].fio.surname;
- cin >> a[p].fio.name;
- cin >> a[p].fio.patronymic;
- cout << "Введите число экзаменов: ";
- cin >> a[p].exams;
- cout << "Введите номер группы: ";
- cin >> a[p].group;
- cout << "Введите названия экзаменов: ";
- for (int j = 0; j < a[p].exams; j++) {
- cin >> a[p].disp[j];
- }
- cout << "Введите оценки студента по этим предметам: ";
- for (int j = 0; j < a[p].exams; j++) {
- cin >> a[p].mark[j];
- }
- }
- void search_info(vector<Student> v) {
- char key[20];
- int f = 1;
- cout << "Введите слово для поиска: ";
- cin >> key;
- cout << endl;
- for (int i = 0; i < v.size(); i++) {
- if (strcmp(key, a[i].fio.surname) == 0 || strcmp(key, a[i].fio.name) == 0 || strcmp(key, a[i].fio.patronymic) == 0) {
- cout << setw(25) << left << a[i].fio.surname << " " << setw(4) << left << a[i].exams;
- for (int j = 0; j < a[i].exams; j++) {
- cout << a[i].disp[j] << " ";
- }
- cout << endl;
- cout << setw(10) << left << a[i].fio.name << " " << setw(15) << left << a[i].fio.patronymic << setw(4) << left << a[i].group << " ";
- for (int j = 0; j < a[i].exams; j++) {
- cout << setw(4) << a[i].mark[j] << " ";
- }
- cout << endl;
- cout << endl;
- }
- cout << endl;
- }
- }
- void sort(vector<Student> v) {
- char repl[20] = " ";
- int p[7];
- int p1;
- int p2;
- for (int i = 0; i < v.size() - 1; i++) {
- for (int j = 0; j < v.size() - i - 1; j++) {
- if (strcmp(a[j].fio.surname, a[j + 1].fio.surname) > 0) {
- strcpy_s(repl, a[j].fio.name);
- strcpy_s(a[j].fio.name, a[j + 1].fio.name);
- strcpy_s(a[j + 1].fio.name, repl);
- strcpy_s(repl, a[j].fio.surname);
- strcpy_s(a[j].fio.surname, a[j + 1].fio.surname);
- strcpy_s(a[j + 1].fio.surname, repl);
- strcpy_s(repl, a[j].fio.patronymic);
- strcpy_s(a[j].fio.patronymic, a[j + 1].fio.patronymic);
- strcpy_s(a[j + 1].fio.patronymic, repl);
- p2 = a[j].group;
- a[j].group = a[j + 1].group;
- a[j + 1].group = p2;
- p1 = a[j].exams;
- a[j].exams = a[j + 1].exams;
- a[j + 1].exams = p1;
- for (int r = 0; r < 7; r++) {
- strcpy_s(repl, a[j].disp[r]);
- strcpy_s(a[j].disp[r], a[j + 1].disp[r]);
- strcpy_s(a[j + 1].disp[r], repl);
- }
- for (int r = 0; r < 7; r++) {
- p[r] = a[j].mark[r];
- a[j].mark[r] = a[j + 1].mark[r];
- a[j + 1].mark[r] = p[r];
- }
- }
- }
- }
- }
- void sort1(vector<Student> v) {
- char repl[20] = " ";
- int p[7];
- int p1;
- int p2;
- for (int i = 0; i < v.size() - 1; i++) {
- for (int j = 0; j < v.size() - i - 1; j++) {
- if (strcmp(a[j].fio.surname, a[j + 1].fio.surname) > 0 ||
- strcmp(a[j].fio.surname, a[j + 1].fio.surname) == 0 && strcmp(a[j].fio.name, a[j + 1].fio.name) > 0) {
- strcpy_s(repl, a[j].fio.name);
- strcpy_s(a[j].fio.name, a[j + 1].fio.name);
- strcpy_s(a[j + 1].fio.name, repl);
- strcpy_s(repl, a[j].fio.surname);
- strcpy_s(a[j].fio.surname, a[j + 1].fio.surname);
- strcpy_s(a[j + 1].fio.surname, repl);
- strcpy_s(repl, a[j].fio.patronymic);
- strcpy_s(a[j].fio.patronymic, a[j + 1].fio.patronymic);
- strcpy_s(a[j + 1].fio.patronymic, repl);
- p2 = a[j].group;
- a[j].group = a[j + 1].group;
- a[j + 1].group = p2;
- p1 = a[j].exams;
- a[j].exams = a[j + 1].exams;
- a[j + 1].exams = p1;
- for (int r = 0; r < 7; r++) {
- strcpy_s(repl, a[j].disp[r]);
- strcpy_s(a[j].disp[r], a[j + 1].disp[r]);
- strcpy_s(a[j + 1].disp[r], repl);
- }
- for (int r = 0; r < 7; r++) {
- p[r] = a[j].mark[r];
- a[j].mark[r] = a[j + 1].mark[r];
- a[j + 1].mark[r] = p[r];
- }
- }
- }
- }
- }
- Student a[20];
- int main() {
- setlocale(LC_ALL, "rus");
- FILE* filetxt;
- FILE* filetxt2;
- fopen_s(&filetxt, "students_info.txt", "a+");
- fopen_s(&filetxt2, "student_info2.txt", "w");
- int i = 0, n = 0;
- int k = -1;
- while (k != 10) {
- printf(" 1-формирование данных\n 2-добавить данные\n 3-поиск данных по ключу\n 4-удаление данных по ключу\n 5-вставка данных в массив перед i-ой записью\n 6-вставка данных в массив после i-ой записи\n 7-сортировка по фамилии\n 8-сортировка по имени и фамилии\n 9-сохранение данных в файл\n 0-вывод данных\n 10-выход\n");
- cin >> k;
- if (k == 1)
- read_file(filetxt);
- if (k == 2) {
- add_info(a);
- }
- if (k == 3)
- search_info(a);
- if (k == 4) {
- delete_info(a);
- }
- if (k == 5) {
- add_info1(a);
- }
- if (k == 6) {
- add_info2(a);
- }
- if (k == 7) {
- sort(a);
- }
- if (k == 8) {
- sort1(a);
- }
- if (k == 9) {
- for (int i = 0; i < n; i++) {
- fprintf(filetxt2, "%-25s", a[i].fio.surname); fprintf(filetxt2, " ");
- fprintf(filetxt2, "%-4d", a[i].exams);
- for (int j = 0; j < a[i].exams; j++) {
- fprintf(filetxt2, "%s", a[i].disp[j]);
- fprintf_s(filetxt2, " ");
- }
- fprintf(filetxt2, "\n");
- fprintf(filetxt2, "%-10s", a[i].fio.name); fprintf(filetxt2, " ");
- fprintf(filetxt2, "%-15s", a[i].fio.patronymic);
- fprintf(filetxt2, "%-4d", a[i].group); fprintf(filetxt2, " ");
- for (int j = 0; j < a[i].exams; j++) {
- fprintf_s(filetxt2, "%-4d", a[i].mark[j]);
- fprintf(filetxt2, " ");
- }
- fprintf(filetxt2, "\n");
- fprintf(filetxt2, "\n");
- }
- }
- if (k == 0)
- print_info(a);
- if (k == 10) {
- fclose(filetxt);
- fclose(filetxt2);
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement