Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <windows.h>
- #include <vector>
- #include <math.h>
- struct referat {
- char topic[25];
- char author[20];
- int str;
- char date[10];
- };
- using namespace std;
- void printTask() {
- cout << "База данных о рефератах с возможностью редактирования" << endl;
- }
- bool checkPath(string path) {
- ifstream file(path);
- if (file.is_open()) {
- cout << path << " найден" << endl;
- return true;
- }
- else {
- cout << path << " не найден" << endl;
- return false;
- }
- }
- string getPathToFile() {
- string path;
- bool isNotValid = false;
- do {
- cout << "Введите абсолютный путь к файлу с данными" << endl;
- cin >> path;
- } while (!checkPath(path));
- return path;
- }
- int getCount() {
- ifstream file("Data.txt");
- file.open("Data.txt");
- file.clear();
- referat a;
- int i = 0;
- while (!file.eof())
- {
- file >> a.topic >> a.author >> a.str >> a.date;
- i++;
- }
- return i;
- }
- vector <referat> getDataFromFile() {
- vector <referat> a;
- ifstream file("Data.txt");
- file.open("Data.txt");
- file.clear();
- int n = getCount();
- referat input;
- for (int i = 0; i < n; i++) {
- file >> input.topic >> input.author >> input.str >> input.date;
- a.push_back(input);
- }
- file.close();
- return a;
- }
- void printData(vector <referat> arr) {
- cout << "Данные: " << endl << endl;
- cout << "Номер " << "Тема " << "Автор " << "Кол-во страниц " << "Дата " << endl << endl;
- for (int i = 0; i < arr.size(); i++) {
- cout << i + 1;
- string s = to_string(i + 1);
- for (int i = s.size(); i < 6; i++)
- cout << " ";
- cout << arr[i].topic;
- s = arr[i].topic;
- for (int i = s.size(); i < 24; i++)
- cout << " ";
- cout << arr[i].author;
- s = arr[i].author;
- for (int i = s.size(); i < 19; i++)
- cout << " ";
- cout << arr[i].str;
- auto ss = to_string(arr[i].str);
- for (int i = ss.size(); i < 20; i++)
- cout << " ";
- cout << arr[i].date;
- s = arr[i].date;
- cout << endl;
- }
- cout << endl;
- }
- short chooseAct() {
- cout << "Выберите действие: " << endl << endl;
- cout << "1 - Добавить реферат" << endl;
- cout << "2 - Удалить реферат" << endl;
- cout << "3 - Редактировать реферат" << endl;
- cout << "4 - показать сведения о рефератах, написанных в текущем году с количеством страниц не более 20 в порядке дат написания. "
- << endl;
- short act;
- do {
- cin >> act;
- if (act != 1 && act != 2 && act != 3 && act != 4)
- cout << "Введите 1, 2, 3 или 4" << endl;
- } while (act != 2 && act != 1 && act != 3 && act != 4);
- return act;
- }
- referat newReferat() {
- referat a;
- cout << "Введите тему реферата:" << endl;
- cin >> a.topic;
- OemToCharA(a.topic, a.topic);
- cout << "Введите автора реферата:" << endl;
- cin >> a.author;
- OemToCharA(a.author, a.author);
- cout << "Введите кол-во страниц реферата:" << endl;
- cin >> a.str;
- cout << "Введите дату написания реферата:" << endl;
- cin >> a.date;
- OemToCharA(a.date, a.date);
- return a;
- }
- void addData() {
- referat a = newReferat();
- ofstream file;
- file.open("Data.txt", ios::app);
- file << endl;
- file << a.topic << " " << a.author << " " << a.str << " " << a.date;
- file.close();
- }
- int chooseIndex() {
- cout << "Введите номер реферата: ";
- int n;
- cin >> n;
- return n;
- }
- void deleteData() {
- int deleteIndex = chooseIndex();
- ifstream file("Data.txt");
- ofstream newFile("NewData.txt");
- int id = 0;
- while (!file.eof()) {
- id++;
- string data, s;
- for (int i = 0; i < 4; i++) {
- file >> s;
- if (i != 3)
- data += s + " ";
- else
- data += s;
- }
- if (id != deleteIndex) {
- newFile << data;
- if (!file.eof() && data != "")
- newFile << endl;
- }
- }
- file.close();
- newFile.close();
- remove("Data.txt");
- rename("NewData.txt", "Data.txt");
- }
- void changeData() {
- int changeIndex = chooseIndex();
- referat a = newReferat();
- ifstream file("Data.txt");
- ofstream newFile("NewData.txt");
- int id = 0;
- while (!file.eof()) {
- id++;
- string data, s;
- for (int i = 0; i < 4; i++) {
- file >> s;
- if (i != 3)
- data += s + " ";
- else
- data += s;
- }
- if (id != changeIndex)
- newFile << data;
- else
- newFile << a.topic << " " << a.author << " " << a.str << " " << a.date;
- if (!file.eof() && data != "")
- newFile << endl;
- }
- file.close();
- newFile.close();
- remove("Data.txt");
- rename("NewData.txt", "Data.txt");
- }
- vector <referat> findRef(vector <referat> arr) {
- vector <referat> a;
- for (int i = 0; i < arr.size(); i++) {
- string s = arr[i].date;
- short k = 2;
- while (k > 0) {
- if (s[0] == '.')
- k--;
- s.erase(0, 1);
- }
- if (s == "2021")
- a.push_back(arr[i]);
- }
- return a;
- }
- vector <referat> throwOutRef(vector <referat> arr) {
- vector <referat> b;
- for (int i = 0; i < arr.size(); i++)
- if (arr[i].str <= 20)
- b.push_back(arr[i]);
- return b;
- }
- vector <int> getDay(vector<referat> arr) {
- vector <referat> b;
- vector <int> day;
- for (int i = 0; i < arr.size(); i++) {
- b.push_back(arr[i]);
- string s = arr[i].date;
- short k = 1;
- string sDay = "";
- string sMonth = "";
- while (k > 0) {
- if (s[0] == '.')
- k--;
- else
- sDay = sDay + s[0];
- s.erase(0, 1);
- }
- day.push_back(stoi(sDay));
- }
- return day;
- }
- vector <int> getMonth(vector<referat> arr) {
- vector <referat> b;
- vector <int> month;
- for (int i = 0; i < arr.size(); i++) {
- b.push_back(arr[i]);
- string s = arr[i].date;
- short k = 2;
- string sDay = "";
- string sMonth = "";
- while (k > 0) {
- if (s[0] == '.')
- k--;
- else
- if (k == 2)
- sDay = sDay + s[0];
- else
- sMonth = sMonth + s[0];
- s.erase(0, 1);
- }
- month.push_back(stoi(sMonth));
- }
- return month;
- }
- vector <referat> sortByMonth(vector <referat> arr) {
- vector <int> month = getMonth(arr);
- for (int i = 0; i < arr.size() - 1; i++)
- for (int j = i; j < arr.size() - i; j++)
- if (month[i] > month[j]) {
- swap(arr[i], arr[j]);
- swap(month[i], month[j]);
- }
- return arr;
- }
- vector <referat> sortByDay(vector <referat> arr) {
- vector <int> day = getDay(arr);
- vector <int> month = getMonth(arr);
- int l = 0;
- int r = 0;
- while (l <= arr.size()) {
- while (month[l] == month[r] && r < arr.size() - 1)
- r++;
- if (r == arr.size() - 1)
- if (month[l] == month[r])
- r++;
- for (int i = l; i < r; i++)
- for (int j = i; j < r - i; j++)
- if (day[i] > day[j]) {
- swap(arr[i], arr[j]);
- swap(day[i], day[j]);
- }
- r++;
- l = r;
- }
- return arr;
- }
- void printRef(vector <referat> arr) {
- cout << endl;
- for (int i = 0; i < arr.size(); i++) {
- cout << i + 1;
- string s = to_string(i + 1);
- for (int i = s.size(); i < 6; i++)
- cout << " ";
- cout << arr[i].topic;
- s = arr[i].topic;
- for (int i = s.size(); i < 24; i++)
- cout << " ";
- cout << arr[i].author;
- s = arr[i].author;
- for (int i = s.size(); i < 19; i++)
- cout << " ";
- cout << arr[i].str;
- auto ss = to_string(arr[i].str);
- for (int i = ss.size(); i < 20; i++)
- cout << " ";
- cout << arr[i].date;
- s = arr[i].date;
- cout << endl;
- }
- cout << endl;
- }
- void start() {
- printTask();
- while (1 == 1) {
- vector <referat> arr = getDataFromFile();
- printData(arr);
- short act = chooseAct();
- if (act == 1)
- addData();
- if (act == 2)
- deleteData();
- if (act == 3)
- changeData();
- if (act == 4) {
- vector <referat> a = findRef(arr);
- a = throwOutRef(a);
- a = sortByMonth(a);
- a = sortByDay(a);
- printRef(a);
- }
- }
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- start();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement