Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <iomanip>
- #include <conio.h>
- #include <string.h>
- #include <fstream>
- #include <sstream>
- #include <Windows.h>
- using namespace std;
- //void input MasStud( int i, student a[]);
- //void output MasStud(int i, student a[]);
- struct date
- {
- int d;
- int m;
- int y;
- };
- struct student
- {
- string name;
- string f;
- string o;
- date dr;
- };
- struct sezon
- {
- string sez;
- int col;
- };
- //#define MAX_LINE 25
- //student a[MAX_LINE];
- //sezon y[4];
- //int i;
- const int L = 30;
- void inputMasStudFile(int &n, student *&px)
- {
- char file[L];
- // n = 0;
- setlocale(LC_ALL, "rus");
- ifstream fin;
- student t;
- cout << "Имя файла: ";
- cin >> file;
- strcat_s(file, ".txt");
- fin.open(file);
- if (fin.fail())
- {
- cout << file << "Не открылся\n ";
- _getch();
- return;
- }
- n = 0;
- while (!fin.eof())
- {
- fin >> t.name >> t.f >> t.o >> t.dr.d >> t.dr.m >> t.dr.y;
- if (fin.fail()) break;
- n++;
- }
- fin.close();
- px = new student[n];
- if (px == NULL)
- {
- cout << "Нет памяти";
- _getch();
- n = 0;
- return;
- }
- fin.open(file);
- if (fin.fail())
- {
- cout << "Файл не открылся";
- _getch();
- delete[] px;
- px = NULL;
- n = 0;
- return;
- }
- for (int i=0; i < n; i++)
- fin >> px[i].name >> px[i].f >> px[i].o >> px[i].dr.d >> px[i].dr.m >> px[i].dr.y;
- fin.close();
- cout << "Файл введен";
- _getch();
- }
- void outputMasStudFile(int n, student px[])
- {
- int j;
- cout << "_____________________________________________________________\n";
- cout << " | | \n";
- cout << "№ | Имя студента | Дата рождения \n";
- cout << " | |__________________________\n";
- cout << " | | День | Месяц | Год \n";
- cout << "__|___________________________________________________________\n";
- for (j = 0; j < n; j++)
- cout << setw(2) << j + 1 << "|" << setw(27) << px[j].name << " " << px[j].f << "." << px[j].o << " | " << setw(6) << px[j].dr.d << " | " << setw(7) << px[j].dr.m << " | " << setw(4) << px[j].dr.y << endl;
- }
- void sortyear(int n, student px[]) //сортировка по дате в порядке возрастания
- {
- student s;
- for (int i = 0; i<n; i++)
- for (int j = 0; j<n; j++)
- if (px[i].dr.y<px[j].dr.y) //сортировка по годам
- {
- s = px[i];
- px[i] = px[j];
- px[j] = s;
- }
- }
- void sortname(int n , student px[]) //по алфавиту
- {
- student t;
- for (int i = 0; i<n; i++)
- for (int j = 0; j<n; j++)
- if (px[i].name<px[j].name)
- {
- t = px[i];
- px[i] = px[j];
- px[j] = t;
- }
- for (int i = 0; i<n; i++)
- for (int j = 0; j<n; j++)
- if (px[i].name == px[j].name)
- {
- if (px[i].f < px[j].f)
- {
- t = px[i];
- px[i] = px[j];
- px[j] = t;
- }
- }
- for (int i = 0; i<n; i++)
- for (int j = 0; j<n; j++)
- if (px[i].name == px[j].name && px[i].f == px[j].f)
- {
- if (px[i].o < px[j].o)
- {
- t = px[i];
- px[i] = px[j];
- px[j] = t;
- }
- }
- }
- void sortyearvixod(int n, student px[]) //сортировка по дате в порядке возрастания
- {
- student s;
- //student y;
- for (int i = 0; i<n; i++)
- for (int j = 0; j<n; j++)
- if (px[i].dr.y>px[j].dr.y) //сортировка по годам
- {
- s = px[i];
- px[i] = px[j];
- px[j] = s;
- }
- for (int i = 0; i<n; i++)//сортировка по месяцам
- for (int j = 0; j<n; j++)
- if (px[i].dr.y == px[j].dr.y)
- {
- if (px[i].dr.m>px[j].dr.m)
- {
- s = px[i];
- px[i] = px[j];
- px[j] = s;
- }
- }
- for (int i = 0; i<n; i++)//сортировка по дням
- for (int j = 0; j<n; j++)
- if (px[i].dr.y == px[j].dr.y && px[i].dr.m == px[j].dr.m)
- {
- if (px[i].dr.d>px[j].dr.d)
- {
- s = px[i];
- px[i] = px[j];
- px[j] = s;
- }
- }
- //a[k] = y[l];
- }
- void sortnameyear(int n, student px[]) //совмещенная сортировка
- {
- student s;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- if (px[i].dr.y < px[j].dr.y) //сортировка по годам
- {
- s = px[i];
- px[i] = px[j];
- px[j] = s;
- }
- for (int i = 0; i < n; i++)//сортировка по месяцам
- for (int j = 0; j < n; j++)
- if (px[i].dr.y == px[j].dr.y)
- {
- if (px[i].dr.m < px[j].dr.m)
- {
- s = px[i];
- px[i] = px[j];
- px[j] = s;
- }
- }
- for (int i = 0; i < n; i++)//сортировка по дням
- for (int j = 0; j < n; j++)
- if (px[i].dr.y == px[j].dr.y && px[i].dr.m == px[j].dr.m)
- {
- if (px[i].dr.d < px[j].dr.d)
- {
- s = px[i];
- px[i] = px[j];
- px[j] = s;
- }
- }
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- if (px[i].dr.y == px[j].dr.y && px[i].dr.m == px[j].dr.m && px[i].dr.d == px[j].dr.d)
- {
- if (px[i].name < px[j].name)
- {
- s = px[i];
- px[i] = px[j];
- px[j] = s;
- }
- }
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- if (px[i].name == px[j].name)
- {
- if (px[i].f < px[j].f)
- {
- s = px[i];
- px[i] = px[j];
- px[j] = s;
- }
- }
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- if (px[i].name == px[j].name && px[i].f == px[j].f)
- {
- if (px[i].o < px[j].o)
- {
- s = px[i];
- px[i] = px[j];
- px[j] = s;
- }
- }
- }
- void deleten(int &n, student *&px)
- {
- int i, j;
- student *p;
- char ch;
- outputMasStudFile(n, px);
- cout << "Номер указанной строки";
- cin >> j;
- j--;
- if (j < 0 || j >= n)
- {
- cout << "Такой строки нет";
- _getch();
- return;
- }
- //j--;
- cout << j + 1 << "-я строка";
- cout << px[j].name << px[j].f << px[j].o << px[j].o << px[j].dr.d << px[j].dr.m << px[j].dr.y << endl;
- cout << "Удалить?" << endl;
- cout << "Yes/No" << endl;
- cin >> ch;
- if (ch == 'n')
- return;
- if (ch != 'y')
- {
- cout << "Некорректно";
- _getch();
- return;
- }
- if (n == 1)
- {
- delete[]px;
- px == NULL;
- n = 0;
- }
- else
- {
- p = new student[n - 1];
- if (p == NULL)
- {
- cout << "Нет памяти";
- _getch;
- return;
- }
- for (i = 0; i < j; i++)
- p[i] = px[i];
- for (i = j + 1; i < n; i++)
- p[i - 1] = px[i];
- delete[]px;
- px = p;
- n--;
- }
- cout << "Запись удалена \n";
- _getch();
- }
- void pluse(int &n, student *&px)
- {
- setlocale(LC_ALL, "rus");
- student t;
- if (n==0)
- {
- cout << "Массив существует";
- _getch();
- return;
- }
- cout << "Фамилия"; cin >> t.name;
- cout << "Имя"; cin >> t.f;
- cout << "Отчество"; cin >> t.o;
- cout << "Дата рождения";
- cout << "День"; cin >> t.dr.d;
- cout << "Месяц"; cin >> t.dr.m;
- cout << "Год"; cin >> t.dr.y;
- student*promMas = new student[n + 1];
- if (promMas == NULL)
- {
- cout << "Нет памяти";
- _getch();
- return;
- }
- for (int i = 0; i < n; i++)
- {
- promMas[i] = px[i];
- }
- promMas[n] = t;
- delete[]px;
- n++;
- px = new student[n];
- if (px == NULL)
- {
- cout << "Нет памяти";
- _getch();
- return;
- }
- for (int i = 0; i < n; i++)
- {
- px[i] = promMas[i];
- }
- cout << "Запись добавлена";
- delete[] promMas;
- _getch();
- }
- void outputMasStudFilet(int n, student px[])
- {
- ofstream fout("filelab3.txt");
- if (!fout.is_open()) cout << "Ошибка";
- int j;
- fout << "_____________________________________________________________\n";
- fout << " | | \n";
- fout << "№ | Имя студента | Дата рождения \n";
- fout << " | |__________________________\n";
- fout << " | | День | Месяц | Год \n";
- fout << "__|___________________________________________________________\n";
- for (j = 0; j < n; j++)
- fout << setw(2) << j + 1 << "|" << setw(27) << px[j].name << " " << px[j].f << "." << px[j].o << " | " << setw(6) << px[j].dr.d << " | " << setw(7) << px[j].dr.m << " | " << setw(4) << px[j].dr.y << endl;
- }
- void outputMasStudFiletvixod(int n, student px[])
- {
- ofstream fout("filelab4.txt");
- if (!fout.is_open()) cout << "Ошибка";
- int j;
- fout << "_____________________________________________________________\n";
- fout << " | | \n";
- fout << "№ | Имя студента | Дата рождения \n";
- fout << " | |__________________________\n";
- fout << " | | День | Месяц | Год \n";
- fout << "__|___________________________________________________________\n";
- for (j = 0; j < n; j++)
- fout << setw(2) << j + 1 << "|" << setw(27) << px[j].name << " " << px[j].f << "." << px[j].o << " | " << setw(6) << px[j].dr.d << " | " << setw(7) << px[j].dr.m << " | " << setw(4) << px[j].dr.y << endl;
- }
- void Perechen(int n, student px[], int &k, sezon *&py)
- {
- int i, j;
- int l = 4;
- k = 4;
- sezon *y;
- y = new sezon[n];
- if (y==NULL)
- {
- cout << "Перечень пуст\n";
- _getch();
- return;
- }
- y[0].col = 0;
- y[1].col = 0;
- y[2].col = 0;
- y[3].col = 0;
- for (i = 0; i < n; i++)
- {
- if ((px[i].dr.m == 12) || (px[i].dr.m == 1) || (px[i].dr.m == 2))
- {
- y[0].col++;
- }
- if ((px[i].dr.m == 3) || (px[i].dr.m == 4) || (px[i].dr.m == 5))
- {
- y[1].col++;
- }
- if ((px[i].dr.m == 6) || (px[i].dr.m == 7) || (px[i].dr.m == 8))
- {
- y[2].col++;
- }
- if ((px[i].dr.m == 9) || (px[i].dr.m == 10) || (px[i].dr.m == 11))
- {
- y[3].col++;
- }
- }
- y[0].sez = "зима";
- y[1].sez = "весна";
- y[2].sez = "лето";
- y[3].sez = "осень";
- if (py != NULL) delete[]py;
- py = new sezon[k];
- if (py == NULL)
- {
- cout << "Нет памяти";
- _getch();
- delete[]py;
- return;
- }
- for (j = 0; j < l; j++)
- {
- py[j] = y[j];
- }
- delete[]y;
- cout << "Перечень составлен \n ";
- _getch();
- }
- void outputPerechen(int k, sezon py[])
- {
- int j;
- cout << "______________________________________________________________\n";
- cout << " | Перечень \n";
- cout << "№ |___________________________________________________________\n";
- cout << " | Сезон | Колличество годов \n";
- cout << "__|___________________________________________________________\n";
- for (j = 0; j < k; j++)
- cout << setw(2) << j + 1 << "|" << setw(33) << py[j].sez << " | " << setw(21) << py[j].col << " | " << endl;
- }
- void sortPerechenyb(int m, sezon py[])
- {
- sezon s;
- for (int i = 0; i < m; i++)
- for (int j = 0; j < m; j++)
- {
- if (py[i].col > py[j].col)
- {
- s = py[i];
- py[i] = py[j];
- py[j] = s;
- }
- }
- }
- void outputPerechenFile(int m, sezon py[])
- {
- ofstream fout("filelab5.txt");
- if (!fout.is_open()) cout << "Ошибка";
- int j;
- fout << "______________________________________________________________\n";
- fout << " | Перечень \n";
- fout << "№ |__________________________________________________________ \n";
- fout << " | Сезон | Колличество годов \n";
- fout << "__|___________________________________________________________\n";
- for (j = 0; j < m; j++)
- fout << setw(2) << j + 1 << "|" << setw(33) << py[j].sez << " | " << setw(21) << py[j].col << " | " << endl;
- }
- int main()
- {
- student *px(NULL), *pb(NULL);
- sezon *py(NULL);
- int n(0),m(0), k(0);
- //int k = 4;
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- }
- setlocale(LC_ALL, "rus");
- while (1)
- {
- system("cls");
- cout << "1-ввод данных из файла" << endl;
- cout << "2-вывод данных на экран" << endl;
- cout << "3-сортировка входного массива по году в порядке возростания" << endl;
- cout << "4-сортировка входного массива по алфавиту" << endl;
- cout << "5-сортировка входного массива по алфавиту и по дате" << endl;
- cout << "6-удаление строки" << endl;
- cout << "7-добавление строки" << endl;
- cout << "8- вывод в файл" << endl;
- cout << "9-в порядке возрастания даты рождения выходной" << endl;
- cout << "10- вывод в файл выходной" << endl;
- cout << "11- перечень" << endl;
- cout << "12- вывод перечня на экран" << endl;
- cout << "13- сортировка перечня в порядке убывания" << endl;
- cout << "14-вывод перечня в файл" << endl;
- int ch;
- cin >> ch;
- switch (ch)
- {
- case 1: inputMasStudFile(n, px);
- case 2: outputMasStudFile(n, px); break;
- case 3: sortyear(n, px); break;
- case 4: sortname(n, px); break;
- case 5: sortnameyear(n, px); break;
- case 6: deleten(n, px); break;
- case 7: pluse(n, px); break;
- case 8: outputMasStudFilet(n, px); break;
- case 9: sortyearvixod(n, px); break;
- case 10: outputMasStudFiletvixod(n, px); break;
- case 11: Perechen(n, px, k, py); break;
- case 12:outputPerechen(k, py); break;
- case 13: sortPerechenyb(k, py); break;
- case 14: outputPerechenFile(k, py); break;
- }
- _getch();
- }
- _getch();
- return 0;
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement