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>
- 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(Student a[], FILE* filetxt, int i, int* n) {
- while (filetxt) {
- fscanf_s(filetxt, "%s", a[i].fio.surname, _countof(a[i].fio.surname));
- if (strlen(a[i].fio.surname) < 2) {
- *n = i;
- break;
- }
- fscanf_s(filetxt, "%d", &a[i].exams);
- for (int j = 0; j < a[i].exams; j++) {
- fscanf_s(filetxt, "%s", a[i].disp[j], _countof(a[i].disp[j]));
- }
- fscanf_s(filetxt, "%s", a[i].fio.name, _countof(a[i].fio.name));
- fscanf_s(filetxt, "%s", a[i].fio.patronymic, _countof(a[i].fio.patronymic));
- fscanf_s(filetxt, "%d", &a[i].group);
- for (int j = 0; j < a[i].exams; j++) {
- fscanf_s(filetxt, "%d", &a[i].mark[j]);
- }
- i++;
- }
- }
- void save_file(Student a[], FILE* filetxt2, int i, int* n) {
- int v = sizeof(a) / sizeof(a[0]);
- for (int i = 0; i < v; i++) {
- fprintf(filetxt2, "%s", a[i].fio.name); cout << " ";
- fprintf(filetxt2, "%s", a[i].fio.surname); cout << " ";
- fprintf(filetxt2, "%s", a[i].fio.patronymic); cout << " ";
- fprintf(filetxt2, "%d", &a[i].exams); cout << " ";
- fprintf(filetxt2, "%d", &a[i].group); cout << " ";
- for (int j = 0; j < 7; j++) {
- fscanf_s(filetxt2, "%d", &a[i].mark[j]);
- }
- cout << endl;
- }
- }
- void print_info(Student a[], int n) {
- for (int i = 0; i < n; i++) {
- cout <<setw(30)<<left<< a[i].fio.surname<<" "<<setw(4)<<left<<a[i].exams;
- for (int j = 0; j < a[i].exams; j++) {
- cout <<setw(7)<<left<< a[i].disp[j] << " ";
- }
- cout << endl;
- cout << setw(15)<<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(7)<< a[i].mark[j] << " ";
- }
- cout << endl;
- cout << endl;
- }
- cout << endl;
- }
- void add_info(Student a[], int n) {
- cout<<"Введите ФИО студента: ";
- cin >> a[n].fio.surname;
- cin >> a[n].fio.name;
- cin >> a[n].fio.patronymic;
- cout << "Введите число экзаменов: ";
- cin >> a[n].exams;
- cout << "Введите номер группы: ";
- cin >> a[n].group;
- cout << "Введите названия экзаменов: ";
- for (int j = 0; j < a[n].exams; j++) {
- cin >> a[n].disp[j];
- }
- cout << "Введите оценки студента по этим предметам: ";
- for (int j = 0; j < a[n].exams; j++) {
- cin >> a[n].mark[j];
- }
- }
- void delete_info(Student a[], int n) {
- char key1[20];
- cout << "Введите ключ,по которому хотите удалить: ";
- cin >> key1;
- for (int i = 0; i < n; 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; i < n; 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].group = a[i + 1].group;
- a[i].exams = a[i + 1].exams;
- 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];
- }
- }
- }
- }
- }
- void add_info1(Student a[], int n) {
- int p;
- printf("Введите номер ячейки, перед которой хотите вставить данные вставить данные: ");
- cin >> p;
- char yep[20] = " ";
- int repl[7];
- int repl2;
- int repl3;
- cout << "Введите ФИО студента: ";
- cin >> a[n].fio.surname;
- cin >> a[n].fio.name;
- cin >> a[n].fio.patronymic;
- cout << "Введите число экзаменов: ";
- cin >> a[n].exams;
- cout << "Введите номер группы: ";
- cin >> a[n].group;
- cout << "Введите названия экзаменов: ";
- for (int j = 0; j < a[n].exams; j++) {
- cin >> a[n].disp[j];
- }
- cout << "Введите оценки студента по этим предметам: ";
- for (int j = 0; j < a[n].exams; j++) {
- cin >> a[n].mark[j];
- }
- for (int i = n; i > p; i--) {
- strcpy_s(yep, a[i - 1].fio.name);
- strcpy_s(a[i - 1].fio.name, a[i].fio.name);
- strcpy_s(a[i].fio.name, yep);
- strcpy_s(yep, a[i - 1].fio.surname);
- strcpy_s(a[i - 1].fio.surname, a[i].fio.surname);
- strcpy_s(a[i].fio.surname, yep);
- strcpy_s(yep, a[i - 1].fio.patronymic);
- strcpy_s(a[i - 1].fio.patronymic, a[i].fio.patronymic);
- strcpy_s(a[i].fio.patronymic, yep);
- repl2 = a[i - 1].exams;
- a[i - 1].exams = a[i].exams;
- a[i].exams = repl2;
- repl3 = a[i - 1].group;
- a[i - 1].group = a[i].group;
- a[i].group = repl3;
- for (int j = 0; j < a[i].exams; j++) {
- strcpy_s(yep, a[i - 1].disp[j]);
- strcpy_s(a[i - 1].disp[j], a[i].disp[j]);
- strcpy_s(a[i].disp[j], yep);
- }
- for (int j = 0; j < a[i].exams; j++) {
- repl[j] = a[i - 1].mark[j];
- a[i - 1].mark[j] = a[i].mark[j];
- a[i].mark[j] = repl[j];
- }
- }
- }
- void add_info2(Student a[], int n) {
- int p;
- printf("Введите номер ячейки, после которой хотите вставить данные вставить данные: ");
- cin >> p;
- char yep[20] = " ";
- int repl[7];
- int repl2;
- int repl3;
- cout << "Введите ФИО студента: ";
- cin >> a[n].fio.surname;
- cin >> a[n].fio.name;
- cin >> a[n].fio.patronymic;
- cout << "Введите число экзаменов: ";
- cin >> a[n].exams;
- cout << "Введите номер группы: ";
- cin >> a[n].group;
- cout << "Введите названия экзаменов: ";
- for (int j = 0; j < a[n].exams; j++) {
- cin >> a[n].disp[j];
- }
- cout << "Введите оценки студента по этим предметам: ";
- for (int j = 0; j < a[n].exams; j++) {
- cin >> a[n].mark[j];
- }
- for (int i = n; i > p + 1; i--) {
- strcpy_s(yep, a[i - 1].fio.name);
- strcpy_s(a[i - 1].fio.name, a[i].fio.name);
- strcpy_s(a[i].fio.name, yep);
- strcpy_s(yep, a[i - 1].fio.surname);
- strcpy_s(a[i - 1].fio.surname, a[i].fio.surname);
- strcpy_s(a[i].fio.surname, yep);
- strcpy_s(yep, a[i - 1].fio.patronymic);
- strcpy_s(a[i - 1].fio.patronymic, a[i].fio.patronymic);
- strcpy_s(a[i].fio.patronymic, yep);
- repl2 = a[i - 1].exams;
- a[i - 1].exams = a[i].exams;
- a[i].exams = repl2;
- repl3 = a[i - 1].group;
- a[i - 1].group = a[i].group;
- a[i].group = repl3;
- for (int j = 0; j < a[n].exams; j++) {
- strcpy_s(yep, a[i - 1].disp[j]);
- strcpy_s(a[i - 1].disp[j], a[i].disp[j]);
- strcpy_s(a[i].disp[j], yep);
- }
- for (int j = 0; j < a[n].exams; j++) {
- repl[j] = a[i - 1].mark[j];
- a[i - 1].mark[j] = a[i].mark[j];
- a[i].mark[j] = repl[j];
- }
- }
- }
- void search_info(Student a[], int n) {
- char key[20];
- int f = 1;
- printf("Введите слово для поиска: ");
- cin >> key;
- for (int i = 0; i < n; 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(30) << left << a[i].fio.surname << " " << setw(4) << left << a[i].exams;
- for (int j = 0; j < a[i].exams; j++) {
- cout << setw(7) << left << a[i].disp[j] << " ";
- }
- cout << endl;
- cout << setw(15) << 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(7) << a[i].mark[j] << " ";
- }
- cout << endl;
- cout << endl;
- }
- cout << endl;
- }
- }
- void sort(Student a[], int n) {
- char repl[20] = " ";
- int p[7];
- int p1;
- int p2;
- for (int i = 0; i < n-1 ; i++) { //n-1
- for (int j = 0; j < n - 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(Student a[], int n) {
- char repl[20] = " ";
- int p[7];
- int p1;
- int p2;
- for (int i = 0; i < n - 1; i++) {
- for (int j = 0; j < n - 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];
- }
- }
- }
- }
- for (int i = 0; i < n - 1; i++) {
- for (int j = 0; j < n - i - 1; j++) {
- if (strcmp(a[j].fio.surname, a[j + 1].fio.surname) == 0){
- if (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(a, filetxt, i, &n);
- if (k == 2) {
- add_info(a, n);
- n++;
- }
- if (k == 3)
- search_info(a, n);
- if (k == 4) {
- delete_info(a, n);
- n--;
- }
- if (k == 5) {
- add_info1(a, n);
- n++;
- }
- if (k == 6) {
- add_info2(a, n);
- n++;
- }
- if (k == 7) {
- sort(a, n);
- }
- if (k == 8) {
- sort1(a, n);
- }
- if (k == 9) {
- for (int i = 0; i < n; i++) {
- fprintf_s(filetxt2, a[i].fio.name);
- fprintf_s(filetxt2, " ");
- fprintf_s(filetxt2, a[i].fio.surname);
- fprintf_s(filetxt2, " ");
- fprintf_s(filetxt2, a[i].fio.patronymic);
- fprintf_s(filetxt2, " ");
- fprintf_s(filetxt2, "%d", a[i].exams);
- fprintf_s(filetxt2, " ");
- fprintf_s(filetxt2, "%d", a[i].group);
- fprintf_s(filetxt2, " ");
- for (int j = 0; j < 7; j++) {
- fprintf_s(filetxt2, "%d", a[i].mark[j]);
- fprintf_s(filetxt2, " ");
- }
- fprintf_s(filetxt2, "\n");
- }
- }
- if (k == 0)
- print_info(a, n);
- if (k == 10) {
- fclose(filetxt);
- fclose(filetxt2);
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement